-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
68 lines (57 loc) · 1.77 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
package main
import (
"fmt"
"net/http"
"os"
"os/signal"
"syscall"
"github.com/go-kit/kit/log"
"github.com/maddevsio/screen-monitoring/dashboard"
gometrics "github.com/rcrowley/go-metrics"
"golang.org/x/net/context"
)
func main() {
ctx := context.Background()
logger := log.NewLogfmtLogger(os.Stderr)
requestCount := gometrics.NewCounter()
requestLatency := gometrics.NewHistogram(gometrics.NewUniformSample(100))
countResult := gometrics.NewHistogram(gometrics.NewUniformSample(100))
dashboardDbManager := dashboard.NewDbManager("screen_monitoring.db")
dashboardMigrator := dashboard.NewMigrator("screen_monitoring.db", "./dashboard/migrations")
var svc dashboard.DashboardService
svc = dashboard.NewDashboardService(dashboardMigrator, dashboardDbManager)
svc = dashboard.NewLoggerService(logger, svc)
svc = dashboard.NewInstrumentingMiddleware(
requestCount,
requestLatency,
countResult,
svc,
)
svc.Init()
mux := http.NewServeMux()
mux.Handle("/dashboard/v1/", dashboard.MakeHandler(ctx, svc, logger))
mux.Handle("/", http.FileServer(http.Dir("./public")))
http.Handle("/", accessControl(mux))
errs := make(chan error, 2)
go func() {
logger.Log("msg", "HTTP", "addr", ":8080")
errs <- http.ListenAndServe(":8080", nil)
}()
go func() {
c := make(chan os.Signal)
signal.Notify(c, syscall.SIGINT)
errs <- fmt.Errorf("%s", <-c)
}()
logger.Log("terminated", <-errs)
}
func accessControl(h http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Access-Control-Allow-Origin", "*")
w.Header().Set("Access-Control-Allow-Methods", "GET, POST, OPTIONS")
w.Header().Set("Access-Control-Allow-Headers", "Origin, Content-Type")
if r.Method == "OPTIONS" {
return
}
h.ServeHTTP(w, r)
})
}