-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
57 lines (50 loc) · 1.43 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
package main
import (
"log/slog"
"net/http"
"os"
"synochatgpt/internal"
)
func abort(msg string, args ...any) {
slog.Error(msg, args...)
os.Exit(1)
}
func mustGetEnv(name string) string {
value, ok := os.LookupEnv(name)
if !ok {
abort("missing env", "name", name)
}
return value
}
func getEnvOr(name string, defaultValue string) string {
value, ok := os.LookupEnv(name)
if !ok {
return defaultValue
}
return value
}
func main() {
slog.SetDefault(slog.New(slog.NewTextHandler(os.Stderr, &slog.HandlerOptions{
Level: func() slog.Level {
var level slog.Level
if err := level.UnmarshalText([]byte(getEnvOr("LOG_LEVEL", "info"))); err != nil {
abort("invalid log level", "err", err)
}
return level
}(),
})))
http.HandleFunc("/", internal.NewHandler(
internal.SynoChatBot{
IncomingURL: mustGetEnv("SYNOCHATGPT_BOT_INCOMING_URL"),
Token: mustGetEnv("SYNOCHATGPT_BOT_TOKEN"),
},
internal.ChatGPTAPI{
ChatContext: getEnvOr("SYNOCHATGPT_API_CONTEXT", "Answers must be very concise. Reply in the same language. For Chinese questions, must reply with use traditional chinese characters."),
ModelName: getEnvOr("SYNOCHATGPT_API_MODEL", "gpt-3.5-turbo"),
BearerToken: mustGetEnv("SYNOCHATGPT_API_TOKEN"),
},
))
listenAddr := mustGetEnv("SYNOCHATGPT_LISTEN_ADDR")
slog.Info("listening on address", "address", listenAddr)
slog.Error("server shutdown", "err", http.ListenAndServe(listenAddr, nil))
}