-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathmain.go
117 lines (101 loc) · 3.24 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
package main
import (
"net/http"
"time"
_ "net/http/pprof"
_ "github.com/lib/pq"
"github.com/apex/httplog"
"github.com/apex/log"
"github.com/apex/log/handlers/logfmt"
"github.com/Github-Web-Apps/Starhub/config"
"github.com/Github-Web-Apps/Starhub/controllers"
"github.com/Github-Web-Apps/Starhub/datastore/database"
"github.com/Github-Web-Apps/Starhub/oauth"
"github.com/Github-Web-Apps/Starhub/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/promhttp"
)
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))
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("/profile").HandlerFunc(
controllers.NewProfile(config, session).Handler,
)
mux.Methods(http.MethodGet).Path("/starred").HandlerFunc(
controllers.NewStarred(config, session).Handler,
)
mux.Methods(http.MethodGet).Path("/startrack").HandlerFunc(
controllers.NewStartrack(config, session).Handler,
)
mux.Methods(http.MethodGet).Path("/downloads").HandlerFunc(
controllers.NewDownloads(config, session).Handler,
)
mux.Methods(http.MethodGet).Path("/tools").HandlerFunc(
controllers.NewTools(config, session).Handler,
)
mux.Methods(http.MethodGet).Path("/apps").HandlerFunc(
controllers.NewApps(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
prometheus.MustRegister(scheduler.TimeGauge)
prometheus.MustRegister(scheduler.ErrorGauge)
mux.Handle("/metrics", promhttp.Handler())
mux.PathPrefix("/debug").Handler(http.DefaultServeMux)
var handler = context.ClearHandler(
httplog.New(
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")
}
}