-
Notifications
You must be signed in to change notification settings - Fork 21
/
router.go
78 lines (64 loc) · 2.66 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
package server
import (
"net/http"
_ "github.com/anonychun/go-blog-api/docs"
"github.com/anonychun/go-blog-api/internal/app/handler"
"github.com/anonychun/go-blog-api/internal/app/repository"
"github.com/anonychun/go-blog-api/internal/app/service"
"github.com/anonychun/go-blog-api/internal/config"
"github.com/anonychun/go-blog-api/internal/db/postgres"
"github.com/anonychun/go-blog-api/internal/db/redis"
"github.com/anonychun/go-blog-api/internal/security/middleware"
"github.com/go-chi/chi"
chimiddleware "github.com/go-chi/chi/middleware"
"github.com/go-chi/cors"
"github.com/go-chi/httprate"
httpSwagger "github.com/swaggo/http-swagger"
)
// @title Go Blog API
// @version 1.0
// @description Implementing back-end services for blog application
// @BasePath /v1
// @securityDefinitions.apikey ApiKeyAuth
// @in header
// @name X-API-Key
func NewRouter(postgresClient postgres.Client, redisClient redis.Client) *chi.Mux {
router := chi.NewRouter()
router.Use(httprate.LimitByIP(
config.Cfg().HttpRateLimitRequest,
config.Cfg().HttpRateLimitTime,
))
router.Use(cors.AllowAll().Handler)
router.Use(chimiddleware.Logger)
router.Use(chimiddleware.Recoverer)
accountRepository := repository.NewAccountRepository(postgresClient, redisClient)
postRepository := repository.NewPostRepository(postgresClient, redisClient)
authService := service.NewAuthService(accountRepository)
accountService := service.NewAccountService(accountRepository)
postService := service.NewPostService(postRepository)
authHandler := handler.NewAuthHandler(authService)
accountHandler := handler.NewAccountHandler(accountService)
postHandler := handler.NewPostHandler(postService)
router.Options("/*", func(w http.ResponseWriter, r *http.Request) {})
api := router.Route("/v1", func(router chi.Router) {})
api.Route("/accounts", func(r chi.Router) {
r.Post("/auth", authHandler.Login())
r.Post("/", accountHandler.Create())
r.Get("/", accountHandler.List())
r.Get("/{account_id}", accountHandler.Get())
r.With(middleware.JWTVerifier).Put("/{account_id}", accountHandler.Update())
r.With(middleware.JWTVerifier).Put("/{account_id}/password", accountHandler.UpdatePassword())
r.With(middleware.JWTVerifier).Delete("/{account_id}", accountHandler.Delete())
})
api.Route("/posts", func(r chi.Router) {
r.With(middleware.JWTVerifier).Post("/", postHandler.Create())
r.Get("/", postHandler.List())
r.Get("/{post_id}", postHandler.Get())
r.With(middleware.JWTVerifier).Put("/{post_id}", postHandler.Update())
r.With(middleware.JWTVerifier).Delete("/{post_id}", postHandler.Delete())
})
api.Get("/swagger/*", httpSwagger.Handler(
httpSwagger.URL("doc.json"),
))
return router
}