-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
64 lines (53 loc) · 1.13 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
package main
import (
"embed"
"fmt"
"log/slog"
"net/http"
"os"
"os/signal"
"syscall"
"github.com/masl/answertag/environment"
"github.com/masl/answertag/storage/inmemory"
"github.com/masl/answertag/tmpl"
"github.com/masl/answertag/web"
"github.com/masl/answertag/ws"
)
var (
//go:embed templates/*
templateFS embed.FS
//go:embed static/*
staticFS embed.FS
)
func main() {
handleSignal()
tm := tmpl.NewTemplateManager()
err := tmpl.RegisterTemplates(tm, templateFS)
if err != nil {
slog.Error("error registering templates", "error", err)
os.Exit(1)
}
store := inmemory.New()
hub := ws.NewHub(store, tm)
go hub.Run()
port, err := environment.Port(3000)
if err != nil {
slog.Error("error getting port", "error", err)
os.Exit(1)
}
server := &http.Server{
Addr: fmt.Sprintf(":%d", port),
Handler: web.GetRouter(store, tm, staticFS, hub),
}
slog.Info("web server listening", "port", port)
panic(server.ListenAndServe())
}
func handleSignal() {
sig := make(chan os.Signal, 1)
signal.Notify(sig, syscall.SIGINT, syscall.SIGTERM)
go func() {
<-sig
slog.Info("exiting, bye-bye!")
os.Exit(1)
}()
}