This repository has been archived by the owner on Sep 28, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.go
77 lines (62 loc) · 1.85 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
// Package main provides ...
package main
import (
"github.com/Pitt-CSC/icarus-backend/auth"
"github.com/Pitt-CSC/icarus-backend/models"
"github.com/Pitt-CSC/icarus-backend/routes"
"github.com/gorilla/mux"
"github.com/jinzhu/gorm"
_ "github.com/mattn/go-sqlite3"
"log"
"net/http"
)
func main() {
////
// Database Connection
//
// See migrate.go for migrations
////
db, err := gorm.Open("sqlite3", "tmp/gorm.db")
if err != nil {
panic(err)
}
db.DB()
// Database Connections for each package
routes.InitializeDBConnection(db)
models.InitializeDBConnection(db)
auth.InitializeDBConnection(db)
////
// Handle routing
////
router := mux.NewRouter()
api := router.PathPrefix("/api").Subrouter()
// Authentiation
oauth := api.Path("/oauth").Subrouter()
oauth.Methods("GET").HandlerFunc(auth.OAuthHandler)
// Session
session := api.PathPrefix("/session").Subrouter()
session.Methods("GET").HandlerFunc(routes.GetAuthenticatedUserHandler)
// Talks
talks := api.Path("/talks").Subrouter()
talks.Methods("GET").HandlerFunc(routes.TalkIndexRoute)
talks.Methods("POST").HandlerFunc(routes.TalkNewRoute)
talk := api.PathPrefix("/talks/{id}").Subrouter()
talk.Methods("GET").HandlerFunc(routes.TalkShowRoute)
talk.Methods("DELETE").HandlerFunc(routes.TalkDeleteRoute)
////
// Add websockets
////
h := NewHub()
go h.run()
router.Handle("/socket/", WsHandler{h: h})
log.Println("Serving at localhost:5000...")
log.Fatal(http.ListenAndServe(":5000", setRequestHeaderMiddleware(router)))
}
func setRequestHeaderMiddleware(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/vnd.api+json")
w.Header().Set("Access-Control-Allow-Origin", "http://localhost:4200")
w.Header().Set("Access-Control-Allow-Credentials", "true")
next.ServeHTTP(w, r)
})
}