-
Notifications
You must be signed in to change notification settings - Fork 0
/
router.go
49 lines (42 loc) · 1.28 KB
/
router.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
package handlers
import (
"net/http"
"time"
"github.com/redestro/zeronet/server/pkg/middlewares"
"github.com/redestro/zeronet/server/pkg/sessions"
"github.com/go-chi/chi"
"github.com/go-chi/chi/middleware"
"github.com/go-chi/cors"
)
//InitRoutes exports the router
func InitRoutes(db *sessions.Database) *chi.Mux {
r := chi.NewRouter()
cors := cors.New(cors.Options{
AllowedMethods: []string{"GET", "POST", "PUT", "DELETE"},
AllowedHeaders: []string{"Origin", "Authorization", "Access-Control-Allow-Origin", "Content-Type", "x-access-token"},
ExposedHeaders: []string{"Content-Length", "Access-Control-Allow-Origin"},
AllowCredentials: true,
})
// middlewares
r.Use(cors.Handler)
r.Use(middleware.RequestID)
r.Use(middleware.Logger)
r.Use(middleware.Recoverer)
r.Use(middleware.Timeout(60 * time.Second))
r.Get("/", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("You have reached zeronet"))
})
r.Route("/start", func(r chi.Router) {
r.Get("/human", StartHuman)
r.Get("/ai", StartAI)
r.Get("/revhuman", StartRevHuman)
r.Get("/human2", StartHuman2)
})
r.Route("/play/{token}", func(r chi.Router) {
r.Use(middlewares.Update)
r.Post("/human", PlayHuman)
r.Post("/ai", PlayAI)
})
r.Delete("/session/{token}", DeleteSession)
return r
}