-
Notifications
You must be signed in to change notification settings - Fork 0
/
router.go
44 lines (34 loc) · 972 Bytes
/
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
package private
import (
"net/http"
"github.com/gardod/shorty-api/internal/driver/http/response"
"github.com/gardod/shorty-api/internal/handler/private/link"
mw "github.com/gardod/shorty-api/internal/middleware"
"github.com/go-chi/chi"
"github.com/go-chi/chi/middleware"
"github.com/spf13/viper"
)
func GetRouter() http.Handler {
r := chi.NewRouter()
r.Use(
middleware.StripSlashes,
middleware.RealIP,
middleware.RequestID,
mw.Logger,
mw.RequestLogger,
mw.Database,
mw.Cache,
mw.Recoverer(response.JSON),
)
r.NotFound(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
response.JSON(w, response.ErrNotFound, http.StatusNotFound)
}))
r.MethodNotAllowed(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
response.JSON(w, response.ErrMethodNotAllowed, http.StatusMethodNotAllowed)
}))
if viper.GetBool("debug") {
r.Mount("/debug", mw.Profiler())
}
r.Mount("/link", link.GetRouter())
return r
}