47 lines
967 B
Go
47 lines
967 B
Go
package port
|
|
|
|
import (
|
|
"errors"
|
|
"net/http"
|
|
|
|
"gitea.dwysokinski.me/twhelp/corev3/internal/port/internal/apimodel"
|
|
"github.com/getkin/kin-openapi/openapi3"
|
|
)
|
|
|
|
func (h *apiHTTPHandler) sendOpenAPIJSON(w http.ResponseWriter, r *http.Request) {
|
|
schema, err := h.openAPISchema()
|
|
if err != nil {
|
|
renderPlainText(w, r, http.StatusInternalServerError, http.StatusText(http.StatusInternalServerError))
|
|
return
|
|
}
|
|
|
|
renderJSON(w, r, http.StatusOK, schema)
|
|
}
|
|
|
|
func getOpenAPISchema(cfg OpenAPIConfig) (*openapi3.T, error) {
|
|
if !cfg.Enabled {
|
|
return nil, errors.New("openapi is disabled")
|
|
}
|
|
|
|
schema, err := apimodel.GetSwagger()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
if len(cfg.Servers) == 0 {
|
|
return schema, nil
|
|
}
|
|
|
|
schema.Servers = make(openapi3.Servers, 0, len(cfg.Servers))
|
|
|
|
for _, s := range cfg.Servers {
|
|
u := *s.URL
|
|
u.Path = cfg.BasePath
|
|
schema.Servers = append(schema.Servers, &openapi3.Server{
|
|
URL: u.String(),
|
|
})
|
|
}
|
|
|
|
return schema, nil
|
|
}
|