-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
96 lines (79 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
94
95
96
package main
import (
"context"
"embed"
"io/fs"
"math/rand"
"os"
"os/signal"
"time"
"bfp/api"
"bfp/conf"
"bfp/echologrus"
"bfp/model"
"github.com/oschwald/geoip2-golang"
log "github.com/sirupsen/logrus"
)
// @title BFP API
// @version 1.0
// @description BFP API
// @license.name Apache 2.0
// @license.url http://www.apache.org/licenses/LICENSE-2.0.html
// @BasePath /api/
// go get -u github.com/swaggo/swag/cmd/swag
//go:generate swag init --parseDependency
//go:generate rm -f docs/swagger.json docs/docs.go
//go:embed static
var frontendStatic embed.FS
func FrontendStatic() fs.FS {
fsys, err := fs.Sub(frontendStatic, "static")
if err != nil {
panic(err)
}
return fsys
}
func main() {
rand.Seed(time.Now().UnixNano())
cfg, err := conf.ReadConfig()
if err != nil {
log.Fatalf("error loading config file: %s", err)
}
initLogger(cfg.LogLevel)
geoDb, err := geoip2.Open("GeoLite2.mmdb")
if err != nil {
log.Panicf("error opening GeoLite2.mmdb: %v", err)
}
defer geoDb.Close()
db, err := model.NewDB(cfg.DBConnectionString)
if err != nil {
log.Panicf("init db: %v", err)
}
defer db.Close()
fpRep := model.NewFingerprint(db)
wordsRep, err := model.NewWordsRepository(db)
if err != nil {
log.Panicf("init words: %v", err)
}
handler := api.NewHandler(fpRep, wordsRep, geoDb)
handler.SetupAPI(cfg)
handler.SetupFrontend(FrontendStatic())
quit := make(chan os.Signal, 2)
signal.Notify(quit, os.Interrupt)
<-quit
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
if err := handler.Shutdown(ctx); err != nil {
log.Errorf("main: shutdown http server: %s", err)
}
}
func initLogger(level string) {
log.SetFormatter(&log.TextFormatter{})
log.SetOutput(os.Stdout)
log.SetLevel(log.DebugLevel)
lvl, err := log.ParseLevel(level)
if err != nil {
log.Warnf("invalid log level %s", level)
}
log.SetLevel(lvl)
echologrus.Logger = log.StandardLogger()
}