-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
45 lines (34 loc) · 997 Bytes
/
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
package main
import (
"context"
"log/slog"
"os"
"os/signal"
"syscall"
"github.com/kuberise/todolist/internal/config"
"github.com/kuberise/todolist/internal/controller"
"github.com/kuberise/todolist/internal/gateway/relational"
"github.com/kuberise/todolist/pkg/postgres"
)
func main() {
logger := slog.New(slog.NewTextHandler(os.Stdout, nil))
cfg := config.New(logger)
db, err := postgres.New(&cfg.Postgres)
if err != nil {
logger.Error("error connecting to postgres", "error", err)
os.Exit(1)
}
_, err = db.Exec("CREATE TABLE IF NOT EXISTS todos (title VARCHAR(255) UNIQUE)")
if err != nil {
logger.Error("error creating table", "error", err)
os.Exit(1)
}
repository := relational.NewRepository(db)
hc := controller.NewHTTPController(logger, &cfg.HTTP, repository)
ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM)
defer stop()
err = hc.Run(ctx)
if err != nil {
logger.Error("http server error", "error", err)
}
}