From 29f85db02279da040dcf2600682318fb011bfd04 Mon Sep 17 00:00:00 2001 From: yusing Date: Mon, 6 Jan 2025 00:49:29 +0800 Subject: [PATCH] schema update and api /v1/schema --- Dockerfile | 3 +++ internal/api/handler.go | 1 + internal/api/v1/file.go | 13 +++++++++++++ schema/providers.schema.json | 28 ++++++++++++---------------- 4 files changed, 29 insertions(+), 16 deletions(-) diff --git a/Dockerfile b/Dockerfile index e5fd4df8..864e79ff 100644 --- a/Dockerfile +++ b/Dockerfile @@ -50,6 +50,9 @@ COPY config.example.yml /app/config/config.yml # copy certs COPY --from=builder /etc/ssl/certs /etc/ssl/certs +# copy schema +COPY schema /app/schema + ENV DOCKER_HOST=unix:///var/run/docker.sock ENV GODOXY_DEBUG=0 diff --git a/internal/api/handler.go b/internal/api/handler.go index d4170d0e..7bdaa4b6 100644 --- a/internal/api/handler.go +++ b/internal/api/handler.go @@ -37,6 +37,7 @@ func NewHandler() http.Handler { mux.HandleFunc("PUT", "/v1/file/{filename...}", auth.RequireAuth(v1.SetFileContent)) mux.HandleFunc("GET", "/v1/stats", v1.Stats) mux.HandleFunc("GET", "/v1/stats/ws", v1.StatsWS) + mux.HandleFunc("GET", "/v1/schema/{filename...}", v1.GetSchemaFile) return mux } diff --git a/internal/api/v1/file.go b/internal/api/v1/file.go index 2a3ec40c..c19518f2 100644 --- a/internal/api/v1/file.go +++ b/internal/api/v1/file.go @@ -64,3 +64,16 @@ func SetFileContent(w http.ResponseWriter, r *http.Request) { } w.WriteHeader(http.StatusOK) } + +func GetSchemaFile(w http.ResponseWriter, r *http.Request) { + filename := r.PathValue("filename") + if filename == "" { + U.RespondError(w, U.ErrMissingKey("filename"), http.StatusBadRequest) + } + content, err := os.ReadFile(path.Join(common.SchemaBasePath, filename)) + if err != nil { + U.HandleErr(w, r, err) + return + } + U.WriteBody(w, content) +} diff --git a/schema/providers.schema.json b/schema/providers.schema.json index 053702c7..96aba37e 100644 --- a/schema/providers.schema.json +++ b/schema/providers.schema.json @@ -121,6 +121,7 @@ }, "load_balance": { "type": "object", + "additionalProperties": false, "properties": { "link": { "type": "string", @@ -149,6 +150,7 @@ }, "healthcheck": { "type": "object", + "additionalProperties": false, "properties": { "disable": { "type": "boolean", @@ -203,7 +205,8 @@ "then": { "properties": { "port": { - "markdownDescription": "Proxy port from **0** to **65535**", + "title": "Proxy port", + "markdownDescription": "From **0** to **65535**", "oneOf": [ { "type": "string", @@ -218,21 +221,14 @@ ] }, "path_patterns": { - "oneOf": [ - { - "type": "array", - "markdownDescription": "A list of [path patterns](https://pkg.go.dev/net/http#hdr-Patterns-ServeMux)", - "items": { - "type": "string", - "pattern": "^(?:([A-Z]+) )?(?:([a-zA-Z0-9.-]+)\\/)?(\\/[^\\s]*)$", - "patternErrorMessage": "invalid path pattern" - } - }, - { - "type": "null", - "description": "No proxy path" - } - ] + "title": "Path patterns", + "type": "array", + "markdownDescription": "See https://pkg.go.dev/net/http#hdr-Patterns-ServeMux", + "items": { + "type": "string", + "pattern": "^(?:([A-Z]+) )?(?:([a-zA-Z0-9.-]+)\\/)?(\\/[^\\s]*)$", + "patternErrorMessage": "invalid path pattern" + } }, "middlewares": { "type": "object"