Files
status/router/server.go
ostiwe 7bc4ce8c96 refactor!: Use gin router instead chi router
- Removed chi dependencies
- Updated router registration logic
2025-11-04 19:20:00 +03:00

116 lines
3.0 KiB
Go

package router
import (
"fmt"
"maps"
"time"
"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/go-andiamo/chioas"
)
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()
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() chioas.Definition {
ctrlList := getControllers()
apiDoc := chioas.Definition{
AutoHeadMethods: true,
DocOptions: chioas.DocOptions{
ServeDocs: true,
HideHeadMethods: true,
},
Info: chioas.Info{
Version: version.AppVersion(),
Title: "Status page API Documentation",
},
Paths: make(chioas.Paths),
Components: &chioas.Components{
Schemas: make(chioas.Schemas, 0),
Requests: make(chioas.CommonRequests),
Responses: make(chioas.CommonResponses),
Examples: make(chioas.Examples, 0),
Parameters: make(chioas.CommonParameters),
SecuritySchemes: make(chioas.SecuritySchemes, 0),
Extensions: make(chioas.Extensions),
},
}
for _, ctrl := range ctrlList {
documentated, ok := ctrl.(controller.DocumentateController)
if !ok {
continue
}
documentatePaths, components := documentated.Documentate()
if documentatePaths != nil {
for path, pathDoc := range *documentatePaths {
apiDoc.Paths[path] = pathDoc
}
}
if components != nil {
apiDoc.Components.Schemas = append(apiDoc.Components.Schemas, components.Schemas...)
apiDoc.Components.Examples = append(apiDoc.Components.Examples, components.Examples...)
apiDoc.Components.SecuritySchemes = append(apiDoc.Components.SecuritySchemes, components.SecuritySchemes...)
maps.Copy(apiDoc.Components.Requests, components.Requests)
maps.Copy(apiDoc.Components.Responses, components.Responses)
maps.Copy(apiDoc.Components.Parameters, components.Parameters)
maps.Copy(apiDoc.Components.Extensions, components.Extensions)
}
}
return apiDoc
}