added: app modules register
This commit is contained in:
parent
5ffb6dde46
commit
7e5010ac78
5 changed files with 85 additions and 7 deletions
38
cmd/main.go
38
cmd/main.go
|
|
@ -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,
|
||||
ApiPrefix: c.AppConf.ApiPrefix,
|
||||
RouterHandler: routerHandler,
|
||||
Modules: modules,
|
||||
})
|
||||
|
||||
err = appl.Run(ctx)
|
||||
|
|
|
|||
|
|
@ -11,6 +11,8 @@ import (
|
|||
|
||||
type App struct {
|
||||
address string
|
||||
apiPrefix string
|
||||
modules []interfaces.Module
|
||||
routerHandler interfaces.Router
|
||||
router *gin.Engine
|
||||
}
|
||||
|
|
@ -18,16 +20,31 @@ type App struct {
|
|||
type Deps struct {
|
||||
Host string
|
||||
Port string
|
||||
Router interfaces.Router
|
||||
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
|
||||
}
|
||||
|
|
|
|||
14
internal/interfaces/app.go
Normal file
14
internal/interfaces/app.go
Normal 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
|
||||
}
|
||||
|
|
@ -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
|
||||
}
|
||||
|
|
|
|||
6
internal/shared/routes.go
Normal file
6
internal/shared/routes.go
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
package shared
|
||||
|
||||
type ExcludeRoute struct {
|
||||
Route string
|
||||
Method string
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue