forked from librespeed/speedtest-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
87 lines (78 loc) · 1.64 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
package main
import (
"context"
"log/slog"
"os"
"os/signal"
"sync"
"syscall"
"time"
_ "time/tzdata"
"github.com/librespeed/speedtest/config"
"github.com/librespeed/speedtest/database"
"github.com/librespeed/speedtest/results"
"github.com/librespeed/speedtest/web"
"github.com/rs/zerolog"
slogzerolog "github.com/samber/slog-zerolog/v2"
_ "golang.org/x/crypto/x509roots/fallback"
)
func init() {
zerologLogger := zerolog.New(zerolog.ConsoleWriter{
Out: os.Stderr,
TimeFormat: time.RFC3339,
}).
With().
Caller().
Logger()
logger := slog.New(
slogzerolog.Option{
Level: slog.LevelInfo,
Logger: &zerologLogger,
}.
NewZerologHandler(),
)
slog.SetDefault(logger)
}
func main() {
conf, err := config.Load()
if err != nil {
slog.Error("failed to load config", slog.Any("error", err))
return
}
web.SetServerLocation(conf)
results.Initialize(conf)
err = database.SetDBInfo(conf)
if err != nil {
slog.Error("init db", slog.Any("error", err))
return
}
ctx, cancel := context.WithCancel(context.Background())
stopWait, closeFn := onceChan[struct{}]()
go func() {
err := web.ListenAndServe(ctx, conf)
if err != nil {
slog.Error("web server", slog.Any("error", err))
}
closeFn()
}()
go wait(closeFn)
<-stopWait
slog.Info("server stopped")
cancel()
}
func wait(closeFn func()) {
sigChan := make(chan os.Signal, 1)
signal.Notify(sigChan, os.Interrupt, syscall.SIGTERM)
sig := <-sigChan
slog.Info("signal received", "signal", sig.String())
closeFn()
}
func onceChan[T any]() (chan T, func()) {
var once sync.Once
var c = make(chan T)
return c, func() {
once.Do(func() {
close(c)
})
}
}