adjustment of CORS

This commit is contained in:
2026-01-06 20:03:09 +11:00
parent 1575ec0481
commit ae7847b64b

View File

@@ -295,6 +295,11 @@ func openAPISpecHandler(w http.ResponseWriter, r *http.Request) {
} }
func diagramHandler(w http.ResponseWriter, r *http.Request) { func diagramHandler(w http.ResponseWriter, r *http.Request) {
if r.Method == http.MethodOptions {
addCORS(w)
w.WriteHeader(http.StatusNoContent)
return
}
id := r.URL.Query().Get("id") id := r.URL.Query().Get("id")
if id == "" { if id == "" {
http.Error(w, "missing id", http.StatusBadRequest) http.Error(w, "missing id", http.StatusBadRequest)
@@ -305,6 +310,7 @@ func diagramHandler(w http.ResponseWriter, r *http.Request) {
http.Error(w, "not found", http.StatusNotFound) http.Error(w, "not found", http.StatusNotFound)
return return
} }
addCORS(w)
w.Header().Set("Content-Type", "application/xml") w.Header().Set("Content-Type", "application/xml")
_, _ = w.Write([]byte(xml)) _, _ = w.Write([]byte(xml))
} }
@@ -325,6 +331,12 @@ func buildDiagramsNetURL(r *http.Request, id string) string {
return "https://app.diagrams.net/?splash=0&ui=min&url=" + url.QueryEscape(base) return "https://app.diagrams.net/?splash=0&ui=min&url=" + url.QueryEscape(base)
} }
func addCORS(w http.ResponseWriter) {
w.Header().Set("Access-Control-Allow-Origin", "*")
w.Header().Set("Access-Control-Allow-Methods", "GET, OPTIONS")
w.Header().Set("Access-Control-Allow-Headers", "Content-Type")
}
func swaggerUIHandler(w http.ResponseWriter, r *http.Request) { func swaggerUIHandler(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodGet { if r.Method != http.MethodGet {
http.Error(w, "method not allowed", http.StatusMethodNotAllowed) http.Error(w, "method not allowed", http.StatusMethodNotAllowed)