-
Notifications
You must be signed in to change notification settings - Fork 6
/
main.go
92 lines (79 loc) · 2.27 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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
package main
import (
"context"
"embed"
"flag"
"io/fs"
"log/slog"
"os"
"os/signal"
"syscall"
"github.com/mr-karan/gullak/internal/llm"
)
var (
buildString = "unknwown"
)
//go:embed ui/dist/*
var content embed.FS
func main() {
cfgPath := flag.String("config", "config.toml", "File path to the config file")
flag.Parse()
// Initialize the configuration.
ko, err := initConfig(*cfgPath)
if err != nil {
slog.Error("Error initializing config", "error", err)
os.Exit(1)
}
// Create a sub filesystem from the embedded files to directly access dist contents
// We do this so that `ui/dist/` becomes the root directory for the file server.
subFS, err := fs.Sub(content, "ui/dist")
if err != nil {
slog.Error("Failed to create sub filesystem", "error", err)
os.Exit(1)
}
// Initialise logger.
lgrOpts := &slog.HandlerOptions{
Level: slog.LevelInfo,
}
if ko.Bool("app.debug") {
lgrOpts.Level = slog.LevelDebug
}
logger := slog.New(slog.NewTextHandler(os.Stdout, lgrOpts))
// Initialize the OpenAI client.
llmMgr, err := llm.New(
ko.MustString("openai.token"),
ko.String("openai.base_url"),
ko.MustString("openai.model"),
ko.Duration("openai.timeout"),
logger)
if err != nil {
logger.Error("Error initializing llm", "error", err)
os.Exit(1)
}
logger.Info("Successfully initialized OpenAI client", "model", ko.MustString("openai.model"))
// Initialize the database.
db, err := initDB(ko.MustString("app.db_path"), ko.String("app.currency"))
if err != nil {
logger.Error("Error initializing database", "error", err)
os.Exit(1)
}
logger.Info("Successfully connected to the database and tables created", "path", ko.MustString("app.db_path"))
// Create a context that is cancelled on SIGTERM or SIGINT
ctx, cancel := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM)
defer cancel()
logger.Info("Starting the app", "version", buildString, "addr", ko.MustString("http.address"), "timeout", ko.MustDuration("http.timeout"))
app := initApp(
ko.MustString("http.address"),
ko.MustDuration("http.timeout"),
subFS,
db,
llmMgr,
logger,
)
if err := app.Start(ctx); err != nil {
logger.Error("Error starting http server", "error", err)
os.Exit(1)
}
<-ctx.Done() // Wait for SIGINT or SIGTERM.
slog.Info("Shutting down!")
}