Skip to content

Commit a74426d

Browse files
0rzechlafriks
authored andcommitted
Swagger.v1.json template (go-gitea#3572)
* Turn swagger.v1.json into template * Rename ENABLE_SWAGGER_ENDPOINT option to ENABLE_SWAGGER
1 parent 412583a commit a74426d

File tree

14 files changed

+74
-21
lines changed

14 files changed

+74
-21
lines changed

Makefile

+10-3
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,10 @@ TAGS ?=
4242

4343
TMPDIR := $(shell mktemp -d 2>/dev/null || mktemp -d -t 'gitea-temp')
4444

45+
SWAGGER_SPEC := templates/swagger/v1_json.tmpl
46+
SWAGGER_SPEC_S_TMPL := s|"basePath":\s*"/api/v1"|"basePath": "{{AppSubUrl}}/api/v1"|g
47+
SWAGGER_SPEC_S_JSON := s|"basePath":\s*"{{AppSubUrl}}/api/v1"|"basePath": "/api/v1"|g
48+
4549
TEST_MYSQL_HOST ?= mysql:3306
4650
TEST_MYSQL_DBNAME ?= testgitea
4751
TEST_MYSQL_USERNAME ?= root
@@ -94,11 +98,12 @@ generate-swagger:
9498
@hash swagger > /dev/null 2>&1; if [ $$? -ne 0 ]; then \
9599
$(GO) get -u github.com/go-swagger/go-swagger/cmd/swagger; \
96100
fi
97-
swagger generate spec -o ./public/swagger.v1.json
101+
swagger generate spec -o './$(SWAGGER_SPEC)'
102+
$(SED_INPLACE) '$(SWAGGER_SPEC_S_TMPL)' './$(SWAGGER_SPEC)'
98103

99104
.PHONY: swagger-check
100105
swagger-check: generate-swagger
101-
@diff=$$(git diff public/swagger.v1.json); \
106+
@diff=$$(git diff '$(SWAGGER_SPEC)'); \
102107
if [ -n "$$diff" ]; then \
103108
echo "Please run 'make generate-swagger' and commit the result:"; \
104109
echo "$${diff}"; \
@@ -110,7 +115,9 @@ swagger-validate:
110115
@hash swagger > /dev/null 2>&1; if [ $$? -ne 0 ]; then \
111116
$(GO) get -u github.com/go-swagger/go-swagger/cmd/swagger; \
112117
fi
113-
swagger validate ./public/swagger.v1.json
118+
$(SED_INPLACE) '$(SWAGGER_SPEC_S_JSON)' './$(SWAGGER_SPEC)'
119+
swagger validate './$(SWAGGER_SPEC)'
120+
$(SED_INPLACE) '$(SWAGGER_SPEC_S_TMPL)' './$(SWAGGER_SPEC)'
114121

115122
.PHONY: errcheck
116123
errcheck:

custom/conf/app.ini.sample

+2-2
Original file line numberDiff line numberDiff line change
@@ -581,8 +581,8 @@ DEFAULT_INTERVAL = 8h
581581
MIN_INTERVAL = 10m
582582

583583
[api]
584-
; Enables /api/swagger, /api/v1/swagger etc. endpoints. True or false; default is true.
585-
ENABLE_SWAGGER_ENDPOINT = true
584+
; Enables Swagger. True or false; default is true.
585+
ENABLE_SWAGGER = true
586586
; Max number of items in a page
587587
MAX_RESPONSE_ITEMS = 50
588588

docs/.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
public/
2+
templates/swagger/v1_json.tmpl
23
themes/

modules/context/context.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,7 @@ func Contexter() macaron.Handler {
264264
ctx.Data["ShowRegistrationButton"] = setting.Service.ShowRegistrationButton
265265
ctx.Data["ShowFooterBranding"] = setting.ShowFooterBranding
266266
ctx.Data["ShowFooterVersion"] = setting.ShowFooterVersion
267-
ctx.Data["EnableSwaggerEndpoint"] = setting.API.EnableSwaggerEndpoint
267+
ctx.Data["EnableSwagger"] = setting.API.EnableSwagger
268268
ctx.Data["EnableOpenIDSignIn"] = setting.Service.EnableOpenIDSignIn
269269

270270
c.Map(ctx)

modules/setting/setting.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -527,11 +527,11 @@ var (
527527

528528
// API settings
529529
API = struct {
530-
EnableSwaggerEndpoint bool
531-
MaxResponseItems int
530+
EnableSwagger bool
531+
MaxResponseItems int
532532
}{
533-
EnableSwaggerEndpoint: true,
534-
MaxResponseItems: 50,
533+
EnableSwagger: true,
534+
MaxResponseItems: 50,
535535
}
536536

537537
U2F = struct {

modules/templates/dynamic.go

+14-2
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ var (
2222
templates = template.New("")
2323
)
2424

25-
// Renderer implements the macaron handler for serving the templates.
26-
func Renderer() macaron.Handler {
25+
// HTMLRenderer implements the macaron handler for serving HTML templates.
26+
func HTMLRenderer() macaron.Handler {
2727
return macaron.Renderer(macaron.RenderOptions{
2828
Funcs: NewFuncMap(),
2929
Directory: path.Join(setting.StaticRootPath, "templates"),
@@ -33,6 +33,18 @@ func Renderer() macaron.Handler {
3333
})
3434
}
3535

36+
// JSONRenderer implements the macaron handler for serving JSON templates.
37+
func JSONRenderer() macaron.Handler {
38+
return macaron.Renderer(macaron.RenderOptions{
39+
Funcs: NewFuncMap(),
40+
Directory: path.Join(setting.StaticRootPath, "templates"),
41+
AppendDirectories: []string{
42+
path.Join(setting.CustomPath, "templates"),
43+
},
44+
HTMLContentType: "application/json",
45+
})
46+
}
47+
3648
// Mailer provides the templates required for sending notification mails.
3749
func Mailer() *template.Template {
3850
for _, funcs := range NewFuncMap() {

modules/templates/static.go

+18-3
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,7 @@ func (templates templateFileSystem) Get(name string) (io.Reader, error) {
4343
return nil, fmt.Errorf("file '%s' not found", name)
4444
}
4545

46-
// Renderer implements the macaron handler for serving the templates.
47-
func Renderer() macaron.Handler {
46+
func NewTemplateFileSystem() templateFileSystem {
4847
fs := templateFileSystem{}
4948
fs.files = make([]macaron.TemplateFile, 0, 10)
5049

@@ -110,9 +109,25 @@ func Renderer() macaron.Handler {
110109
}
111110
}
112111

112+
return fs
113+
}
114+
115+
var tplFileSys = NewTemplateFileSystem()
116+
117+
// HTMLRenderer implements the macaron handler for serving HTML templates.
118+
func HTMLRenderer() macaron.Handler {
119+
return macaron.Renderer(macaron.RenderOptions{
120+
Funcs: NewFuncMap(),
121+
TemplateFileSystem: tplFileSys,
122+
})
123+
}
124+
125+
// JSONRenderer implements the macaron handler for serving JSON templates.
126+
func JSONRenderer() macaron.Handler {
113127
return macaron.Renderer(macaron.RenderOptions{
114128
Funcs: NewFuncMap(),
115-
TemplateFileSystem: fs,
129+
TemplateFileSystem: tplFileSys,
130+
HTMLContentType: "application/json",
116131
})
117132
}
118133

routers/api/v1/api.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -278,13 +278,13 @@ func mustAllowPulls(ctx *context.Context) {
278278
func RegisterRoutes(m *macaron.Macaron) {
279279
bind := binding.Bind
280280

281-
if setting.API.EnableSwaggerEndpoint {
281+
if setting.API.EnableSwagger {
282282
m.Get("/swagger", misc.Swagger) //Render V1 by default
283283
}
284284

285285
m.Group("/v1", func() {
286286
// Miscellaneous
287-
if setting.API.EnableSwaggerEndpoint {
287+
if setting.API.EnableSwagger {
288288
m.Get("/swagger", misc.Swagger)
289289
}
290290
m.Get("/version", misc.Version)

routers/api/v1/misc/swagger.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import (
1010
)
1111

1212
// tplSwagger swagger page template
13-
const tplSwagger base.TplName = "swagger"
13+
const tplSwagger base.TplName = "swagger/ui"
1414

1515
// Swagger render swagger-ui page with v1 json
1616
func Swagger(ctx *context.Context) {

routers/routes/routes.go

+5-1
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ func NewMacaron() *macaron.Macaron {
7979
},
8080
))
8181

82-
m.Use(templates.Renderer())
82+
m.Use(templates.HTMLRenderer())
8383
models.InitMailRender(templates.Mailer())
8484

8585
localeNames, err := options.Dir("locale")
@@ -755,6 +755,10 @@ func RegisterRoutes(m *macaron.Macaron) {
755755
m.Post("/purge", user.NotificationPurgePost)
756756
}, reqSignIn)
757757

758+
if setting.API.EnableSwagger {
759+
m.Get("/swagger.v1.json", templates.JSONRenderer(), routers.SwaggerV1Json)
760+
}
761+
758762
m.Group("/api", func() {
759763
apiv1.RegisterRoutes(m)
760764
}, ignSignIn)

routers/swagger_json.go

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package routers
2+
3+
import (
4+
"code.gitea.io/gitea/modules/base"
5+
"code.gitea.io/gitea/modules/context"
6+
)
7+
8+
// tplSwaggerV1Json swagger v1 json template
9+
const tplSwaggerV1Json base.TplName = "swagger/v1_json"
10+
11+
// SwaggerV1Json render swagger v1 json
12+
func SwaggerV1Json(ctx *context.Context) {
13+
ctx.HTML(200, tplSwaggerV1Json)
14+
}

templates/base/footer.tmpl

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
</div>
3030
</div>
3131
<a href="{{AppSubUrl}}/vendor/librejs.html" data-jslicense="1">JavaScript licenses</a>
32-
{{if .EnableSwaggerEndpoint}}<a href="{{AppSubUrl}}/api/swagger">API</a>{{end}}
32+
{{if .EnableSwagger}}<a href="{{AppSubUrl}}/api/swagger">API</a>{{end}}
3333
<a target="_blank" rel="noopener noreferrer" href="https://gitea.io">{{.i18n.Tr "website"}}</a>
3434
{{if (or .ShowFooterVersion .PageIsAdmin)}}<span class="version">{{GoVer}}</span>{{end}}
3535
</div>
File renamed without changes.

public/swagger.v1.json templates/swagger/v1_json.tmpl

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
},
2222
"version": "1.1.1"
2323
},
24-
"basePath": "/api/v1",
24+
"basePath": "{{AppSubUrl}}/api/v1",
2525
"paths": {
2626
"/admin/users": {
2727
"post": {

0 commit comments

Comments
 (0)