-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathrouter.go
83 lines (68 loc) · 2.73 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
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
package nethttp
import (
"net/http"
"time"
"github.com/go-chi/chi/v5"
"github.com/go-chi/chi/v5/middleware"
"github.com/swaggest/openapi-go"
"github.com/swaggest/openapi-go/openapi3"
"github.com/swaggest/rest"
"github.com/swaggest/rest/_examples/task-api/internal/infra/schema"
"github.com/swaggest/rest/_examples/task-api/internal/infra/service"
"github.com/swaggest/rest/_examples/task-api/internal/usecase"
"github.com/swaggest/rest/nethttp"
"github.com/swaggest/rest/web"
swgui "github.com/swaggest/swgui/v5emb"
)
// NewRouter creates HTTP router.
func NewRouter(locator *service.Locator) http.Handler {
s := web.NewService(openapi3.NewReflector())
schema.SetupOpenAPICollector(s.OpenAPICollector)
adminAuth := middleware.BasicAuth("Admin Access", map[string]string{"admin": "admin"})
userAuth := middleware.BasicAuth("User Access", map[string]string{"user": "user"})
s.Wrap(
middleware.NoCache,
middleware.Timeout(time.Second),
)
ff := func(h *nethttp.Handler) {
h.ReqMapping = rest.RequestMapping{rest.ParamInPath: map[string]string{"ID": "id"}}
}
// Unrestricted access.
s.Route("/dev", func(r chi.Router) {
r.Use(nethttp.OpenAPIAnnotationsMiddleware(s.OpenAPICollector, func(oc openapi.OperationContext) error {
oc.SetTags("Dev Mode")
return nil
}))
r.Group(func(r chi.Router) {
r.Method(http.MethodPost, "/tasks", nethttp.NewHandler(usecase.CreateTask(locator),
nethttp.SuccessStatus(http.StatusCreated)))
r.Method(http.MethodPut, "/tasks/{id}", nethttp.NewHandler(usecase.UpdateTask(locator), ff))
r.Method(http.MethodGet, "/tasks/{id}", nethttp.NewHandler(usecase.FindTask(locator), ff))
r.Method(http.MethodGet, "/tasks", nethttp.NewHandler(usecase.FindTasks(locator)))
r.Method(http.MethodDelete, "/tasks/{id}", nethttp.NewHandler(usecase.FinishTask(locator), ff))
})
})
// Endpoints with admin access.
s.Route("/admin", func(r chi.Router) {
r.Group(func(r chi.Router) {
r.Use(nethttp.OpenAPIAnnotationsMiddleware(s.OpenAPICollector, func(oc openapi.OperationContext) error {
oc.SetTags("Admin Mode")
return nil
}))
r.Use(adminAuth, nethttp.HTTPBasicSecurityMiddleware(s.OpenAPICollector, "Admin", "Admin access"))
r.Method(http.MethodPut, "/tasks/{id}", nethttp.NewHandler(usecase.UpdateTask(locator), ff))
})
})
// Endpoints with user access.
s.Route("/user", func(r chi.Router) {
r.Group(func(r chi.Router) {
r.Use(userAuth, nethttp.HTTPBasicSecurityMiddleware(s.OpenAPICollector, "User", "User access"))
r.Method(http.MethodPost, "/tasks", nethttp.NewHandler(usecase.CreateTask(locator),
nethttp.SuccessStatus(http.StatusCreated)))
})
})
// Swagger UI endpoint at /docs.
s.Docs("/docs", swgui.New)
s.Mount("/debug", middleware.Profiler())
return s
}