feat: init api
This commit is contained in:
+14
-20
@@ -7,29 +7,19 @@ import (
|
||||
"github.com/gin-gonic/gin/binding"
|
||||
"github.com/go-playground/validator/v10"
|
||||
"github.com/iliadenisov/galaxy/internal/controller"
|
||||
"github.com/iliadenisov/galaxy/internal/model/rest"
|
||||
"github.com/iliadenisov/galaxy/internal/router/handler"
|
||||
)
|
||||
|
||||
var (
|
||||
StoragePath string
|
||||
)
|
||||
|
||||
func init() {
|
||||
StoragePath = os.Getenv("STORAGE_PATH")
|
||||
}
|
||||
|
||||
func param() func(*controller.Param) {
|
||||
func initConfig() func(*controller.Param) {
|
||||
return func(p *controller.Param) {
|
||||
// TODO: initialize base controller settings
|
||||
p.StoragePath = StoragePath
|
||||
p.StoragePath = os.Getenv("STORAGE_PATH")
|
||||
}
|
||||
}
|
||||
|
||||
type Executor func(rest.Command) error
|
||||
|
||||
type Router struct {
|
||||
r *gin.Engine
|
||||
executor Executor
|
||||
executor handler.CommandExecutor
|
||||
}
|
||||
|
||||
func (r Router) Run() error {
|
||||
@@ -37,14 +27,14 @@ func (r Router) Run() error {
|
||||
}
|
||||
|
||||
func NewRouter() Router {
|
||||
return NewRouterExecutor(Execute)
|
||||
return NewRouterExecutor(handler.ExecuteCommand)
|
||||
}
|
||||
|
||||
func NewRouterExecutor(executor Executor) Router {
|
||||
return Router{r: setupRouter(executor)}
|
||||
func NewRouterExecutor(executor handler.CommandExecutor) Router {
|
||||
return Router{r: setupRouter(initConfig(), executor)}
|
||||
}
|
||||
|
||||
func setupRouter(executor Executor) *gin.Engine {
|
||||
func setupRouter(config controller.Config, executor handler.CommandExecutor) *gin.Engine {
|
||||
gin.SetMode(gin.ReleaseMode)
|
||||
r := gin.New()
|
||||
r.Use(gin.Recovery())
|
||||
@@ -53,7 +43,11 @@ func setupRouter(executor Executor) *gin.Engine {
|
||||
v.RegisterValidation("notblank", notBlankStringValidator)
|
||||
}
|
||||
|
||||
r.GET("/api/v1/status", StatusHandler)
|
||||
r.PUT("/api/v1/command", LimitMiddleware(1), func(ctx *gin.Context) { CommandHandler(ctx, executor) })
|
||||
groupV1 := r.Group("/api/v1")
|
||||
|
||||
groupV1.GET("/status", func(ctx *gin.Context) { handler.StatusHandler(ctx, config) })
|
||||
groupV1.POST("/init", func(ctx *gin.Context) { handler.InitHandler(ctx, config) })
|
||||
groupV1.PUT("/command", LimitMiddleware(1), func(ctx *gin.Context) { handler.CommandHandler(ctx, config, executor) })
|
||||
|
||||
return r
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user