Update swagger renderer to load external files properly
This commit is contained in:
parent
7d9b926eb4
commit
304fa4a99e
@ -67,6 +67,7 @@ func (m *Module) SetupModule(router *mux.Router, f func(cb func(http.ResponseWri
|
|||||||
}
|
}
|
||||||
router.HandleFunc("/", m.getClient(m.homepage))
|
router.HandleFunc("/", m.getClient(m.homepage))
|
||||||
router.HandleFunc("/login", m.sessionWrapper(m.loginPage))
|
router.HandleFunc("/login", m.sessionWrapper(m.loginPage))
|
||||||
|
router.PathPrefix("/spec/{org}/{repo}/{sha}/{spec:.+}").HandlerFunc(m.getClient(m.specPage))
|
||||||
router.PathPrefix("/swagger").HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
|
router.PathPrefix("/swagger").HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
|
||||||
p := filepath.Join("swagger", filepath.Base(req.URL.Path))
|
p := filepath.Join("swagger", filepath.Base(req.URL.Path))
|
||||||
open, err := assets.SwaggerAssets.Open(p)
|
open, err := assets.SwaggerAssets.Open(p)
|
||||||
@ -140,6 +141,7 @@ func (m *Module) homepage(rw http.ResponseWriter, req *http.Request, state *util
|
|||||||
selModule := ""
|
selModule := ""
|
||||||
selCommitTime := ""
|
selCommitTime := ""
|
||||||
selCommitHash := ""
|
selCommitHash := ""
|
||||||
|
selCommitHashShort := ""
|
||||||
mySpecs := make([]struct {
|
mySpecs := make([]struct {
|
||||||
Name string
|
Name string
|
||||||
Code int
|
Code int
|
||||||
@ -196,54 +198,9 @@ func (m *Module) homepage(rw http.ResponseWriter, req *http.Request, state *util
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if q.Has("spec") {
|
|
||||||
spec := q.Get("spec")
|
|
||||||
if q.Has("raw") && q.Get("raw") == "true" {
|
|
||||||
open, _, err := giteaClient.GetFile(myOrg, selRepo, ref.Object.SHA, spec)
|
|
||||||
if err != nil {
|
|
||||||
http.Error(rw, "OpenAPI spec raw: "+err.Error(), http.StatusInternalServerError)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
http.ServeContent(rw, req, spec, repo.Updated, bytes.NewReader(open))
|
|
||||||
return
|
|
||||||
}
|
|
||||||
contents, _, err := giteaClient.GetContents(myOrg, selRepo, ref.Object.SHA, spec)
|
|
||||||
if err != nil {
|
|
||||||
http.Error(rw, "OpenAPI spec: "+err.Error(), http.StatusInternalServerError)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
tmp, err := template.New("swagger").Parse(swaggerTemplate)
|
|
||||||
if err != nil {
|
|
||||||
fmt.Println("Template parse error:", err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
q2 := q
|
|
||||||
q2.Set("raw", "true")
|
|
||||||
err = tmp.Execute(rw, struct {
|
|
||||||
LoadUrls []struct {
|
|
||||||
Url string `json:"url"`
|
|
||||||
Name string `json:"name"`
|
|
||||||
}
|
|
||||||
LoadMain string
|
|
||||||
}{
|
|
||||||
LoadUrls: []struct {
|
|
||||||
Url string `json:"url"`
|
|
||||||
Name string `json:"name"`
|
|
||||||
}{
|
|
||||||
{Url: "/gitea/?" + q2.Encode(), Name: contents.Name},
|
|
||||||
},
|
|
||||||
LoadMain: contents.Name,
|
|
||||||
})
|
|
||||||
if err != nil {
|
|
||||||
fmt.Println("Template execute error:", err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
selCommitTime = commit.CommitMeta.Created.UTC().Format("20060102150405")
|
selCommitTime = commit.CommitMeta.Created.UTC().Format("20060102150405")
|
||||||
selCommitHash = commit.CommitMeta.SHA[:12]
|
selCommitHash = commit.CommitMeta.SHA
|
||||||
|
selCommitHashShort = commit.CommitMeta.SHA[:12]
|
||||||
goMod, resp, err := giteaClient.GetFile(myOrg, selRepo, ref.Object.SHA, "go.mod")
|
goMod, resp, err := giteaClient.GetFile(myOrg, selRepo, ref.Object.SHA, "go.mod")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if resp.StatusCode != http.StatusNotFound {
|
if resp.StatusCode != http.StatusNotFound {
|
||||||
@ -318,6 +275,7 @@ func (m *Module) homepage(rw http.ResponseWriter, req *http.Request, state *util
|
|||||||
ShowGoMod bool
|
ShowGoMod bool
|
||||||
CommitTime string
|
CommitTime string
|
||||||
CommitHash string
|
CommitHash string
|
||||||
|
CommitHashShort string
|
||||||
ShowSpec bool
|
ShowSpec bool
|
||||||
Specs []struct {
|
Specs []struct {
|
||||||
Name string
|
Name string
|
||||||
@ -335,6 +293,7 @@ func (m *Module) homepage(rw http.ResponseWriter, req *http.Request, state *util
|
|||||||
ShowGoMod: selModule != "",
|
ShowGoMod: selModule != "",
|
||||||
CommitTime: selCommitTime,
|
CommitTime: selCommitTime,
|
||||||
CommitHash: selCommitHash,
|
CommitHash: selCommitHash,
|
||||||
|
CommitHashShort: selCommitHashShort,
|
||||||
ShowSpec: len(mySpecs) > 0,
|
ShowSpec: len(mySpecs) > 0,
|
||||||
Specs: mySpecs,
|
Specs: mySpecs,
|
||||||
})
|
})
|
||||||
@ -383,25 +342,51 @@ func (m *Module) loginPage(rw http.ResponseWriter, req *http.Request, state *uti
|
|||||||
http.Redirect(rw, req, m.oauthClient.AuthCodeURL(flowState.String(), oauth2.AccessTypeOffline), http.StatusTemporaryRedirect)
|
http.Redirect(rw, req, m.oauthClient.AuthCodeURL(flowState.String(), oauth2.AccessTypeOffline), http.StatusTemporaryRedirect)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *Module) fetchRepos(giteaClient *gitea.Client) {
|
func (m *Module) specPage(rw http.ResponseWriter, req *http.Request, state *utils.State, giteaClient *gitea.Client) {
|
||||||
repos, _, err := giteaClient.ListOrgRepos("snow", gitea.ListOrgReposOptions{ListOptions: gitea.ListOptions{Page: 0, PageSize: 100}})
|
vars := mux.Vars(req)
|
||||||
|
myOrg := vars["org"]
|
||||||
|
myRepo := vars["repo"]
|
||||||
|
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 {
|
if err != nil {
|
||||||
fmt.Println(err)
|
http.Error(rw, "OpenAPI spec raw: "+err.Error(), http.StatusInternalServerError)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
for _, myRepo := range repos {
|
http.ServeContent(rw, req, mySpec, time.Now(), bytes.NewReader(open))
|
||||||
refs, _, err := giteaClient.GetRepoRefs("snow", myRepo.Name, "heads")
|
return
|
||||||
|
}
|
||||||
|
contents, _, err := giteaClient.GetContents(myOrg, myRepo, mySha, mySpec)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println(err)
|
http.Error(rw, "OpenAPI spec: "+err.Error(), http.StatusInternalServerError)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
fmt.Println(len(refs))
|
|
||||||
for _, myRef := range refs {
|
tmp, err := template.New("swagger").Parse(swaggerTemplate)
|
||||||
if myRef.Ref == "refs/heads/"+myRepo.DefaultBranch {
|
if err != nil {
|
||||||
fmt.Println(myRef.Ref)
|
fmt.Println("Template parse error:", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
err = tmp.Execute(rw, struct {
|
||||||
|
LoadUrls []struct {
|
||||||
|
Url string `json:"url"`
|
||||||
|
Name string `json:"name"`
|
||||||
}
|
}
|
||||||
fmt.Println("Can't find default branch")
|
LoadMain string
|
||||||
|
}{
|
||||||
|
LoadUrls: []struct {
|
||||||
|
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},
|
||||||
|
},
|
||||||
|
LoadMain: contents.Name,
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println("Template execute error:", err)
|
||||||
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -57,7 +57,7 @@
|
|||||||
<div>
|
<div>
|
||||||
{{if .ShowGoMod}}
|
{{if .ShowGoMod}}
|
||||||
<p>Repository details:</p>
|
<p>Repository details:</p>
|
||||||
<p>Go import: go get {{.SelModule}}@v0.0.0-{{.CommitTime}}-{{.CommitHash}}</p>
|
<p>Go import: go get {{.SelModule}}@v0.0.0-{{.CommitTime}}-{{.CommitHashShort}}</p>
|
||||||
{{end}}
|
{{end}}
|
||||||
{{if .ShowSpec}}
|
{{if .ShowSpec}}
|
||||||
<div>
|
<div>
|
||||||
@ -68,7 +68,7 @@
|
|||||||
{{if not (eq .Code 200)}}
|
{{if not (eq .Code 200)}}
|
||||||
{{.Name}} ({{.Code}})
|
{{.Name}} ({{.Code}})
|
||||||
{{else}}
|
{{else}}
|
||||||
<a href="?org={{$.SelOrg}}&repo={{$.SelRepo}}&spec={{.Name}}" target="_blank">{{.Name}}</a>
|
<a href="/gitea/spec/{{$.SelOrg}}/{{$.SelRepo}}/{{$.CommitHash}}/{{.Name}}" target="_blank">{{.Name}}</a>
|
||||||
{{end}}
|
{{end}}
|
||||||
</li>
|
</li>
|
||||||
{{end}}
|
{{end}}
|
||||||
|
@ -4,16 +4,16 @@
|
|||||||
<meta charset="UTF-8">
|
<meta charset="UTF-8">
|
||||||
<meta name="viewport" content="width=device-width, initial-scale=1">
|
<meta name="viewport" content="width=device-width, initial-scale=1">
|
||||||
<title>Swagger | Gitea | Melon Tools</title>
|
<title>Swagger | Gitea | Melon Tools</title>
|
||||||
<link rel="stylesheet" type="text/css" href="swagger/swagger-ui.css" />
|
<link rel="stylesheet" type="text/css" href="/gitea/swagger/swagger-ui.css" />
|
||||||
<link rel="stylesheet" type="text/css" href="swagger/index.css" />
|
<link rel="stylesheet" type="text/css" href="/gitea/swagger/index.css" />
|
||||||
<link rel="icon" type="image/png" href="swagger/favicon-32x32.png" sizes="32x32" />
|
<link rel="icon" type="image/png" href="/gitea/swagger/favicon-32x32.png" sizes="32x32" />
|
||||||
<link rel="icon" type="image/png" href="swagger/favicon-16x16.png" sizes="16x16" />
|
<link rel="icon" type="image/png" href="/gitea/swagger/favicon-16x16.png" sizes="16x16" />
|
||||||
</head>
|
</head>
|
||||||
<body>
|
<body>
|
||||||
<div id="swagger-ui"></div>
|
<div id="swagger-ui"></div>
|
||||||
<script src="swagger/swagger-ui-bundle.js" charset="UTF-8"></script>
|
<script src="/gitea/swagger/swagger-ui-bundle.js" charset="UTF-8"></script>
|
||||||
<script src="swagger/swagger-ui-standalone-preset.js" charset="UTF-8"></script>
|
<script src="/gitea/swagger/swagger-ui-standalone-preset.js" charset="UTF-8"></script>
|
||||||
<script src="swagger/swagger-initializer-custom.js" charset="UTF-8"></script>
|
<script src="/gitea/swagger/swagger-initializer-custom.js" charset="UTF-8"></script>
|
||||||
<script>
|
<script>
|
||||||
window.loadUrls = {{.LoadUrls}};
|
window.loadUrls = {{.LoadUrls}};
|
||||||
window.loadMain = {{.LoadMain}};
|
window.loadMain = {{.LoadMain}};
|
||||||
|
Loading…
Reference in New Issue
Block a user