-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrouter.go
83 lines (64 loc) · 1.91 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 app
import (
"net/http"
"github.com/imantung/boilerplate-go-backend/internal/app/infra/auth"
"github.com/imantung/boilerplate-go-backend/internal/app/infra/logger"
"github.com/imantung/boilerplate-go-backend/internal/app/service"
"github.com/imantung/boilerplate-go-backend/internal/generated/oapi"
"github.com/labstack/echo/v4"
"github.com/labstack/echo/v4/middleware"
"go.uber.org/dig"
_ "expvar" // NOTE: enable `/debug/vars` endpoint
_ "net/http/pprof" // NOTE: enable `/debug/pprof` endpoint
)
type Router struct {
dig.In
Health AppHealth
Oauth *auth.OAuthHandler
BasicAuthValidator middleware.BasicAuthValidator
service.EmployeeSvc
service.ClockSvc
}
var _ oapi.StrictServerInterface = (*Router)(nil)
func (r *Router) SetRoute(e *echo.Echo) {
e.HTTPErrorHandler = r.customErrorHandler
e.Pre(
middleware.RequestID(),
)
e.Use(
middleware.CORS(),
middleware.Recover(),
middleware.Secure(),
logger.HTTPRequest(),
)
// NOTE: register open-api endpoints
oapi.RegisterHandlers(
e.Group("api"),
oapi.NewStrictHandler(r, []oapi.StrictMiddlewareFunc{
r.Oauth.ValidateToken,
}))
e.Any("/oauth/authorize", r.Oauth.HandleAuthorizeRequest)
e.Any("/oauth/token", r.Oauth.HandleTokenRequest)
e.File("/swagger/api-spec.yaml", "api/api-spec.yaml")
e.Static("/swagger/ui", "api/swagger-ui")
basicAuth := middleware.BasicAuth(r.BasicAuthValidator)
e.Any("/health", r.Health.Handle, basicAuth)
e.GET("/debug/*/*", echo.WrapHandler(http.DefaultServeMux), basicAuth)
}
func (r *Router) customErrorHandler(err error, c echo.Context) {
code := http.StatusInternalServerError
message := "unknown error"
if he, ok := err.(*echo.HTTPError); ok {
code = he.Code
message, _ = he.Message.(string)
} else {
message = err.Error()
}
c.Logger().Error(err)
resp := oapi.Error{
ErrorMessage: message,
}
if err := c.JSON(code, resp); err != nil {
c.Logger().Error(err)
}
}