From c8a2effac4972723db3ade1374265e723fe410cb Mon Sep 17 00:00:00 2001 From: Ralph Slooten Date: Sun, 30 Jul 2023 17:14:23 +1200 Subject: [PATCH 1/2] Add comment to apiv1 --- server/apiv1/api.go | 1 + 1 file changed, 1 insertion(+) diff --git a/server/apiv1/api.go b/server/apiv1/api.go index f6e8163..e5010d2 100644 --- a/server/apiv1/api.go +++ b/server/apiv1/api.go @@ -1,3 +1,4 @@ +// Package apiv1 handles all the API responses package apiv1 import ( From 24fb49d079a5e4ffebd96176b550374c6b63d8f8 Mon Sep 17 00:00:00 2001 From: Ralph Slooten Date: Sun, 30 Jul 2023 17:35:17 +1200 Subject: [PATCH 2/2] Fix: Add basePath to swagger.json if webroot is specified @See #147 --- server/server.go | 21 +++++++++++++++++++++ server/ui/api/v1/swagger.json | 10 +++++----- 2 files changed, 26 insertions(+), 5 deletions(-) diff --git a/server/server.go b/server/server.go index d991c14..b90c06f 100644 --- a/server/server.go +++ b/server/server.go @@ -2,8 +2,10 @@ package server import ( + "bytes" "compress/gzip" "embed" + "fmt" "io" "io/fs" "net/http" @@ -96,6 +98,7 @@ func defaultRoutes() *mux.Router { r.HandleFunc(config.Webroot+"api/v1/message/{id}", middleWareFunc(apiv1.GetMessage)).Methods("GET") r.HandleFunc(config.Webroot+"api/v1/info", middleWareFunc(apiv1.AppInfo)).Methods("GET") r.HandleFunc(config.Webroot+"api/v1/webui", middleWareFunc(apiv1.WebUIConfig)).Methods("GET") + r.HandleFunc(config.Webroot+"api/v1/swagger.json", middleWareFunc(swaggerBasePath)).Methods("GET") // return blank 200 response for OPTIONS requests for CORS r.PathPrefix(config.Webroot + "api/v1/").Handler(middleWareFunc(apiv1.GetOptions)).Methods("OPTIONS") @@ -205,3 +208,21 @@ func addSlashToWebroot(w http.ResponseWriter, r *http.Request) { func apiWebsocket(w http.ResponseWriter, r *http.Request) { websockets.ServeWs(websockets.MessageHub, w, r) } + +// Wrapper to artificially inject a basePath to the swagger.json if a webroot has been specified +func swaggerBasePath(w http.ResponseWriter, _ *http.Request) { + f, err := embeddedFS.ReadFile("ui/api/v1/swagger.json") + if err != nil { + panic(err) + } + + if config.Webroot != "/" { + // artificially inject a path at the start + replacement := fmt.Sprintf("{\n \"basePath\": \"%s\",", strings.TrimRight(config.Webroot, "/")) + + f = bytes.Replace(f, []byte("{"), []byte(replacement), 1) + } + + w.Header().Add("Content-Type", "application/json") + _, _ = w.Write(f) +} diff --git a/server/ui/api/v1/swagger.json b/server/ui/api/v1/swagger.json index 0617672..f0241e4 100644 --- a/server/ui/api/v1/swagger.json +++ b/server/ui/api/v1/swagger.json @@ -670,7 +670,7 @@ } }, "x-go-name": "Response", - "x-go-package": "github.com/axllent/mailpit/utils/html-check" + "x-go-package": "github.com/axllent/mailpit/utils/htmlcheck" }, "HTMLCheckResult": { "description": "Result struct", @@ -702,7 +702,7 @@ } }, "x-go-name": "Result", - "x-go-package": "github.com/axllent/mailpit/utils/html-check" + "x-go-package": "github.com/axllent/mailpit/utils/htmlcheck" }, "HTMLCheckScore": { "description": "Score struct", @@ -730,7 +730,7 @@ } }, "x-go-name": "Score", - "x-go-package": "github.com/axllent/mailpit/utils/html-check" + "x-go-package": "github.com/axllent/mailpit/utils/htmlcheck" }, "HTMLCheckTotal": { "description": "Total weighted result for all scores", @@ -763,7 +763,7 @@ } }, "x-go-name": "Total", - "x-go-package": "github.com/axllent/mailpit/utils/html-check" + "x-go-package": "github.com/axllent/mailpit/utils/htmlcheck" }, "HTMLCheckWarning": { "description": "Warning represents a failed test", @@ -819,7 +819,7 @@ } }, "x-go-name": "Warning", - "x-go-package": "github.com/axllent/mailpit/utils/html-check" + "x-go-package": "github.com/axllent/mailpit/utils/htmlcheck" }, "Message": { "description": "Message data excluding physical attachments",