-
-
Notifications
You must be signed in to change notification settings - Fork 131
/
main.go
91 lines (82 loc) · 2.39 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
package main
import (
"embed"
"net/http"
"os"
"time"
"github.com/apex/httplog"
"github.com/apex/log"
"github.com/apex/log/handlers/text"
"github.com/caarlos0/starcharts/config"
"github.com/caarlos0/starcharts/controller"
"github.com/caarlos0/starcharts/internal/cache"
"github.com/caarlos0/starcharts/internal/github"
"github.com/go-redis/redis"
"github.com/gorilla/mux"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
"github.com/prometheus/client_golang/prometheus/promhttp"
)
//go:embed static/*
var static embed.FS
var version = "devel"
func main() {
log.SetHandler(text.New(os.Stderr))
// log.SetLevel(log.DebugLevel)
config := config.Get()
ctx := log.WithField("listen", config.Listen)
options, err := redis.ParseURL(config.RedisURL)
if err != nil {
log.WithError(err).Fatal("invalid redis_url")
}
redis := redis.NewClient(options)
cache := cache.New(redis)
defer cache.Close()
github := github.New(config, cache)
r := mux.NewRouter()
r.Path("/").
Methods(http.MethodGet).
Handler(controller.Index(static, version))
r.Path("/").
Methods(http.MethodPost).
HandlerFunc(controller.HandleForm())
r.PathPrefix("/static/").
Methods(http.MethodGet).
Handler(http.FileServer(http.FS(static)))
r.Path("/{owner}/{repo}.svg").
Methods(http.MethodGet).
Handler(controller.GetRepoChart(github, cache))
r.Path("/{owner}/{repo}").
Methods(http.MethodGet).
Handler(controller.GetRepo(static, github, cache, version))
// generic metrics
requestCounter := promauto.NewCounterVec(prometheus.CounterOpts{
Namespace: "starcharts",
Subsystem: "http",
Name: "requests_total",
Help: "total requests",
}, []string{"code", "method"})
responseObserver := promauto.NewSummaryVec(prometheus.SummaryOpts{
Namespace: "starcharts",
Subsystem: "http",
Name: "responses",
Help: "response times and counts",
}, []string{"code", "method"})
r.Methods(http.MethodGet).Path("/metrics").Handler(promhttp.Handler())
srv := &http.Server{
Handler: httplog.New(
promhttp.InstrumentHandlerDuration(
responseObserver,
promhttp.InstrumentHandlerCounter(
requestCounter,
r,
),
),
),
Addr: config.Listen,
WriteTimeout: 60 * time.Second,
ReadTimeout: 60 * time.Second,
}
ctx.Info("starting up...")
ctx.WithError(srv.ListenAndServe()).Error("failed to start up server")
}