-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
90 lines (72 loc) · 2.12 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
package main
import (
"flag"
"fmt"
"log"
"net/http"
"os"
"strings"
"github.com/braintree/manners"
_ "github.com/freemed/remitt-server/api"
"github.com/freemed/remitt-server/common"
"github.com/freemed/remitt-server/config"
"github.com/freemed/remitt-server/jobqueue"
"github.com/freemed/remitt-server/model"
"github.com/gin-gonic/contrib/gzip"
"github.com/gin-gonic/gin"
)
var (
configFile = flag.String("config-file", "./remitt.yml", "Configuration file")
debug = flag.Bool("debug", false, "Enable debugging (overrides config)")
)
func main() {
flag.Parse()
log.SetFlags(log.LstdFlags | log.Ltime | log.Lshortfile)
c, err := config.LoadConfigWithDefaults(*configFile)
if err != nil {
panic(err)
}
if c == nil {
panic("UNABLE TO LOAD CONFIG")
}
config.Config = c
if *debug {
log.Print("Overriding existing debug configuration")
config.Config.Debug = true
} else {
gin.SetMode(gin.ReleaseMode)
}
log.Print("Initializing database backend")
model.DbMap = model.InitDb()
if config.Config.Paths.TemporaryPath != "/tmp" {
log.Print("Ensuring temporary directory exists")
os.MkdirAll(config.Config.Paths.TemporaryPath, 0700)
}
log.Printf("Initializing worker threads")
jobqueue.StartDispatcher(config.Config.TimingIterations.NumWorkerThreads)
log.Print("Initializing web services")
m := gin.New()
m.Use(gin.Logger())
m.Use(gin.Recovery())
m.Use(BasicAuth(model.BasicAuthCallback, "REMITT"))
// Enable gzip compression
m.Use(gzip.Gzip(gzip.DefaultCompression))
// Serve up the static UI...
m.Static("/ui", "./ui")
m.StaticFile("/favicon.ico", "./ui/favicon.ico")
// ... with a redirection for the root page
m.GET("/", func(c *gin.Context) {
c.Redirect(http.StatusMovedPermanently, "./ui/index.html")
})
a := m.Group("/api")
// Iterate through initializing API maps
for k, v := range common.ApiMap {
f := make([]string, 0)
f = append(f, "AUTH")
log.Printf("Adding handler /api/%s [%s]", k, strings.Join(f, ","))
v(a.Group("/" + k))
}
// HTTP
log.Printf("Launching http on port :%d", config.Config.Port)
log.Fatal(manners.ListenAndServe(fmt.Sprintf(":%d", config.Config.Port), m))
}