-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
60 lines (47 loc) · 2.06 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
package main
import (
"net/http"
"github.com/Squwid/bytegolf/auth"
"github.com/Squwid/bytegolf/compiler"
"github.com/Squwid/bytegolf/compiler/bg"
_ "github.com/Squwid/bytegolf/db"
"github.com/Squwid/bytegolf/globals"
"github.com/Squwid/bytegolf/holes"
"github.com/Squwid/bytegolf/profiles"
"github.com/gorilla/mux"
log "github.com/sirupsen/logrus"
)
func main() {
log.SetLevel(log.DebugLevel)
port := globals.Port()
env := globals.Env()
r := mux.NewRouter()
r.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { w.WriteHeader(200) })
r.HandleFunc("/login/check", auth.CallbackHandler)
r.HandleFunc("/login", auth.LoginHandler)
r.HandleFunc("/api/test/{hole}/{id}", holes.GetTest).Methods("GET")
r.HandleFunc("/api/tests/{hole}", holes.ListTests).Methods("GET")
r.HandleFunc("/api/holes", holes.ListHoles).Methods("GET")
r.HandleFunc("/api/hole/{id}", holes.GetHole).Methods("GET")
r.HandleFunc("/api/submissions", compiler.ListSubmissions).Methods("GET")
r.HandleFunc("/api/submissions/{id}", compiler.GetSubmission).Methods("GET")
r.HandleFunc("/api/submissions/best/{hole}", compiler.GetBestSubmissionHandler).Methods("GET")
r.HandleFunc("/api/submit/{hole}", bg.SubmissionHandler).Methods("POST")
r.HandleFunc("/api/leaderboards", compiler.LeaderboardHandler).Methods("GET")
r.HandleFunc("/api/profile/{id}", profiles.GetProfile).Methods("GET") // checks if a user is logged in
r.HandleFunc("/api/claims", auth.ShowClaims).Methods("GET") // Returns a user's claims and see if they are logged in
log.WithField("Env", env).Infof("Starting container on port :%s", port)
http.ListenAndServe(":"+port, loggedIn(cors(r)))
}
func cors(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Access-Control-Allow-Methods", "GET,POST,OPTIONS")
w.Header().Set("Access-Control-Allow-Origin", globals.FrontendAddr())
w.Header().Set("Access-Control-Allow-Credentials", "true")
if r.Method == http.MethodOptions {
w.WriteHeader(200)
return
}
next.ServeHTTP(w, r)
})
}