diff --git a/cmd/main.go b/cmd/main.go index 841b0a7..fd4c50b 100644 --- a/cmd/main.go +++ b/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, + Host: c.AppConf.Host, + Port: c.AppConf.Port, + ApiPrefix: c.AppConf.ApiPrefix, + RouterHandler: routerHandler, + Modules: modules, }) err = appl.Run(ctx) diff --git a/internal/app/handler.go b/internal/app/handler.go index c329a9c..37cbe30 100644 --- a/internal/app/handler.go +++ b/internal/app/handler.go @@ -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 } diff --git a/internal/interfaces/app.go b/internal/interfaces/app.go new file mode 100644 index 0000000..1f8eecf --- /dev/null +++ b/internal/interfaces/app.go @@ -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 +} diff --git a/internal/router/handler.go b/internal/router/handler.go index 403feb0..b916349 100644 --- a/internal/router/handler.go +++ b/internal/router/handler.go @@ -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 } diff --git a/internal/shared/routes.go b/internal/shared/routes.go new file mode 100644 index 0000000..28aea9d --- /dev/null +++ b/internal/shared/routes.go @@ -0,0 +1,6 @@ +package shared + +type ExcludeRoute struct { + Route string + Method string +}