-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.go
111 lines (95 loc) · 2.4 KB
/
server.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
package main
import (
"fmt"
"net/http"
"os"
log "github.com/sirupsen/logrus"
)
// Middleware class
type Middleware struct {
MiddlewareHandlers [](func(handler http.Handler) http.Handler)
}
// Apply : Create a handler where the core handler is wrapped by middleware handlers
func (mw *Middleware) Apply(
coreHandler func(w http.ResponseWriter, r *http.Request),
) http.Handler {
handler := http.Handler(http.HandlerFunc(coreHandler))
for _, nextHandler := range mw.MiddlewareHandlers {
handler = nextHandler(handler)
}
return handler
}
// ApplyFake :
func (mw Middleware) ApplyFake(
coreHandler func(w http.ResponseWriter, r *http.Request),
) http.Handler {
return http.Handler(http.HandlerFunc(coreHandler))
}
func runTestSequence(testMode bool) {
err := createOrganization("ysc", "[email protected]")
if err != nil {
fmt.Println(err)
}
_, err = createMembersFromCSV("ysc", "./csv/test_john3.csv")
if err != nil {
fmt.Println(err)
return
}
i := 0
for i < 2 {
err = addRound("ysc", fmt.Sprintf("2019-01-02 %d:55:00", i))
if err != nil {
fmt.Println(err)
return
}
// NOTE: Do you want to actually send out emails?
err = runPairingRound("ysc", i, testMode)
if err != nil {
fmt.Println(err)
return
}
i++
}
}
func main() {
args := os.Args
if len(args) == 2 {
if args[1] == "pair" {
// runTestSequence(true)
err := runPairingScheduler(false)
if err != nil {
fmt.Println(err)
}
return
} else if args[1] == "migrate" {
err := migrateToLastRoundWithForPairing()
if err != nil {
fmt.Println(err)
}
return
} else {
fmt.Printf("argument '%s' not recognized", args[1])
return
}
}
mw := Middleware{
MiddlewareHandlers: [](func(handler http.Handler) http.Handler){
GetAuthHandler,
GetCorsHandler,
},
}
serveMux := http.NewServeMux()
serveMux.Handle("/members", mw.Apply(MembersHandler))
serveMux.Handle("/orgs", mw.Apply(GetOrganizationsHandler))
serveMux.Handle("/org", mw.Apply(CreateOrganizationHandler))
serveMux.Handle("/crossmatchtrait", mw.Apply(CrossMatchTraitHandler))
serveMux.Handle("/rounds", mw.Apply(GetRoundsHandler))
serveMux.Handle("/round", mw.Apply(RoundHandler))
serveMux.Handle("/pairs", mw.Apply(GetPairsHandler))
serveMux.Handle("/", http.FileServer(http.Dir("./static")))
port := os.Getenv("PORT")
if port == "" {
port = "8080"
}
log.Fatal(http.ListenAndServe(":"+port, serveMux))
}