generated from WildEgor/e-shop-fiber-microservice-boilerplate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
public_router.go
108 lines (94 loc) · 2.79 KB
/
public_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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
package router
import (
"github.com/WildEgor/e-shop-auth/internal/configs"
hch "github.com/WildEgor/e-shop-auth/internal/handlers/health_check"
login_handler "github.com/WildEgor/e-shop-auth/internal/handlers/login"
logout_handler "github.com/WildEgor/e-shop-auth/internal/handlers/logout"
otp_generate_handler "github.com/WildEgor/e-shop-auth/internal/handlers/otp-generate"
otp_login_handler "github.com/WildEgor/e-shop-auth/internal/handlers/otp-login"
rch "github.com/WildEgor/e-shop-auth/internal/handlers/ready_check"
reg_handler "github.com/WildEgor/e-shop-auth/internal/handlers/reg"
auth_middleware "github.com/WildEgor/e-shop-auth/internal/middlewares/auth"
"github.com/WildEgor/e-shop-auth/internal/repositories"
"github.com/WildEgor/e-shop-auth/internal/services"
"github.com/gofiber/fiber/v3"
"github.com/gofiber/fiber/v3/middleware/healthcheck"
"github.com/gofiber/fiber/v3/middleware/limiter"
"log/slog"
)
type PublicRouter struct {
rc *reg_handler.RegHandler
lo *login_handler.LoginHandler
lt *logout_handler.LogoutHandler
og *otp_generate_handler.OTPGenHandler
lot *otp_login_handler.OTPLoginHandler
hch *hch.HealthCheckHandler
rch *rch.ReadyCheckHandler
ur *repositories.UserRepository
tr *repositories.TokensRepository
jwt *services.JWTAuthenticator
jwtc *configs.JWTConfig
}
func NewPublicRouter(
rc *reg_handler.RegHandler,
lo *login_handler.LoginHandler,
lt *logout_handler.LogoutHandler,
og *otp_generate_handler.OTPGenHandler,
lot *otp_login_handler.OTPLoginHandler,
hch *hch.HealthCheckHandler,
rch *rch.ReadyCheckHandler,
ur *repositories.UserRepository,
tr *repositories.TokensRepository,
jwt *services.JWTAuthenticator,
jwtc *configs.JWTConfig,
) *PublicRouter {
return &PublicRouter{
rc,
lo,
lt,
og,
lot,
hch,
rch,
ur,
tr,
jwt,
jwtc,
}
}
func (r *PublicRouter) Setup(app *fiber.App) {
api := app.Group("/api", limiter.New(limiter.Config{
Max: 10,
SkipSuccessfulRequests: true,
}))
v1 := api.Group("/v1")
am := auth_middleware.NewAuthMiddleware(auth_middleware.AuthMiddlewareConfig{
UR: r.ur,
JWT: r.jwt,
})
v1.Post("/auth/reg", r.rc.Handle)
v1.Post("/auth/login", r.lo.Handle)
v1.Post("/auth/logout", am, r.lt.Handle)
v1.Post("/auth/otp/gen", r.og.Handle)
v1.Post("/auth/otp/login", r.lot.Handle)
v1.Get("/livez", healthcheck.NewHealthChecker(healthcheck.Config{
Probe: func(ctx fiber.Ctx) bool {
if err := r.hch.Handle(ctx); err != nil {
slog.Error("error not healthy")
return false
}
slog.Debug("is healthy")
return true
},
}))
v1.Get("/readyz", healthcheck.NewHealthChecker(healthcheck.Config{
Probe: func(ctx fiber.Ctx) bool {
if err := r.rch.Handle(ctx); err != nil {
slog.Error("error not ready")
return false
}
slog.Debug("is ready")
return true
},
}))
}