/
routes.go
58 lines (45 loc) · 1.75 KB
/
routes.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
package comms
import (
"net/http"
"github.com/atulanand206/find/server/core/db"
"github.com/atulanand206/find/server/core/services"
"github.com/atulanand206/go-mongo"
net "github.com/atulanand206/go-network"
)
var (
Db db.DB
CommsHub *Hub
Controller services.Service
)
// Add handlers and interceptors to the endpoints.
func Routes(mongoClientId string, database string) *http.ServeMux {
mongo.ConfigureMongoClient(mongoClientId)
db.Database = database
// Interceptor chain for attaching to the requests.
chain := net.MiddlewareChain{
net.ApplicationJsonInterceptor(),
// net.AuthenticationInterceptor(),
}
// Interceptor chain with only PUT method.
getChain := chain.Add(net.CorsInterceptor(http.MethodGet))
putChain := chain.Add(net.CorsInterceptor(http.MethodPut))
// Interceptor chain with only POST method.
postChain := chain.Add(net.CorsInterceptor(http.MethodPost))
Db = db.NewDb()
Controller = services.Init(Db)
PermissionHandler := PermissionHandler{crud: db.PermissionCrud{}}
MatchHandler := MatchHandler{matchService: Controller.MatchService}
router := http.NewServeMux()
router.HandleFunc("/quizzes/active", postChain.Handler(MatchHandler.HandlerActiveQuizzes))
router.HandleFunc("/permission/create", postChain.Handler(PermissionHandler.HandlerCreatePermission))
router.HandleFunc("/permissions", getChain.Handler(PermissionHandler.HandlerFindPermissions))
router.HandleFunc("/question/add", postChain.Handler(HandlerAddQuestion))
router.HandleFunc("/questions/seed", putChain.Handler(HandlerSeedQuestions))
// Register the websocket connection hub.
CommsHub = NewHub(Controller)
go CommsHub.Run()
router.HandleFunc("/ws", func(w http.ResponseWriter, r *http.Request) {
ServeWebsocket(CommsHub, w, r)
})
return router
}