-
Notifications
You must be signed in to change notification settings - Fork 2
/
routes.go
58 lines (45 loc) · 2.38 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 handlers
import (
"log"
"net/http"
"github.com/events-app/events-api2/internal/mid"
"github.com/events-app/events-api2/internal/platform/auth"
"github.com/events-app/events-api2/internal/platform/web"
"github.com/jmoiron/sqlx"
)
// API constructs an http.Handler with all application routes defined.
func API(db *sqlx.DB, log *log.Logger, authenticator *auth.Authenticator) http.Handler {
// Construct the web.App which holds all routes as well as common Middleware.
app := web.NewApp(log, mid.Logger(log), mid.Errors(log), mid.Metrics(), mid.Cors("*"))
{
// Register health check handler. This route is not authenticated.
c := Check{db: db}
app.Handle(http.MethodGet, "/v1/health", c.Health)
}
{
// Register user handlers.
u := Users{db: db, authenticator: authenticator}
// The token route can't be authenticated because they need this route to
// get the token in the first place.
app.Handle(http.MethodGet, "/v1/users/token", u.Token)
}
{
// Register Card handlers. Ensure all routes are authenticated.
c := Cards{db: db, log: log}
app.Handle(http.MethodGet, "/v1/cards", c.List, mid.Authenticate(authenticator))
app.Handle(http.MethodGet, "/v1/cards/{id}", c.Retrieve, mid.Authenticate(authenticator))
app.Handle(http.MethodPost, "/v1/cards", c.Create, mid.Authenticate(authenticator), mid.HasRole(auth.RoleAdmin))
app.Handle(http.MethodPut, "/v1/cards/{id}", c.Update, mid.Authenticate(authenticator), mid.HasRole(auth.RoleAdmin))
app.Handle(http.MethodDelete, "/v1/cards/{id}", c.Delete, mid.Authenticate(authenticator), mid.HasRole(auth.RoleAdmin))
// app.Handle(http.MethodPost, "/v1/cards/{id}/sales", c.AddSale, mid.Authenticate(authenticator), mid.HasRole(auth.RoleAdmin))
// app.Handle(http.MethodGet, "/v1/cards/{id}/sales", c.ListSales, mid.Authenticate(authenticator))
// Register Card handlers. Ensure all routes are authenticated.
m := Menus{db: db, log: log}
app.Handle(http.MethodGet, "/v1/menus", m.List, mid.Authenticate(authenticator))
app.Handle(http.MethodGet, "/v1/menus/{id}", m.Retrieve, mid.Authenticate(authenticator))
app.Handle(http.MethodPost, "/v1/menus", m.Create, mid.Authenticate(authenticator))
app.Handle(http.MethodPut, "/v1/menus/{id}", m.Update, mid.Authenticate(authenticator))
app.Handle(http.MethodDelete, "/v1/menus/{id}", m.Delete, mid.Authenticate(authenticator), mid.HasRole(auth.RoleAdmin))
}
return app
}