Fix loading external spec files

This commit is contained in:
Melon 2022-09-09 16:48:18 +01:00
parent 304fa4a99e
commit cb37844281
Signed by: melon
GPG Key ID: B0ADD5395BCDAAB6

View File

@ -68,6 +68,7 @@ func (m *Module) SetupModule(router *mux.Router, f func(cb func(http.ResponseWri
router.HandleFunc("/", m.getClient(m.homepage))
router.HandleFunc("/login", m.sessionWrapper(m.loginPage))
router.PathPrefix("/spec/{org}/{repo}/{sha}/{spec:.+}").HandlerFunc(m.getClient(m.specPage))
router.PathPrefix("/spec-raw/{org}/{repo}/{sha}/{spec:.+}").HandlerFunc(m.getClient(m.specRawPage))
router.PathPrefix("/swagger").HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
p := filepath.Join("swagger", filepath.Base(req.URL.Path))
open, err := assets.SwaggerAssets.Open(p)
@ -349,16 +350,6 @@ func (m *Module) specPage(rw http.ResponseWriter, req *http.Request, state *util
mySha := vars["sha"]
mySpec := vars["spec"]
q := req.URL.Query()
if q.Has("raw") && q.Get("raw") == "true" {
open, _, err := giteaClient.GetFile(myOrg, myRepo, mySha, mySpec)
if err != nil {
http.Error(rw, "OpenAPI spec raw: "+err.Error(), http.StatusInternalServerError)
return
}
http.ServeContent(rw, req, mySpec, time.Now(), bytes.NewReader(open))
return
}
contents, _, err := giteaClient.GetContents(myOrg, myRepo, mySha, mySpec)
if err != nil {
http.Error(rw, "OpenAPI spec: "+err.Error(), http.StatusInternalServerError)
@ -381,7 +372,7 @@ func (m *Module) specPage(rw http.ResponseWriter, req *http.Request, state *util
Url string `json:"url"`
Name string `json:"name"`
}{
{Url: fmt.Sprintf("/gitea/spec/%s/%s/%s/%s?raw=true", myOrg, myRepo, mySha, mySpec), Name: contents.Name},
{Url: fmt.Sprintf("/gitea/spec-raw/%s/%s/%s/%s", myOrg, myRepo, mySha, mySpec), Name: contents.Name},
},
LoadMain: contents.Name,
})
@ -390,3 +381,18 @@ func (m *Module) specPage(rw http.ResponseWriter, req *http.Request, state *util
return
}
}
func (m *Module) specRawPage(rw http.ResponseWriter, req *http.Request, state *utils.State, giteaClient *gitea.Client) {
vars := mux.Vars(req)
myOrg := vars["org"]
myRepo := vars["repo"]
mySha := vars["sha"]
mySpec := vars["spec"]
open, _, err := giteaClient.GetFile(myOrg, myRepo, mySha, mySpec)
if err != nil {
http.Error(rw, "OpenAPI spec raw: "+err.Error(), http.StatusInternalServerError)
return
}
http.ServeContent(rw, req, mySpec, time.Now(), bytes.NewReader(open))
}