-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
69 lines (59 loc) · 1.36 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
package main
import (
"flag"
"log"
"net/http"
"os"
"time"
"github.com/pressly/chi"
"github.com/pressly/chi/middleware"
"github.com/rs/xlog"
)
var (
port string
)
func init() {
const defaultPort = "8080"
flag.StringVar(&port, "p", defaultPort, "the port to listen on")
}
func main() {
flag.Parse()
var err error
db, err := newBoltStore()
if err != nil {
log.Fatalln(err)
}
defer db.Close()
host, _ := os.Hostname()
r := chi.NewRouter()
r.Use(xlog.NewHandler(xlog.Config{
Output: xlog.NewOutputChannel(xlog.NewConsoleOutput()),
Fields: xlog.F{"hostname": host},
}))
r.Use(xlog.MethodHandler("method"))
r.Use(xlog.URLHandler("url"))
r.Use(xlog.RemoteAddrHandler("ip"))
r.Use(xlog.RequestIDHandler("req_id", "Request-Id"))
r.Use(logHandler)
r.Use(middleware.Recoverer)
r.Use(middleware.Timeout(5 * time.Second))
r.Route("/api", func(r chi.Router) {
r.Route("/alert", func(r chi.Router) {
r.Post("/", postAlert(db))
r.Route("/:alertID", func(r chi.Router) {
r.Use(alertCtx)
r.Get("/", getAlert(db))
r.Delete("/", deleteAlert(db))
})
})
r.Route("/alerts", func(r chi.Router) {
r.Get("/", listAlerts(db))
r.Get("/:prefix", listAlerts(db))
})
r.Route("/backup", func(r chi.Router) {
r.Get("/", boltBackupHandler(db))
})
})
r.FileServer("/", http.Dir("./dashboard/"))
log.Fatal(http.ListenAndServe(":"+port, r))
}