-
Notifications
You must be signed in to change notification settings - Fork 3
/
server.go
138 lines (115 loc) · 3.73 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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
package server
import (
"encoding/json"
"log"
"net/http"
"time"
"github.com/go-chi/chi"
"github.com/go-chi/chi/middleware"
"github.com/go-chi/cors"
"github.com/nokia-wroclaw/innovativeproject-togeather/backend/pkg/core"
)
type Server struct {
// smth like config goes here
router chi.Router
}
func New(
restaurantService core.RestaurantService,
lobbyService core.LobbyService,
userService core.UserService,
) *Server {
s := &Server{}
r := chi.NewRouter()
cors := cors.New(cors.Options{
// AllowedOrigins: []string{"https://foo.com"}, // Use this to allow specific origin hosts
AllowedOrigins: []string{"*"},
// AllowOriginFunc: func(r *http.Request, origin string) bool { return true },
AllowedMethods: []string{"GET", "POST", "PUT", "DELETE", "OPTIONS"},
AllowedHeaders: []string{"Accept", "Authorization", "Content-Type", "Content-Length", "X-CSRF-Token"},
ExposedHeaders: []string{"Link"},
AllowCredentials: true,
MaxAge: 300, // Maximum value not ignored by any of major browsers
})
r.Use(
middleware.RequestID,
middleware.Logger,
middleware.StripSlashes,
middleware.Timeout(10*time.Second),
cors.Handler,
)
// add handlers
pingHandler := pingHandler{}
restaurantHandler := restaurantHandler{restaurantService: restaurantService}
lobbyHandler := lobbyHandler{lobbyService: lobbyService}
userHandler := userHandler{userService: userService}
lobbyMiddleware := lobbyMiddleware{lobbyService: lobbyService, userService: userService}
authHandler := authHandler{userService: userService}
r.Route("/api", func(r chi.Router) {
r.Route("/restaurants", func(r chi.Router) {
r.Get("/", restaurantHandler.list)
r.Route("/{restaurantID}", func(r chi.Router){
r.Get("/", restaurantHandler.get)
r.Get("/menu", restaurantHandler.menu)
})
})
r.Route("/lobbies", func(r chi.Router) {
r.Get("/", lobbyHandler.list)
r.Post("/", authMiddleware(lobbyHandler.create, lobbyMiddleware))
r.Route("/{lobbyID}", func(r chi.Router){
//r.Put("/", authMiddleware(lobbyHandler.edit, lobbyMiddleware))
r.Post("/", authMiddleware(lobbyHandler.join, lobbyMiddleware))
r.Get("/", authMiddleware(lobbyHandler.get, lobbyMiddleware))
r.Route("/order", func(r chi.Router){
r.Post("/", authMiddleware(lobbyHandler.addToCart, lobbyMiddleware))
r.Delete("/", authMiddleware(lobbyHandler.delFromCart, lobbyMiddleware))
r.Get("/", authMiddleware(lobbyHandler.getCart, lobbyMiddleware))
})
})
})
r.Route("/users", func(r chi.Router) {
r.Get("/", userHandler.list)
})
r.Route("/user", func(r chi.Router) {
r.Get("/", userHandler.get)
})
r.Route("/auth", func (r chi.Router) {
r.Post("/register", authHandler.register)
r.Post("/login", authHandler.login)
r.Delete("/logout", authMiddleware(authHandler.logout, lobbyMiddleware))
})
r.Route("/ping", func(r chi.Router) {
r.Get("/", authMiddleware(pingHandler.ping, lobbyMiddleware))
})
})
s.router = r
return s
}
func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
s.router.ServeHTTP(w, r)
}
func respondJSON(w http.ResponseWriter, status int, payload interface{}) {
jsonData, err := json.Marshal(payload)
if err != nil {
log.Println(err)
return
}
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(status)
_, _ = w.Write(jsonData)
}
func respondError(w http.ResponseWriter, status int, err error) {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(status)
_ = json.NewEncoder(w).Encode(map[string]string{
"error": err.Error(),
})
}
func addCookie(w http.ResponseWriter, name string, value string, maxAge int, path string) {
cookie := http.Cookie{
Name: name,
Value: value,
MaxAge: maxAge,
Path: path,
}
http.SetCookie(w, &cookie)
}