-
-
Notifications
You must be signed in to change notification settings - Fork 229
/
Copy pathapi.go
58 lines (44 loc) · 1.1 KB
/
api.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 admin ties together administration resources and handlers.
package admin
import (
"net/http"
"github.com/sirupsen/logrus"
"github.com/uptrace/bun"
"github.com/go-chi/chi/v5"
"github.com/dhax/go-base/auth/authorize"
"github.com/dhax/go-base/database"
"github.com/dhax/go-base/logging"
)
const (
roleAdmin = "admin"
)
type ctxKey int
const (
ctxAccount ctxKey = iota
)
// API provides admin application resources and handlers.
type API struct {
Accounts *AccountResource
}
// NewAPI configures and returns admin application API.
func NewAPI(db *bun.DB) (*API, error) {
accountStore := database.NewAdmAccountStore(db)
accounts := NewAccountResource(accountStore)
api := &API{
Accounts: accounts,
}
return api, nil
}
// Router provides admin application routes.
func (a *API) Router() *chi.Mux {
r := chi.NewRouter()
r.Use(authorize.RequiresRole(roleAdmin))
r.Get("/", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("Hello Admin"))
})
r.Mount("/accounts", a.Accounts.router())
return r
}
func log(r *http.Request) logrus.FieldLogger {
return logging.GetLogEntry(r)
}