-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvangothrone.go
92 lines (79 loc) · 2.56 KB
/
vangothrone.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
package main
import (
"encoding/json"
"fmt"
"log"
"net/http"
"github.com/aelnor/vangothrone/config"
"github.com/aelnor/vangothrone/models"
"github.com/julienschmidt/httprouter"
"github.com/rs/cors"
)
func prettyPrint(v interface{}) {
b, _ := json.MarshalIndent(v, "", " ")
println(string(b))
}
func InitEnvironment() (*config.Env, error) {
db, err := config.InitDatabase()
if err != nil {
return nil, fmt.Errorf("Can't init table: %s", err.Error())
}
if err := models.InitUsersTable(db); err != nil {
return nil, fmt.Errorf("Can't init table 'Users': %s", err.Error())
}
if err := models.InitMatchesTable(db); err != nil {
return nil, fmt.Errorf("Can't init table 'Matches': %s", err.Error())
}
if err := models.InitPredictionsTable(db); err != nil {
return nil, fmt.Errorf("Can't init table 'Predictions': %s", err.Error())
}
if err := models.InitStagesTable(db); err != nil {
return nil, fmt.Errorf("Can't init table 'Stages': %s", err.Error())
}
log.Printf("Database Initialized")
env := &config.Env{
DB: db,
}
return env, nil
}
func teamsHandler(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
w.Header().Set("Content-Type", "application/json")
w.Header().Set("Pragma", "no-cache")
w.Header().Set("Cache-Control", "no-cache,must-revalidate")
w.Header().Set("Access-Control-Allow-Credentials", "true")
w.Header().Set("Access-Control-Allow-Origin", "*")
jsontext, err := json.Marshal(models.Teams)
if err != nil {
fmt.Printf("Can't marshal teams: %v", err)
return
}
fmt.Fprintf(w, string(jsontext))
}
func main() {
env, err := InitEnvironment()
if err != nil {
log.Fatal("Can't init environment: ", err)
}
hh := &HttpHandlers{Env: env}
rtr := httprouter.New()
rtr.GET("/teams", teamsHandler)
rtr.GET("/matches", hh.GetMatches)
rtr.POST("/matches", hh.PostMatches)
rtr.PUT("/predictions", hh.PutPredictions)
rtr.PUT("/matches/:id", hh.PutMatch)
rtr.POST("/login", hh.PostLogin)
rtr.GET("/login", hh.GetLogin)
rtr.GET("/logout", hh.GetLogout)
rtr.GET("/users", hh.GetUsers)
rtr.GET("/stages", hh.GetStages)
rtr.GET("/", hh.GetIndex)
rtr.ServeFiles("/static/*filepath", http.Dir(config.GetStaticPath()+"static/"))
c := cors.New(cors.Options{
AllowedMethods: []string{"GET", "POST", "DELETE", "PUT", "OPTIONS"},
AllowOriginFunc: func(_ string) bool { return true },
AllowedHeaders: []string{"Origin", "Content-Type", "X-Requested-With", "X-Auth-Token", "Accept", "Accept-Language"},
AllowCredentials: true,
})
log.Printf("Preparations finished, serving")
log.Fatal(http.ListenAndServe(":8383", c.Handler(rtr)))
}