This repository has been archived by the owner on May 3, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
80 lines (62 loc) · 2.08 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
package main
import (
"log"
"net/http"
"os"
"github.com/Fakerr/sern/http/handlers"
"github.com/Fakerr/sern/http/handlers/api"
"github.com/Fakerr/sern/http/handlers/authentication"
"github.com/Fakerr/sern/http/handlers/hooks"
"github.com/Fakerr/sern/http/middleware"
"github.com/Fakerr/sern/http/session"
"github.com/Fakerr/sern/persist"
"github.com/gorilla/mux"
"github.com/justinas/alice"
)
const (
PUBLIC_DIR = "/public/"
)
// Force SSL redirection
func ForceSsl(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if os.Getenv("ENV") == "production" {
if r.Header.Get("x-forwarded-proto") != "https" {
sslUrl := "https://" + r.Host + r.RequestURI
http.Redirect(w, r, sslUrl, http.StatusTemporaryRedirect)
return
}
}
next.ServeHTTP(w, r)
})
}
func main() {
// Init postgres connection
persist.InitConnection()
// Init database tables
persist.InitTables()
// Init Redis connection pool
persist.InitRedis()
// Configure the session cookie store
session.Configure()
r := mux.NewRouter()
// Middlewares
r.Use(middleware.LogRequest)
r.PathPrefix(PUBLIC_DIR).Handler(http.StripPrefix(PUBLIC_DIR, http.FileServer(http.Dir("."+PUBLIC_DIR))))
r.PathPrefix("/static").Handler(http.StripPrefix("/static/", http.FileServer(http.Dir("./ui/build/static"))))
r.HandleFunc("/", handlers.MainHandler)
r.HandleFunc("/login", authentication.LoginHandler)
r.HandleFunc("/logout", authentication.LogoutHandler)
r.HandleFunc("/github_oauth_cb", authentication.GithubCallbackHandler)
r.HandleFunc("/github_webhook_cb", hooks.WebhookCallbackHandler)
// API requiring authenitcation.
r.Handle("/api/repos", alice.New(middleware.WithAuth).ThenFunc(api.GetRepositoriesList)).Methods("GET")
// API not requiring authenitcation
r.Handle("/api/{owner}/{repo}/queue", alice.New().ThenFunc(api.GetQueue)).Methods("GET")
port := os.Getenv("PORT")
if port == "" {
// default port
port = "8089"
}
log.Print("INFO: Started running on http://127.0.0.1:" + port + "\n")
log.Fatal(http.ListenAndServe(":"+port, ForceSsl(r)))
}