added: app modules register

This commit is contained in:
nquidox 2025-07-06 22:24:06 +03:00
parent 5ffb6dde46
commit 7e5010ac78
5 changed files with 85 additions and 7 deletions

View file

@ -4,11 +4,22 @@ import (
"context"
log "github.com/sirupsen/logrus"
"merch-parser-api/config"
_ "merch-parser-api/docs" //for swagger
"merch-parser-api/internal/api/user"
"merch-parser-api/internal/app"
"merch-parser-api/internal/interfaces"
"merch-parser-api/internal/router"
"merch-parser-api/pkg/db"
"merch-parser-api/pkg/utils"
)
// @Title Merch Parser
// @BasePath /api/v2
// @Version 2.0.0-alpha
// @SecurityDefinitions.apikey BearerAuth
// @In header
// @Name Authorization
// @Description Введите "Bearer {your_token}" для аутентификации
func main() {
//setup config
//c := config.NewConfig()
@ -27,11 +38,36 @@ func main() {
GinMode: c.AppConf.GinMode,
})
//base providers
//jwtProv := token.NewJWT(token.Deps{
// SecretKey: c.JWTConf.Secret,
// Issuer: c.JWTConf.Issuer,
// AccessExpire: c.JWTConf.AccessExpire,
// RefreshExpire: c.JWTConf.RefreshExpire,
//})
utilsProv := utils.NewUtils()
//deps providers
//register app modules
users := user.NewHandler(user.Deps{
DB: database,
Utils: utilsProv,
})
//collect modules
modules := []interfaces.Module{
users,
}
//keep last
appl := app.NewApp(app.Deps{
Host: c.AppConf.Host,
Port: c.AppConf.Port,
Router: routerHandler,
Host: c.AppConf.Host,
Port: c.AppConf.Port,
ApiPrefix: c.AppConf.ApiPrefix,
RouterHandler: routerHandler,
Modules: modules,
})
err = appl.Run(ctx)

View file

@ -11,23 +11,40 @@ import (
type App struct {
address string
apiPrefix string
modules []interfaces.Module
routerHandler interfaces.Router
router *gin.Engine
}
type Deps struct {
Host string
Port string
Router interfaces.Router
Host string
Port string
ApiPrefix string
Modules []interfaces.Module
RouterHandler interfaces.Router
}
func NewApp(deps Deps) *App {
app := &App{
address: deps.Host + ":" + deps.Port,
routerHandler: deps.Router,
apiPrefix: deps.ApiPrefix,
routerHandler: deps.RouterHandler,
modules: deps.Modules,
}
app.router = app.routerHandler.Set()
apiRoutes := app.router.Group(app.apiPrefix)
apiRoutes.GET("/", func(c *gin.Context) {
c.String(http.StatusOK, "API is ready")
})
for _, m := range app.modules {
if hasRoutes, ok := m.(interfaces.ModuleRoutes); ok {
hasRoutes.RegisterRoutes(apiRoutes)
}
}
return app
}

View file

@ -0,0 +1,14 @@
package interfaces
import (
"github.com/gin-gonic/gin"
"merch-parser-api/internal/shared"
)
type Module interface {
}
type ModuleRoutes interface {
RegisterRoutes(r *gin.RouterGroup)
ExcludeRoutes() []shared.ExcludeRoute
}

View file

@ -3,6 +3,8 @@ package router
import (
"github.com/gin-gonic/gin"
log "github.com/sirupsen/logrus"
swaggerFiles "github.com/swaggo/files"
ginSwagger "github.com/swaggo/gin-swagger"
"merch-parser-api/internal/interfaces"
"net/http"
)
@ -44,5 +46,8 @@ func (r *router) Set() *gin.Engine {
//version
r.engine.GET("/version", func(c *gin.Context) { c.JSON(200, gin.H{"version": "2.0.0-alpha"}) })
//swagger
r.engine.GET("/swagger/*any", ginSwagger.WrapHandler(swaggerFiles.Handler))
return r.engine
}

View file

@ -0,0 +1,6 @@
package shared
type ExcludeRoute struct {
Route string
Method string
}