/
maze.go
71 lines (56 loc) · 1.45 KB
/
maze.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
package main
import (
"flag"
"net/http"
"time"
"github.com/gin-gonic/gin"
"github.com/ianschenck/envflag"
"github.com/mikkeloscar/maze/checker"
"github.com/mikkeloscar/maze/remote"
"github.com/mikkeloscar/maze/repo"
"github.com/mikkeloscar/maze/router"
"github.com/mikkeloscar/maze/router/middleware/context"
"github.com/mikkeloscar/maze/store/datastore"
log "github.com/sirupsen/logrus"
)
var (
addr = envflag.String("SERVER_ADDR", ":8080", "")
debug = flag.Bool("d", false, "")
check = flag.Bool("check", false, "Enable automatic check of package updates.")
stateTTL = 2 * time.Hour
)
func main() {
flag.Parse()
envflag.Parse()
if !*debug {
// disbale gin debug mode
gin.SetMode(gin.ReleaseMode)
}
err := repo.LoadRepoStorage()
if err != nil {
log.Fatalf("repo storage error: %s", err)
}
log.Printf("using repo storage path: %s", repo.RepoStorage)
ctxStore, err := datastore.Load()
if err != nil {
log.Fatalf("failed to load datastore: %s", err)
}
ctxRemote := remote.Load()
middleware := []gin.HandlerFunc{
context.SetStore(ctxStore),
context.SetRemote(ctxRemote),
}
if *check {
state := checker.NewState(stateTTL)
chck := checker.Checker{
Remote: ctxRemote,
Store: ctxStore,
State: state,
}
go chck.Run()
middleware = append(middleware, context.SetState(state))
}
// setup the server and start listening
handler := router.Load(middleware...)
log.Fatal(http.ListenAndServe(*addr, handler))
}