added: app modules register
This commit is contained in:
parent
5ffb6dde46
commit
7e5010ac78
5 changed files with 85 additions and 7 deletions
42
cmd/main.go
42
cmd/main.go
|
|
@ -4,11 +4,22 @@ import (
|
||||||
"context"
|
"context"
|
||||||
log "github.com/sirupsen/logrus"
|
log "github.com/sirupsen/logrus"
|
||||||
"merch-parser-api/config"
|
"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/app"
|
||||||
|
"merch-parser-api/internal/interfaces"
|
||||||
"merch-parser-api/internal/router"
|
"merch-parser-api/internal/router"
|
||||||
"merch-parser-api/pkg/db"
|
"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() {
|
func main() {
|
||||||
//setup config
|
//setup config
|
||||||
//c := config.NewConfig()
|
//c := config.NewConfig()
|
||||||
|
|
@ -27,11 +38,36 @@ func main() {
|
||||||
GinMode: c.AppConf.GinMode,
|
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
|
//keep last
|
||||||
appl := app.NewApp(app.Deps{
|
appl := app.NewApp(app.Deps{
|
||||||
Host: c.AppConf.Host,
|
Host: c.AppConf.Host,
|
||||||
Port: c.AppConf.Port,
|
Port: c.AppConf.Port,
|
||||||
Router: routerHandler,
|
ApiPrefix: c.AppConf.ApiPrefix,
|
||||||
|
RouterHandler: routerHandler,
|
||||||
|
Modules: modules,
|
||||||
})
|
})
|
||||||
|
|
||||||
err = appl.Run(ctx)
|
err = appl.Run(ctx)
|
||||||
|
|
|
||||||
|
|
@ -11,23 +11,40 @@ import (
|
||||||
|
|
||||||
type App struct {
|
type App struct {
|
||||||
address string
|
address string
|
||||||
|
apiPrefix string
|
||||||
|
modules []interfaces.Module
|
||||||
routerHandler interfaces.Router
|
routerHandler interfaces.Router
|
||||||
router *gin.Engine
|
router *gin.Engine
|
||||||
}
|
}
|
||||||
|
|
||||||
type Deps struct {
|
type Deps struct {
|
||||||
Host string
|
Host string
|
||||||
Port string
|
Port string
|
||||||
Router interfaces.Router
|
ApiPrefix string
|
||||||
|
Modules []interfaces.Module
|
||||||
|
RouterHandler interfaces.Router
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewApp(deps Deps) *App {
|
func NewApp(deps Deps) *App {
|
||||||
app := &App{
|
app := &App{
|
||||||
address: deps.Host + ":" + deps.Port,
|
address: deps.Host + ":" + deps.Port,
|
||||||
routerHandler: deps.Router,
|
apiPrefix: deps.ApiPrefix,
|
||||||
|
routerHandler: deps.RouterHandler,
|
||||||
|
modules: deps.Modules,
|
||||||
}
|
}
|
||||||
|
|
||||||
app.router = app.routerHandler.Set()
|
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
|
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 (
|
import (
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
log "github.com/sirupsen/logrus"
|
log "github.com/sirupsen/logrus"
|
||||||
|
swaggerFiles "github.com/swaggo/files"
|
||||||
|
ginSwagger "github.com/swaggo/gin-swagger"
|
||||||
"merch-parser-api/internal/interfaces"
|
"merch-parser-api/internal/interfaces"
|
||||||
"net/http"
|
"net/http"
|
||||||
)
|
)
|
||||||
|
|
@ -44,5 +46,8 @@ func (r *router) Set() *gin.Engine {
|
||||||
//version
|
//version
|
||||||
r.engine.GET("/version", func(c *gin.Context) { c.JSON(200, gin.H{"version": "2.0.0-alpha"}) })
|
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
|
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