1
0
mirror of https://github.com/axllent/mailpit.git synced 2025-04-11 11:41:58 +02:00

Fix: Add basePath to swagger.json if webroot is specified

@See #147
This commit is contained in:
Ralph Slooten 2023-07-30 17:35:17 +12:00
parent c8a2effac4
commit 24fb49d079
2 changed files with 26 additions and 5 deletions

View File

@ -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)
}

View File

@ -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",