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("/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) {
|
||||
p := filepath.Join("swagger", filepath.Base(req.URL.Path))
|
||||
open, err := assets.SwaggerAssets.Open(p)
|
||||
@ -140,6 +141,7 @@ func (m *Module) homepage(rw http.ResponseWriter, req *http.Request, state *util
|
||||
selModule := ""
|
||||
selCommitTime := ""
|
||||
selCommitHash := ""
|
||||
selCommitHashShort := ""
|
||||
mySpecs := make([]struct {
|
||||
Name string
|
||||
Code int
|
||||
@ -196,54 +198,9 @@ func (m *Module) homepage(rw http.ResponseWriter, req *http.Request, state *util
|
||||
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")
|
||||
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")
|
||||
if err != nil {
|
||||
if resp.StatusCode != http.StatusNotFound {
|
||||
@ -310,33 +267,35 @@ func (m *Module) homepage(rw http.ResponseWriter, req *http.Request, state *util
|
||||
Name string
|
||||
Private bool
|
||||
}
|
||||
MyOrg string
|
||||
SelOrg string
|
||||
SelRepo string
|
||||
ShowOrg bool
|
||||
SelModule string
|
||||
ShowGoMod bool
|
||||
CommitTime string
|
||||
CommitHash string
|
||||
ShowSpec bool
|
||||
Specs []struct {
|
||||
MyOrg string
|
||||
SelOrg string
|
||||
SelRepo string
|
||||
ShowOrg bool
|
||||
SelModule string
|
||||
ShowGoMod bool
|
||||
CommitTime string
|
||||
CommitHash string
|
||||
CommitHashShort string
|
||||
ShowSpec bool
|
||||
Specs []struct {
|
||||
Name string
|
||||
Code int
|
||||
}
|
||||
}{
|
||||
Username: myUser.UserName,
|
||||
Orgs: orgSimple,
|
||||
Repos: repoSimple,
|
||||
MyOrg: myOrg,
|
||||
SelOrg: selOrg,
|
||||
SelRepo: selRepo,
|
||||
SelModule: selModule,
|
||||
ShowOrg: myOrg != "",
|
||||
ShowGoMod: selModule != "",
|
||||
CommitTime: selCommitTime,
|
||||
CommitHash: selCommitHash,
|
||||
ShowSpec: len(mySpecs) > 0,
|
||||
Specs: mySpecs,
|
||||
Username: myUser.UserName,
|
||||
Orgs: orgSimple,
|
||||
Repos: repoSimple,
|
||||
MyOrg: myOrg,
|
||||
SelOrg: selOrg,
|
||||
SelRepo: selRepo,
|
||||
SelModule: selModule,
|
||||
ShowOrg: myOrg != "",
|
||||
ShowGoMod: selModule != "",
|
||||
CommitTime: selCommitTime,
|
||||
CommitHash: selCommitHash,
|
||||
CommitHashShort: selCommitHashShort,
|
||||
ShowSpec: len(mySpecs) > 0,
|
||||
Specs: mySpecs,
|
||||
})
|
||||
if err != nil {
|
||||
fmt.Println("Template execute error:", err)
|
||||
@ -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)
|
||||
}
|
||||
|
||||
func (m *Module) fetchRepos(giteaClient *gitea.Client) {
|
||||
repos, _, err := giteaClient.ListOrgRepos("snow", gitea.ListOrgReposOptions{ListOptions: gitea.ListOptions{Page: 0, PageSize: 100}})
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return
|
||||
}
|
||||
for _, myRepo := range repos {
|
||||
refs, _, err := giteaClient.GetRepoRefs("snow", myRepo.Name, "heads")
|
||||
func (m *Module) specPage(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"]
|
||||
|
||||
q := req.URL.Query()
|
||||
if q.Has("raw") && q.Get("raw") == "true" {
|
||||
open, _, err := giteaClient.GetFile(myOrg, myRepo, mySha, mySpec)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
http.Error(rw, "OpenAPI spec raw: "+err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
fmt.Println(len(refs))
|
||||
for _, myRef := range refs {
|
||||
if myRef.Ref == "refs/heads/"+myRepo.DefaultBranch {
|
||||
fmt.Println(myRef.Ref)
|
||||
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)
|
||||
return
|
||||
}
|
||||
|
||||
tmp, err := template.New("swagger").Parse(swaggerTemplate)
|
||||
if err != nil {
|
||||
fmt.Println("Template parse error:", err)
|
||||
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>
|
||||
{{if .ShowGoMod}}
|
||||
<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}}
|
||||
{{if .ShowSpec}}
|
||||
<div>
|
||||
@ -68,7 +68,7 @@
|
||||
{{if not (eq .Code 200)}}
|
||||
{{.Name}} ({{.Code}})
|
||||
{{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}}
|
||||
</li>
|
||||
{{end}}
|
||||
|
@ -4,16 +4,16 @@
|
||||
<meta charset="UTF-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1">
|
||||
<title>Swagger | Gitea | Melon Tools</title>
|
||||
<link rel="stylesheet" type="text/css" href="swagger/swagger-ui.css" />
|
||||
<link rel="stylesheet" type="text/css" href="swagger/index.css" />
|
||||
<link rel="icon" type="image/png" href="swagger/favicon-32x32.png" sizes="32x32" />
|
||||
<link rel="icon" type="image/png" href="swagger/favicon-16x16.png" sizes="16x16" />
|
||||
<link rel="stylesheet" type="text/css" href="/gitea/swagger/swagger-ui.css" />
|
||||
<link rel="stylesheet" type="text/css" href="/gitea/swagger/index.css" />
|
||||
<link rel="icon" type="image/png" href="/gitea/swagger/favicon-32x32.png" sizes="32x32" />
|
||||
<link rel="icon" type="image/png" href="/gitea/swagger/favicon-16x16.png" sizes="16x16" />
|
||||
</head>
|
||||
<body>
|
||||
<div id="swagger-ui"></div>
|
||||
<script src="swagger/swagger-ui-bundle.js" charset="UTF-8"></script>
|
||||
<script src="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-ui-bundle.js" charset="UTF-8"></script>
|
||||
<script src="/gitea/swagger/swagger-ui-standalone-preset.js" charset="UTF-8"></script>
|
||||
<script src="/gitea/swagger/swagger-initializer-custom.js" charset="UTF-8"></script>
|
||||
<script>
|
||||
window.loadUrls = {{.LoadUrls}};
|
||||
window.loadMain = {{.LoadMain}};
|
||||
|
Loading…
Reference in New Issue
Block a user