Skip to content

Commit

Permalink
Move swagger-ui under /api/v1 (go-gitea#2746)
Browse files Browse the repository at this point in the history
* Move swagger interface under /api/v1

* Update swagger-ui

* Add /api/swagger and prepare for multiple api version

* Update test links

* Fix footer link
  • Loading branch information
sapk authored and lunny committed Oct 21, 2017
1 parent bc8d726 commit 619b9b5
Show file tree
Hide file tree
Showing 17 changed files with 166 additions and 192 deletions.
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -304,10 +304,10 @@ generate-stylesheets:
.PHONY: swagger-ui
swagger-ui:
rm -Rf public/vendor/assets/swagger-ui
git clone --depth=10 -b v3.0.7 --single-branch https://github.com/swagger-api/swagger-ui.git $(TMPDIR)/swagger-ui
git clone --depth=10 -b v3.3.2 --single-branch https://github.com/swagger-api/swagger-ui.git $(TMPDIR)/swagger-ui
mv $(TMPDIR)/swagger-ui/dist public/vendor/assets/swagger-ui
rm -Rf $(TMPDIR)/swagger-ui
$(SED_INPLACE) "s;http://petstore.swagger.io/v2/swagger.json;../../swagger.v1.json;g" public/assets/swagger-ui/index.html
$(SED_INPLACE) "s;http://petstore.swagger.io/v2/swagger.json;../../../swagger.v1.json;g" public/vendor/assets/swagger-ui/index.html

.PHONY: update-translations
update-translations:
Expand Down
6 changes: 4 additions & 2 deletions integrations/links_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ func TestLinksNoLogin(t *testing.T) {
"/user/sign_up",
"/user/login",
"/user/forgot_password",
"/swagger",
"/api/swagger",
"/api/v1/swagger",
// TODO: follow this page and test every link
"/vendor/librejs.html",
}
Expand All @@ -47,7 +48,8 @@ func testLinksAsUser(userName string, t *testing.T) {
"/explore/organizations?q=test&tab=",
"/",
"/user/forgot_password",
"/swagger",
"/api/swagger",
"/api/v1/swagger",
"/issues",
"/issues?type=your_repositories&repo=0&sort=&state=open",
"/issues?type=assigned&repo=0&sort=&state=open",
Expand Down
12 changes: 7 additions & 5 deletions public/vendor/assets/swagger-ui/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@
<style>
html
{
box-sizing: border-box;
overflow: -moz-scrollbars-vertical;
overflow-y: scroll;
box-sizing: border-box;
overflow: -moz-scrollbars-vertical;
overflow-y: scroll;
}
*,
*:before,
*:after
{
box-sizing: inherit;
box-sizing: inherit;
}

body {
Expand Down Expand Up @@ -71,10 +71,12 @@
<script src="./swagger-ui-standalone-preset.js"> </script>
<script>
window.onload = function() {

// Build a system
const ui = SwaggerUIBundle({
url: "../../swagger.v1.json",
url: "../../../swagger.v1.json",
dom_id: '#swagger-ui',
deepLinking: true,
presets: [
SwaggerUIBundle.presets.apis,
SwaggerUIStandalonePreset
Expand Down
54 changes: 14 additions & 40 deletions public/vendor/assets/swagger-ui/oauth2-redirect.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,18 @@
function run () {
var oauth2 = window.opener.swaggerUIRedirectOauth2;
var sentState = oauth2.state;
var isValid, qp;
var redirectUrl = oauth2.redirectUrl;
var isValid, qp, arr;

qp = (window.location.hash || location.search).substring(1);
if (/code|token|error/.test(window.location.hash)) {
qp = window.location.hash.substring(1);
} else {
qp = location.search.substring(1);
}

qp = qp ? JSON.parse('{"' + qp.replace(/&/g, '","').replace(/=/g, '":"') + '"}',
arr = qp.split("&")
arr.forEach(function (v,i,_arr) { _arr[i] = '"' + v.replace('=', '":"') + '"';})
qp = qp ? JSON.parse('{' + arr.join() + '}',
function (key, value) {
return key === "" ? value : decodeURIComponent(value)
}
Expand All @@ -33,51 +40,18 @@
if (qp.code) {
delete oauth2.state;
oauth2.auth.code = qp.code;
createForm(oauth2.auth, qp).submit();
oauth2.callback({auth: oauth2.auth, redirectUrl: redirectUrl});
} else {
oauth2.errCb({
authId: oauth2.auth.name,
source: "auth",
level: "error",
message: "Authorization failed: no accessCode came from the server"
message: "Authorization failed: no accessCode received from the server"
});
window.close();
}
} else {
oauth2.callback({auth: oauth2.auth, token: qp, isValid: isValid});
window.close();
oauth2.callback({auth: oauth2.auth, token: qp, isValid: isValid, redirectUrl: redirectUrl});
}
window.close();
}

function createForm(auth, qp) {
var form = document.createElement("form");
var schema = auth.schema;
var action = schema.get("tokenUrl");
var name, input;

var fields = {
code: qp.code,
"redirect_uri": location.protocol + "//" + location.host + location.pathname,
"grant_type": "authorization_code",
"client_secret": auth.clientSecret,
"client_id": auth.clientId
}

for ( name in fields ) {
input = document.createElement("input");
input.name = name;
input.value = fields[name];
input.type = "hidden";
form.appendChild(input);
}


form.method = "POST";
form.action = action;

document.body.appendChild(form);

return form;
}

</script>
190 changes: 86 additions & 104 deletions public/vendor/assets/swagger-ui/swagger-ui-bundle.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion public/vendor/assets/swagger-ui/swagger-ui-bundle.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 12 additions & 7 deletions public/vendor/assets/swagger-ui/swagger-ui-standalone-preset.js

Large diffs are not rendered by default.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion public/vendor/assets/swagger-ui/swagger-ui.css

Large diffs are not rendered by default.

21 changes: 7 additions & 14 deletions public/vendor/assets/swagger-ui/swagger-ui.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion public/vendor/assets/swagger-ui/swagger-ui.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions routers/api/v1/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -275,8 +275,11 @@ func mustAllowPulls(ctx *context.Context) {
func RegisterRoutes(m *macaron.Macaron) {
bind := binding.Bind

m.Get("/swagger", misc.Swagger) //Render V1 by default

m.Group("/v1", func() {
// Miscellaneous
m.Get("/swagger", misc.Swagger)
m.Get("/version", misc.Version)
m.Post("/markdown", bind(api.MarkdownOption{}), misc.Markdown)
m.Post("/markdown/raw", misc.MarkdownRaw)
Expand Down
19 changes: 19 additions & 0 deletions routers/api/v1/misc/swagger.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// Copyright 2017 The Gitea Authors. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.

package misc

import (
"code.gitea.io/gitea/modules/base"
"code.gitea.io/gitea/modules/context"
)

// tplSwagger swagger page template
const tplSwagger base.TplName = "swagger"

// Swagger render swagger-ui page with v1 json
func Swagger(ctx *context.Context) {
ctx.Data["APIJSONVersion"] = "v1"
ctx.HTML(200, tplSwagger)
}
7 changes: 0 additions & 7 deletions routers/home.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@ import (
const (
// tplHome home page template
tplHome base.TplName = "home"
// tplSwagger swagger page template
tplSwagger base.TplName = "swagger"
// tplExploreRepos explore repositories page template
tplExploreRepos base.TplName = "explore/repos"
// tplExploreUsers explore users page template
Expand Down Expand Up @@ -53,11 +51,6 @@ func Home(ctx *context.Context) {
ctx.HTML(200, tplHome)
}

// Swagger render swagger-ui page
func Swagger(ctx *context.Context) {
ctx.HTML(200, tplSwagger)
}

// RepoSearchOptions when calling search repositories
type RepoSearchOptions struct {
OwnerID int64
Expand Down
1 change: 0 additions & 1 deletion routers/routes/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,6 @@ func RegisterRoutes(m *macaron.Macaron) {
return ""
})
m.Get("/", ignSignIn, routers.Home)
m.Get("/swagger", ignSignIn, routers.Swagger)
m.Group("/explore", func() {
m.Get("", func(ctx *context.Context) {
ctx.Redirect(setting.AppSubURL + "/explore/repos")
Expand Down
2 changes: 1 addition & 1 deletion templates/base/footer.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
</div>
</div>
<a href="{{AppSubUrl}}/vendor/librejs.html" data-jslicense="1">Javascript licenses</a>
<a href="{{AppSubUrl}}/swagger">API</a>
<a href="{{AppSubUrl}}/api/swagger">API</a>
<a target="_blank" rel="noopener" href="https://gitea.io">{{.i18n.Tr "website"}}</a>
{{if (or .ShowFooterVersion .PageIsAdmin)}}<span class="version">{{GoVer}}</span>{{end}}
</div>
Expand Down
12 changes: 7 additions & 5 deletions templates/swagger.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@
<style>
html
{
box-sizing: border-box;
overflow: -moz-scrollbars-vertical;
overflow-y: scroll;
box-sizing: border-box;
overflow: -moz-scrollbars-vertical;
overflow-y: scroll;
}
*,
*:before,
*:after
{
box-sizing: inherit;
box-sizing: inherit;
}

body {
Expand Down Expand Up @@ -70,11 +70,13 @@
<script src="{{AppSubUrl}}/vendor/assets/swagger-ui/swagger-ui-bundle.js"> </script>
<script src="{{AppSubUrl}}/vendor/assets/swagger-ui/swagger-ui-standalone-preset.js"> </script>
<script>

window.onload = function() {
// Build a system
const ui = SwaggerUIBundle({
url: "{{AppUrl}}swagger.v1.json",
url: "{{AppUrl}}swagger.{{.APIJSONVersion}}.json",
dom_id: '#swagger-ui',
deepLinking: true,
presets: [
SwaggerUIBundle.presets.apis,
SwaggerUIStandalonePreset
Expand Down

0 comments on commit 619b9b5

Please sign in to comment.