-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
83 lines (65 loc) · 1.38 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
package main
import (
"TodoList/internal/config"
"TodoList/internal/pkg/cachetemplate"
"TodoList/internal/pkg/logger"
"TodoList/internal/pkg/render"
"TodoList/internal/server"
"TodoList/internal/store"
"TodoList/internal/todos"
"TodoList/internal/users"
"context"
"database/sql"
"time"
_ "github.com/lib/pq"
"github.com/golangcollege/sessions"
)
func main() {
l := logger.New("02 Jan 15:04", 3)
cfg, err := config.New()
if err != nil {
l.Fatal(err)
}
cachedTemplate, err := cachetemplate.New("./static/templates")
if err != nil {
l.Fatal(err)
}
db, err := openDB(cfg.ConnectionString)
if err != nil {
l.Fatal(err)
}
defer func() {
err = db.Close()
if err != nil {
l.Fatal(err)
}
}()
ps, err := store.NewPostgreStore(db)
if err != nil {
l.Fatal(err)
}
r := render.NewRender(cachedTemplate)
session := sessions.New([]byte(cfg.SecretKey))
t := todos.New(l, db, ps, ps)
u := users.New(l, cfg, ps, r, session)
// TODO: hell of dependencies
s, err := server.New(l, cfg, session, t, u, r)
if err != nil {
l.Fatal(err)
}
s.Start()
}
// TODO: should we here?
func openDB(dsn string) (*sql.DB, error) {
db, err := sql.Open("postgres", dsn)
if err != nil {
return nil, err
}
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
defer cancel()
err = db.PingContext(ctx)
if err != nil {
return nil, err
}
return db, err
}