-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.go
63 lines (52 loc) · 1.32 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
59
60
61
62
63
package api
import (
"net/http"
"github.com/djordjev/auth/internal/api/middleware"
"github.com/djordjev/auth/internal/domain"
"github.com/djordjev/auth/internal/utils"
"github.com/go-chi/chi/v5"
chimiddleware "github.com/go-chi/chi/v5/middleware"
"golang.org/x/exp/slog"
)
type Api interface {
Mount(point string)
}
type jsonApi struct {
cfg utils.Config
mux *http.ServeMux
subrouter *chi.Mux
domain domain.Domain
logger *slog.Logger
}
func NewApi(cfg utils.Config, mux *http.ServeMux, domain domain.Domain, logger *slog.Logger) *jsonApi {
router := chi.NewMux()
return &jsonApi{
cfg: cfg,
mux: mux,
subrouter: router,
domain: domain,
logger: logger,
}
}
func (a *jsonApi) setupMiddleware() {
r := a.subrouter
r.Use(middleware.Logger(a.logger))
r.Use(chimiddleware.RequestID)
r.Use(chimiddleware.Recoverer)
}
func (a *jsonApi) setupRoutes() {
r := a.subrouter
r.Post("/signup", a.postSignup)
r.Post("/login", a.postLogin)
r.Delete("/delete", a.deleteAccount)
r.Post("/verify", a.postVerifyAccount)
r.Post("/forget", a.postForgetPassword)
r.Post("/passwordreset", a.postVerifyPasswordReset)
r.Get("/session", a.getSession)
r.Post("/logout", a.postLogout)
}
func (a *jsonApi) Mount(point string) {
a.setupMiddleware()
a.setupRoutes()
a.mux.Handle(point, a.subrouter)
}