mirror of
https://github.com/bitechdev/ResolveSpec.git
synced 2026-01-02 09:54:26 +00:00
Added openapi spec
This commit is contained in:
@@ -22,11 +22,12 @@ type FallbackHandler func(w common.ResponseWriter, r common.Request, params map[
|
||||
|
||||
// Handler handles API requests using database and model abstractions
|
||||
type Handler struct {
|
||||
db common.Database
|
||||
registry common.ModelRegistry
|
||||
nestedProcessor *common.NestedCUDProcessor
|
||||
hooks *HookRegistry
|
||||
fallbackHandler FallbackHandler
|
||||
db common.Database
|
||||
registry common.ModelRegistry
|
||||
nestedProcessor *common.NestedCUDProcessor
|
||||
hooks *HookRegistry
|
||||
fallbackHandler FallbackHandler
|
||||
openAPIGenerator func() (string, error)
|
||||
}
|
||||
|
||||
// NewHandler creates a new API handler with database and registry abstractions
|
||||
@@ -75,6 +76,12 @@ func (h *Handler) Handle(w common.ResponseWriter, r common.Request, params map[s
|
||||
}
|
||||
}()
|
||||
|
||||
// Check for ?openapi query parameter
|
||||
if r.UnderlyingRequest().URL.Query().Get("openapi") != "" {
|
||||
h.HandleOpenAPI(w, r)
|
||||
return
|
||||
}
|
||||
|
||||
ctx := r.UnderlyingRequest().Context()
|
||||
|
||||
body, err := r.Body()
|
||||
@@ -156,6 +163,12 @@ func (h *Handler) HandleGet(w common.ResponseWriter, r common.Request, params ma
|
||||
}
|
||||
}()
|
||||
|
||||
// Check for ?openapi query parameter
|
||||
if r.UnderlyingRequest().URL.Query().Get("openapi") != "" {
|
||||
h.HandleOpenAPI(w, r)
|
||||
return
|
||||
}
|
||||
|
||||
schema := params["schema"]
|
||||
entity := params["entity"]
|
||||
|
||||
@@ -1433,3 +1446,28 @@ func toSnakeCase(s string) string {
|
||||
}
|
||||
return strings.ToLower(result.String())
|
||||
}
|
||||
|
||||
// HandleOpenAPI generates and returns the OpenAPI specification
|
||||
func (h *Handler) HandleOpenAPI(w common.ResponseWriter, r common.Request) {
|
||||
if h.openAPIGenerator == nil {
|
||||
logger.Error("OpenAPI generator not configured")
|
||||
h.sendError(w, http.StatusInternalServerError, "openapi_not_configured", "OpenAPI generation not configured", nil)
|
||||
return
|
||||
}
|
||||
|
||||
spec, err := h.openAPIGenerator()
|
||||
if err != nil {
|
||||
logger.Error("Failed to generate OpenAPI spec: %v", err)
|
||||
h.sendError(w, http.StatusInternalServerError, "openapi_generation_error", "Failed to generate OpenAPI specification", err)
|
||||
return
|
||||
}
|
||||
|
||||
w.SetHeader("Content-Type", "application/json")
|
||||
w.WriteHeader(http.StatusOK)
|
||||
w.Write([]byte(spec))
|
||||
}
|
||||
|
||||
// SetOpenAPIGenerator sets the OpenAPI generator function
|
||||
func (h *Handler) SetOpenAPIGenerator(generator func() (string, error)) {
|
||||
h.openAPIGenerator = generator
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user