Reorganaze modules, add auth processing.

This commit is contained in:
2026-03-13 00:07:23 +03:00
parent 95b1b867db
commit 970e64745b
14 changed files with 220 additions and 97 deletions

View File

@@ -11,7 +11,7 @@ import (
"github.com/prometheus/client_golang/prometheus/promhttp"
"go.uber.org/fx"
"payouts/internal/api/payment"
"payouts/internal/api/payout"
"payouts/internal/api/user"
"payouts/internal/api/version"
appConfig "payouts/internal/config"
@@ -21,7 +21,7 @@ import (
// Module is a fx module
var Module = fx.Options(
user.Module,
payment.Module,
payout.Module,
version.Module,
monitoring.Module,
@@ -37,9 +37,9 @@ type Params struct {
AppConfig *appConfig.App
PaymentHandler payment.Handler
UserHandler user.Handler
Version version.Handler
PayoutHandler payout.Handler
UserHandler user.Handler
Version version.Handler
Metrics monitoring.Metrics
}
@@ -66,9 +66,9 @@ func RegisterRoutes(p Params, lc fx.Lifecycle) {
userRouter.HandleFunc(user.RegisterRoute, p.UserHandler.UserRegister).Methods(http.MethodPost)
userRouter.HandleFunc(user.LoginRoute, p.UserHandler.UserLogin).Methods(http.MethodPost)
paymentRouter := apiRouter.PathPrefix(payment.BaseRoute).Subrouter()
paymentRouter.HandleFunc(payment.CreateRoute, p.PaymentHandler.PaymentCreate).Methods(http.MethodPost)
paymentRouter.HandleFunc(payment.CallbackRoute, p.PaymentHandler.PaymentCallback).Methods(http.MethodPost)
payoutRouter := apiRouter.PathPrefix(payout.BaseRoute).Subrouter()
payoutRouter.HandleFunc(payout.CreateRoute, p.PayoutHandler.PayoutCreate).Methods(http.MethodPost)
payoutRouter.HandleFunc(payout.CallbackRoute, p.PayoutHandler.PayoutCallback).Methods(http.MethodPost)
// collect api metrics
apiRouter.Use(p.Metrics.GetMiddleware())

View File

@@ -1,17 +0,0 @@
package payment
import (
"net/http"
"go.uber.org/fx"
)
var Module = fx.Options(
fx.Provide(NewPaymentHandler),
)
type Handler interface {
GetSbpBanks(http.ResponseWriter, *http.Request)
PaymentCreate(http.ResponseWriter, *http.Request)
PaymentCallback(http.ResponseWriter, *http.Request)
}

View File

@@ -1,54 +0,0 @@
package payment
import (
"net/http"
"go.uber.org/fx"
"payouts/internal/config"
"payouts/internal/service/cache"
"payouts/internal/service/database"
)
const (
BaseRoute = "/payment"
CreateRoute = "/create"
CallbackRoute = "/callback"
BanksRoute = "/sbp/banks"
)
type paymentHandler struct {
dbService database.Service
cacheService cache.Service
}
// Params represents the module input params
type Params struct {
fx.In
AppConfig *config.App
DbService database.Service
CacheService cache.Service
}
func NewPaymentHandler(p Params) (Handler, error) {
return &paymentHandler{
dbService: p.DbService,
cacheService: p.CacheService,
}, nil
}
// GetSbpBanks implements [Handler].
func (p *paymentHandler) GetSbpBanks(http.ResponseWriter, *http.Request) {
panic("unimplemented")
}
// PaymentCreate implements [Handler].
func (p *paymentHandler) PaymentCreate(http.ResponseWriter, *http.Request) {
panic("unimplemented")
}
// PaymentCallback implements [Handler].
func (p *paymentHandler) PaymentCallback(http.ResponseWriter, *http.Request) {
panic("unimplemented")
}

View File

@@ -0,0 +1,17 @@
package payout
import (
"net/http"
"go.uber.org/fx"
)
var Module = fx.Options(
fx.Provide(NewPayoutHandler),
)
type Handler interface {
GetSbpBanks(http.ResponseWriter, *http.Request)
PayoutCreate(http.ResponseWriter, *http.Request)
PayoutCallback(http.ResponseWriter, *http.Request)
}

View File

@@ -0,0 +1,91 @@
package payout
import (
"errors"
"net/http"
"regexp"
"go.uber.org/fx"
"payouts/internal/config"
"payouts/internal/service/cache"
"payouts/internal/service/database"
"payouts/internal/service/database/orm"
"payouts/internal/service/yookassa"
)
const (
BaseRoute = "/payment"
CreateRoute = "/create"
CallbackRoute = "/callback"
BanksRoute = "/sbp/banks"
)
var authHeaderRe = regexp.MustCompile(`^Bearer\s+(.*)$`)
type payoutHandler struct {
dbService database.Service
cacheService cache.Service
yooKassa yookassa.Service
}
// Params represents the module input params
type Params struct {
fx.In
AppConfig *config.App
DbService database.Service
YooKassa yookassa.Service
CacheService cache.Service
}
func NewPayoutHandler(p Params) (Handler, error) {
return &payoutHandler{
dbService: p.DbService,
cacheService: p.CacheService,
yooKassa: p.YooKassa,
}, nil
}
func (p *payoutHandler) getSession(r *http.Request) (*orm.User, error) {
authHeaderValue := r.Header.Get("Authorization")
if len(authHeaderValue) == 0 {
return nil, errors.New("no valid auth header")
}
matches := authHeaderRe.FindStringSubmatch(authHeaderValue)
if matches == nil {
return nil, errors.New("no valid auth header")
}
sessionId := matches[1]
userSession, err := p.cacheService.GetSession(sessionId)
if err != nil {
return nil, errors.New("session not found")
}
return &userSession, nil
}
// GetSbpBanks implements [Handler].
func (p *payoutHandler) GetSbpBanks(w http.ResponseWriter, r *http.Request) {
panic("unimplemented")
}
// PaymentCreate implements [Handler].
func (p *payoutHandler) PayoutCreate(w http.ResponseWriter, r *http.Request) {
errResponse := func(message string, err error, status int) {
http.Error(w, errors.Join(errors.New(message), err).Error(), status)
}
_, err := p.getSession(r)
if err != nil {
errResponse("unautiorized", err, http.StatusUnauthorized)
}
panic("unimplemented")
}
// PaymentCallback implements [Handler].
func (p *payoutHandler) PayoutCallback(http.ResponseWriter, *http.Request) {
}