refactor(router): Rename file
This commit is contained in:
99
router/server.go
Normal file
99
router/server.go
Normal file
@@ -0,0 +1,99 @@
|
||||
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/ping"
|
||||
"git.ostiwe.com/ostiwe-com/status/router/controller/service"
|
||||
"git.ostiwe.com/ostiwe-com/status/version"
|
||||
"github.com/go-andiamo/chioas"
|
||||
"github.com/go-chi/chi/v5"
|
||||
"github.com/go-chi/chi/v5/middleware"
|
||||
)
|
||||
|
||||
func getControllers() []controller.Controller {
|
||||
return []controller.Controller{
|
||||
ping.Controller{}.New(),
|
||||
service.Controller{}.New(),
|
||||
}
|
||||
}
|
||||
|
||||
func InitRoutes() *chi.Mux {
|
||||
log.Global.Get(log.SERVER).Info("Setting up routers")
|
||||
startTime := time.Now()
|
||||
|
||||
httpLogger := middleware.RequestLogger(&middleware.DefaultLogFormatter{Logger: log.Global.Get(log.SERVER), NoColor: false})
|
||||
|
||||
r := chi.NewRouter()
|
||||
r.Use(httpLogger)
|
||||
r.Use(
|
||||
middleware.RequestID,
|
||||
middleware.RealIP,
|
||||
middleware.StripSlashes,
|
||||
middleware.Recoverer,
|
||||
middleware.CleanPath,
|
||||
middleware.Timeout(15*time.Second),
|
||||
)
|
||||
|
||||
ctrlList := getControllers()
|
||||
for _, ctrl := range ctrlList {
|
||||
r.Group(ctrl.Group)
|
||||
}
|
||||
|
||||
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 {
|
||||
documentatePaths, components := ctrl.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
|
||||
}
|
||||
Reference in New Issue
Block a user