-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
98 lines (77 loc) · 2.56 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
package main
import (
"flag"
"fmt"
"log"
"net/http"
"github.com/MyHomeworkSpace/api-server/schools"
"github.com/julienschmidt/httprouter"
"github.com/MyHomeworkSpace/api-server/schools/columbia"
"github.com/MyHomeworkSpace/api-server/schools/cornell"
"github.com/MyHomeworkSpace/api-server/schools/dalton"
"github.com/MyHomeworkSpace/api-server/schools/mit"
"github.com/MyHomeworkSpace/api-server/api"
"github.com/MyHomeworkSpace/api-server/auth"
"github.com/MyHomeworkSpace/api-server/config"
"github.com/MyHomeworkSpace/api-server/data"
"github.com/MyHomeworkSpace/api-server/email"
)
type errorResponse struct {
Status string `json:"status"`
Error string `json:"error"`
}
type csrfResponse struct {
Status string `json:"status"`
Token string `json:"token"`
}
func main() {
log.Println("MyHomeworkSpace API Server")
config.Init()
initDatabase()
initRedis()
// initWebauthn()
migrationName := flag.String("migrate", "", "If specified, the API server will run the migration with the given name.")
flag.Parse()
if *migrationName != "" {
migrate(*migrationName)
return
}
email.Init()
api.DB = DB
api.MainRegistry = schools.MainRegistry
api.RedisClient = RedisClient
api.WebAuthnHandler = WebAuthnHandler
auth.DB = DB
auth.RedisClient = RedisClient
data.DB = DB
data.MainRegistry = schools.MainRegistry
data.RedisClient = RedisClient
schools.MainRegistry.Register(dalton.CreateSchool())
schools.MainRegistry.Register(mit.CreateSchool())
schools.MainRegistry.Register(cornell.CreateSchool())
schools.MainRegistry.Register(columbia.CreateSchool(false))
schools.MainRegistry.Register(columbia.CreateSchool(true))
router := httprouter.New()
router.GET("/", func(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
w.Write([]byte("MyHomeworkSpace API Server"))
})
router.ServeFiles("/api_tester/*filepath", http.Dir("api_tester/"))
api.Init(router) // API init delayed because router must be started first
http.Handle("/", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// if it's a preflight, handle it here
if r.Method == "OPTIONS" {
w.Header().Set("Access-Control-Allow-Credentials", "false")
w.Header().Set("Access-Control-Allow-Origin", "*")
w.Header().Set("Access-Control-Allow-Headers", "authorization")
w.WriteHeader(http.StatusOK)
return
}
// otherwise, pass it through
router.ServeHTTP(w, r)
}))
log.Printf("Listening on port %d", config.GetCurrent().Server.Port)
err := http.ListenAndServe(fmt.Sprintf(":%d", config.GetCurrent().Server.Port), nil)
if err != nil {
log.Fatalln(err)
}
}