2022-12-31 10:22:38 +01:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
"github.com/go-playground/validator/v10"
|
2023-01-30 19:59:42 +01:00
|
|
|
"github.com/twofas/2fas-server/config"
|
|
|
|
extension "github.com/twofas/2fas-server/internal/api/browser_extension/service"
|
|
|
|
health "github.com/twofas/2fas-server/internal/api/health/service"
|
|
|
|
icons "github.com/twofas/2fas-server/internal/api/icons/service"
|
|
|
|
mobile "github.com/twofas/2fas-server/internal/api/mobile/service"
|
|
|
|
support "github.com/twofas/2fas-server/internal/api/support/service"
|
|
|
|
"github.com/twofas/2fas-server/internal/common/api"
|
|
|
|
"github.com/twofas/2fas-server/internal/common/db"
|
|
|
|
"github.com/twofas/2fas-server/internal/common/redis"
|
2023-03-12 14:58:54 +01:00
|
|
|
"github.com/twofas/2fas-server/internal/common/validation"
|
2022-12-31 10:22:38 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
var validate *validator.Validate
|
|
|
|
|
|
|
|
type Module interface {
|
|
|
|
RegisterRoutes(router *gin.Engine)
|
|
|
|
}
|
|
|
|
|
|
|
|
type Application struct {
|
|
|
|
Addr string
|
|
|
|
|
|
|
|
Router *gin.Engine
|
|
|
|
|
|
|
|
Config config.Configuration
|
|
|
|
|
|
|
|
Modules []Module
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewApplication(config config.Configuration) *Application {
|
|
|
|
validate = validator.New()
|
|
|
|
|
|
|
|
gorm := db.NewGormConnection(config)
|
|
|
|
database := db.NewDbConnection(config)
|
|
|
|
redisClient := redis.New(config.Redis.ServiceUrl, config.Redis.Port)
|
|
|
|
|
2023-03-12 14:58:54 +01:00
|
|
|
validate.RegisterValidation("not_blank", validation.NotBlank)
|
|
|
|
|
2022-12-31 10:22:38 +01:00
|
|
|
modules := []Module{
|
|
|
|
health.NewHealthModule(config, redisClient),
|
|
|
|
support.NewSupportModule(config, gorm, database, validate),
|
|
|
|
icons.NewIconsModule(config, gorm, database, validate),
|
2023-03-12 14:58:54 +01:00
|
|
|
extension.NewBrowserExtensionModule(config, gorm, database, redisClient, validate),
|
2022-12-31 10:22:38 +01:00
|
|
|
mobile.NewMobileModule(config, gorm, database, validate, redisClient),
|
|
|
|
}
|
|
|
|
|
|
|
|
app := &Application{
|
|
|
|
Addr: config.App.ListenAddr,
|
|
|
|
Config: config,
|
|
|
|
Modules: modules,
|
|
|
|
}
|
|
|
|
|
|
|
|
return app
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *Application) RegisterRoutes(router *gin.Engine) {
|
|
|
|
router.NoRoute(func(c *gin.Context) {
|
|
|
|
c.JSON(404, api.NotFoundError(errors.New("URI not found")))
|
|
|
|
})
|
|
|
|
|
|
|
|
for _, module := range a.Modules {
|
|
|
|
module.RegisterRoutes(router)
|
|
|
|
}
|
|
|
|
}
|