- Apply automated linter fixes - Manually reformat fields in model/service.go - Resolve code complexity issues
96 lines
2.0 KiB
Go
96 lines
2.0 KiB
Go
package router
|
|
|
|
import (
|
|
"fmt"
|
|
"time"
|
|
|
|
"git.ostiwe.com/ostiwe-com/status/modules/jwt"
|
|
"git.ostiwe.com/ostiwe-com/status/modules/log"
|
|
"git.ostiwe.com/ostiwe-com/status/router/controller"
|
|
"git.ostiwe.com/ostiwe-com/status/router/controller/auth"
|
|
"git.ostiwe.com/ostiwe-com/status/router/controller/ping"
|
|
"git.ostiwe.com/ostiwe-com/status/router/controller/service"
|
|
"git.ostiwe.com/ostiwe-com/status/version"
|
|
"github.com/gin-gonic/gin"
|
|
"github.com/swaggest/openapi-go/openapi3"
|
|
)
|
|
|
|
func getControllers() []controller.Controller {
|
|
return []controller.Controller{
|
|
new(ping.Controller).New(),
|
|
new(service.GetServices).New(),
|
|
new(auth.PlainAuthController).New(),
|
|
}
|
|
}
|
|
|
|
func InitRoutes() *gin.Engine {
|
|
log.Global.Get(log.SERVER).Info("Setting up routers")
|
|
startTime := time.Now()
|
|
|
|
jwt.Init()
|
|
|
|
r := gin.New()
|
|
r.Use(
|
|
gin.Recovery(),
|
|
gin.LoggerWithConfig(gin.LoggerConfig{
|
|
Output: log.Global.Get(log.SERVER).Out,
|
|
}),
|
|
)
|
|
|
|
ctrlList := getControllers()
|
|
for _, ctrl := range ctrlList {
|
|
secured, ok := ctrl.(controller.SecuredController)
|
|
if ok {
|
|
r.Handle(
|
|
ctrl.Method(),
|
|
ctrl.Path(),
|
|
append(secured.Middlewares(), ctrl.Handler())...,
|
|
)
|
|
|
|
continue
|
|
}
|
|
|
|
r.Handle(
|
|
ctrl.Method(),
|
|
ctrl.Path(),
|
|
ctrl.Handler(),
|
|
)
|
|
}
|
|
|
|
log.Global.Get(log.SERVER).Info(fmt.Sprintf("Initialized %d routers", len(ctrlList)))
|
|
log.Global.Get(log.SERVER).Info(fmt.Sprintf(
|
|
"Setting up routers is done for %dms, start server",
|
|
time.Since(startTime).Milliseconds(),
|
|
))
|
|
|
|
return r
|
|
}
|
|
|
|
func Documentate() (*openapi3.Spec, error) {
|
|
ctrlList := getControllers()
|
|
|
|
reflector := &openapi3.Reflector{
|
|
Spec: &openapi3.Spec{
|
|
Openapi: "3.0.3",
|
|
Info: openapi3.Info{
|
|
Title: "OstiweStatus API",
|
|
Version: version.AppVersion(),
|
|
},
|
|
},
|
|
}
|
|
|
|
for _, ctrl := range ctrlList {
|
|
documentated, ok := ctrl.(controller.DocumentateController)
|
|
if !ok {
|
|
continue
|
|
}
|
|
|
|
err := reflector.AddOperation(documentated.Documentate(reflector))
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
}
|
|
|
|
return reflector.Spec, nil
|
|
}
|