-
Notifications
You must be signed in to change notification settings - Fork 0
/
router.go
77 lines (63 loc) · 2.35 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
package main
import (
"database/sql"
"github.com/go-chi/chi"
"github.com/go-chi/chi/middleware"
"github.com/go-chi/httprate"
"github.com/go-chi/render"
"github.com/pranayhere/simple-wallet/api"
middleware2 "github.com/pranayhere/simple-wallet/middleware"
"github.com/pranayhere/simple-wallet/pkg/constant"
"github.com/pranayhere/simple-wallet/service"
"github.com/pranayhere/simple-wallet/store"
"github.com/pranayhere/simple-wallet/token"
"net/http"
"time"
)
func createRouter() *chi.Mux {
r := chi.NewRouter()
r.Use(middleware.RequestID)
r.Use(middleware.RealIP)
r.Use(middleware.Recoverer)
r.Use(middleware.Logger)
r.Use(httprate.LimitByIP(100, 1*time.Minute))
return r
}
func initRoutes(db *sql.DB, r *chi.Mux) *chi.Mux {
currencyRepo := store.NewCurrencyRepo(db)
currencySvc := service.NewCurrencyService(currencyRepo)
currencyApi := api.NewCurrencyResource(currencySvc)
tokenMaker, err := token.NewJWTMaker(constant.SymmetricKey)
if err != nil {
panic(err)
}
userRepo := store.NewUserRepo(db)
userSvc := service.NewUserService(userRepo, tokenMaker)
userApi := api.NewUserResource(userSvc)
transferRepo := store.NewTransferRepo(db)
entryRepo := store.NewEntryRepo(db)
walletRepo := store.NewWalletRepo(db, transferRepo, entryRepo)
bankAccountRepo := store.NewBankAccountRepo(db, walletRepo, userRepo)
bankAcctSvc := service.NewBankAccountService(bankAccountRepo, currencySvc)
bankAcctApi := api.NewBankAccountResource(bankAcctSvc)
walletSvc := service.NewWalletService(walletRepo)
walletApi := api.NewWalletResource(walletSvc)
paymentRequestRepo := store.NewPaymentRequestRepo(db)
paymentRequestSvc := service.NewPaymentRequestService(paymentRequestRepo, walletSvc)
paymentRequestApi := api.NewPaymentRequestResource(paymentRequestSvc)
// Routes
// public
userApi.RegisterRoutes(r.With(httprate.LimitByIP(10, 1*time.Minute)))
// authorized
r.Group(func(r chi.Router) {
r.Use(middleware2.Auth(tokenMaker))
bankAcctApi.RegisterRoutes(r)
currencyApi.RegisterRoutes(r)
walletApi.RegisterRoutes(r)
paymentRequestApi.RegisterRoutes(r)
})
r.Get("/health", func(w http.ResponseWriter, r *http.Request) {
render.JSON(w, r, "ok")
})
return r
}