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

@ -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
}