forked from caarlos0/starcharts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
85 lines (77 loc) · 2.25 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
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
func main() {
log.SetHandler(text.New(os.Stderr))
config := config.Get()
ctx := log.WithField("port", config.Port)
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).
HandlerFunc(controller.Index(static))
r.PathPrefix("/static/").
Methods(http.MethodGet).
Handler(http.FileServer(http.FS(static)))
r.Path("/{owner}/{repo}.svg").
Methods(http.MethodGet).
HandlerFunc(controller.GetRepoChart(github, cache))
r.Path("/{owner}/{repo}").
Methods(http.MethodGet).
HandlerFunc(controller.GetRepo(static, github, cache))
// 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: "0.0.0.0:" + config.Port,
WriteTimeout: 30 * time.Second,
ReadTimeout: 30 * time.Second,
}
ctx.Info("starting up...")
ctx.WithError(srv.ListenAndServe()).Error("failed to start up server")
}