This repository was archived by the owner on Sep 4, 2022. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathmain.go
124 lines (107 loc) · 3.29 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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
package main
//_ "net/http/pprof"
import (
"net/http"
"time"
_ "github.com/lib/pq"
"github.com/apex/log"
"github.com/apex/log/handlers/logfmt"
"github.com/caarlos0/watchub/config"
"github.com/caarlos0/watchub/controllers"
"github.com/caarlos0/watchub/datastore/database"
"github.com/caarlos0/watchub/oauth"
"github.com/caarlos0/watchub/scheduler"
"github.com/gorilla/context"
"github.com/gorilla/mux"
"github.com/gorilla/sessions"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
"github.com/prometheus/client_golang/prometheus/promhttp"
)
// TODO: refactor this.
// nolint: funlen
func main() {
log.SetHandler(logfmt.Default)
log.SetLevel(log.InfoLevel)
log.Info("starting up...")
var config = config.Get()
var db = database.Connect(config.DatabaseURL)
defer func() { _ = db.Close() }()
var store = database.NewDatastore(db)
// oauth
var session = sessions.NewCookieStore([]byte(config.SessionSecret))
session.Options = &sessions.Options{
MaxAge: 3600,
HttpOnly: true,
SameSite: http.SameSiteLaxMode,
Path: "/",
}
var oauth = oauth.New(config)
var loginCtrl = controllers.NewLogin(config, session, oauth, store)
// schedulers
var sch = scheduler.New(config, store, oauth, session)
sch.Start()
defer sch.Stop()
// routes
var mux = mux.NewRouter()
mux.PathPrefix("/static/").Handler(
http.StripPrefix("/static/", http.FileServer(http.Dir("static"))),
)
mux.Methods(http.MethodGet).Path("/").HandlerFunc(
controllers.NewIndex(config, session, store).Handler,
)
mux.Methods(http.MethodGet).Path("/donate").HandlerFunc(
controllers.NewDonate(config, session).Handler,
)
mux.Methods(http.MethodGet).Path("/contact").HandlerFunc(
controllers.NewContact(config, session).Handler,
)
mux.Methods(http.MethodGet).Path("/schedule").HandlerFunc(
controllers.NewSchedule(config, session, store).Handler,
)
mux.Methods(http.MethodGet).Path("/login").HandlerFunc(
loginCtrl.Handler,
)
mux.Methods(http.MethodGet).Path("/login/callback").HandlerFunc(
loginCtrl.CallbackHandler,
)
mux.Path("/logout").HandlerFunc(
controllers.NewLogout(config, session).Handler,
)
// prometheus stuff
var requestCounter = promauto.NewCounterVec(prometheus.CounterOpts{
Namespace: "watchub",
Subsystem: "http",
Name: "requests_total",
Help: "total requests",
}, []string{"code", "method"})
var responseObserver = promauto.NewSummaryVec(prometheus.SummaryOpts{
Namespace: "watchub",
Subsystem: "http",
Name: "responses",
Help: "response times and counts",
}, []string{"code", "method"})
prometheus.MustRegister(scheduler.TimeGauge)
prometheus.MustRegister(scheduler.ErrorGauge)
mux.Methods(http.MethodGet).Path("/metrics").Handler(promhttp.Handler())
mux.PathPrefix("/debug").Handler(http.DefaultServeMux)
var handler = context.ClearHandler(
promhttp.InstrumentHandlerDuration(
responseObserver,
promhttp.InstrumentHandlerCounter(
requestCounter,
mux,
),
),
)
var server = &http.Server{
Handler: handler,
Addr: ":" + config.Port,
WriteTimeout: 15 * time.Second,
ReadTimeout: 15 * time.Second,
}
log.WithField("addr", server.Addr).Info("started")
if err := server.ListenAndServe(); err != nil {
log.WithError(err).Fatal("failed to start up server")
}
}