feat: Add documentation generation and serve functionality

- Implement documentation generation
- Replace chi router with gin for documentation server
- Fix issues with docs command execution
This commit is contained in:
2025-11-04 19:15:28 +03:00
parent 3fc545f067
commit 5a8b53b49d
10 changed files with 206 additions and 100 deletions

View File

@@ -2,7 +2,8 @@ package controller
import (
"github.com/gin-gonic/gin"
"github.com/go-andiamo/chioas"
"github.com/swaggest/openapi-go"
"github.com/swaggest/openapi-go/openapi3"
)
// SecuredController - means, controller has middlewares
@@ -12,7 +13,7 @@ type SecuredController interface {
}
type DocumentateController interface {
Documentate() (*chioas.Paths, *chioas.Components)
Documentate(r *openapi3.Reflector) openapi.OperationContext
}
type Controller interface {

View File

@@ -6,7 +6,8 @@ import (
"git.ostiwe.com/ostiwe-com/status/modules/log"
"git.ostiwe.com/ostiwe-com/status/router/controller"
"github.com/gin-gonic/gin"
"github.com/go-andiamo/chioas"
"github.com/swaggest/openapi-go"
"github.com/swaggest/openapi-go/openapi3"
)
type Controller struct {
@@ -34,29 +35,19 @@ func (c *Controller) Handler() gin.HandlerFunc {
}
}
func (c *Controller) Documentate() (*chioas.Paths, *chioas.Components) {
return &chioas.Paths{
"/ping": chioas.Path{
Methods: map[string]chioas.Method{
http.MethodGet: {
Handler: c.Handler(),
Responses: chioas.Responses{
http.StatusOK: {
ContentType: "plain/text",
Schema: chioas.Schema{Type: "string"},
Examples: chioas.Examples{
{
func (c *Controller) Documentate(r *openapi3.Reflector) openapi.OperationContext {
op, err := r.NewOperationContext(c.Method(), c.Path())
if err != nil {
return nil
}
Name: "200 OK",
Value: "pong",
},
},
},
},
},
},
Tag: "service",
Comment: "Route for check service is alive",
},
}, nil
op.SetDescription("Route for check service is alive")
op.SetSummary("Server ping")
op.AddRespStructure("pong", func(cu *openapi.ContentUnit) {
cu.ContentType = "plain/text"
cu.HTTPStatus = http.StatusOK
})
return op
}

View File

@@ -2,9 +2,9 @@ package router
import (
"fmt"
"maps"
"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"
@@ -12,7 +12,7 @@ import (
"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"
"github.com/swaggest/openapi-go/openapi3"
)
func getControllers() []controller.Controller {
@@ -27,6 +27,8 @@ 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(),
@@ -61,27 +63,16 @@ func InitRoutes() *gin.Engine {
return r
}
func Documentate() chioas.Definition {
func Documentate() (*openapi3.Spec, error) {
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),
reflector := &openapi3.Reflector{
Spec: &openapi3.Spec{
Openapi: "3.0.3",
Info: openapi3.Info{
Title: "OstiweStatus API",
Version: version.AppVersion(),
},
},
}
@@ -91,25 +82,12 @@ func Documentate() chioas.Definition {
continue
}
documentatePaths, components := documentated.Documentate()
if documentatePaths != nil {
for path, pathDoc := range *documentatePaths {
apiDoc.Paths[path] = pathDoc
}
err := reflector.AddOperation(documentated.Documentate(reflector))
if err != nil {
return nil, err
}
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
return reflector.Spec, nil
}