-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
67 lines (56 loc) · 1.25 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
package main
import (
"net/http"
"os"
"os/signal"
"strconv"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
"gitlab.com/totakoko/onetimesecret/conf"
"gitlab.com/totakoko/onetimesecret/helpers"
"gitlab.com/totakoko/onetimesecret/httpserver"
"gitlab.com/totakoko/onetimesecret/store"
)
func main() {
helpers.TryOrFatal(startServer())
}
func startServer() error {
zerolog.TimeFieldFormat = ""
log.Info().Msg("Starting server")
config, err := conf.New()
if err != nil {
return err
}
logLevel, err := zerolog.ParseLevel(config.LogLevel)
if err != nil {
return err
}
zerolog.SetGlobalLevel(logLevel)
store := store.New(config.Store)
if err := store.Init(); err != nil {
return err
}
server := &httpserver.HTTPServer{
PublicURL: config.PublicURL,
Store: store,
}
if err := server.Init(); err != nil {
return err
}
go func() {
err := server.Run(":" + strconv.Itoa(config.ListenPort))
if err != http.ErrServerClosed {
log.Fatal().Msg(err.Error())
}
log.Warn().Msgf("Server stopped")
}()
quit := make(chan os.Signal, 1)
signal.Notify(quit, os.Interrupt)
<-quit
log.Warn().Msgf("Shutting down server...")
if err := server.Shutdown(); err != nil {
return err
}
log.Warn().Msgf("Server exiting")
return nil
}