-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
93 lines (82 loc) · 1.91 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
93
package main
import (
"embed"
"fmt"
"log/slog"
"net/http"
cookie "github.com/ddbgio/cookie/v2"
logger "github.com/ddbgio/log"
)
const embedDir = "static"
//go:embed static
var content embed.FS // object representing the embedded directory
var log *slog.Logger
func init() {
// setup logger
opts := slog.HandlerOptions{
Level: slog.LevelDebug,
AddSource: true,
}
var err error
log, err = logger.New(opts)
log = log.With("service", "nailivic")
if err != nil {
panic(err)
}
}
func main() {
var err error
// setup cookies
// TODO this must be changed if more than one server is ever active
cookieGlobalSecret, err = cookie.NewCookieSecret()
if err != nil {
log.Error("failed to generate secret",
"error", err,
)
panic(err)
}
log.Warn("secret set", "secret", string(cookieGlobalSecret))
// read embed dir
_, err = content.ReadDir(embedDir)
if err != nil {
log.Error("failed to read directory",
"error", err,
)
panic(err)
}
// setup database
// TODO extract ddbgio/db into it's own package,
// and use that for the backend
// ROUTES
// full pages
http.HandleFunc("/secret", logMW(authMW(serveSecret)))
http.HandleFunc("/parts", logMW(serveParts))
http.HandleFunc("/inventory", logMW(serveInventory))
http.HandleFunc("/", logMW(serveRoot))
// htmx components
http.HandleFunc("/htmx/{component}", logMW(serveHtmx))
// static files
http.Handle("/static/",
http.FileServer(http.FS(content)),
)
http.HandleFunc("/favicon.ico", func(w http.ResponseWriter, r *http.Request) {
log.Debug("favicon requested")
http.ServeFile(w, r, "static/img/favicon.ico")
})
// start server
port := 8008
log.Info("starting server",
"port", port,
)
log.Info("server is running",
"port", port,
)
err = http.ListenAndServe(fmt.Sprintf(":%d", port), nil)
if err != nil {
slog.Error("failed to start server",
"error", err,
"port", port,
)
}
log.Info("server stopped", "port", port)
}