THIS IS A TEST INSTANCE ONLY! REPOSITORIES CAN BE DELETED AT ANY TIME!

Browse Source

Swagger.v1.json template (#3572)

* Turn swagger.v1.json into template

* Rename ENABLE_SWAGGER_ENDPOINT option to ENABLE_SWAGGER
pull/4531/head
Piotr Orzechowski 7 months ago
parent
commit
a74426d631

+ 10
- 3
Makefile View File

@@ -42,6 +42,10 @@ TAGS ?=
42 42
 
43 43
 TMPDIR := $(shell mktemp -d 2>/dev/null || mktemp -d -t 'gitea-temp')
44 44
 
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
+
45 49
 TEST_MYSQL_HOST ?= mysql:3306
46 50
 TEST_MYSQL_DBNAME ?= testgitea
47 51
 TEST_MYSQL_USERNAME ?= root
@@ -94,11 +98,12 @@ generate-swagger:
94 98
 	@hash swagger > /dev/null 2>&1; if [ $$? -ne 0 ]; then \
95 99
 		$(GO) get -u github.com/go-swagger/go-swagger/cmd/swagger; \
96 100
 	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)'
98 103
 
99 104
 .PHONY: swagger-check
100 105
 swagger-check: generate-swagger
101
-	@diff=$$(git diff public/swagger.v1.json); \
106
+	@diff=$$(git diff '$(SWAGGER_SPEC)'); \
102 107
 	if [ -n "$$diff" ]; then \
103 108
 		echo "Please run 'make generate-swagger' and commit the result:"; \
104 109
 		echo "$${diff}"; \
@@ -110,7 +115,9 @@ swagger-validate:
110 115
 	@hash swagger > /dev/null 2>&1; if [ $$? -ne 0 ]; then \
111 116
 		$(GO) get -u github.com/go-swagger/go-swagger/cmd/swagger; \
112 117
 	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)'
114 121
 
115 122
 .PHONY: errcheck
116 123
 errcheck:

+ 2
- 2
custom/conf/app.ini.sample View File

@@ -581,8 +581,8 @@ DEFAULT_INTERVAL = 8h
581 581
 MIN_INTERVAL = 10m
582 582
 
583 583
 [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
586 586
 ; Max number of items in a page
587 587
 MAX_RESPONSE_ITEMS = 50
588 588
 

+ 1
- 0
docs/.gitignore View File

@@ -1,2 +1,3 @@
1 1
 public/
2
+templates/swagger/v1_json.tmpl
2 3
 themes/

+ 1
- 1
modules/context/context.go View File

@@ -264,7 +264,7 @@ func Contexter() macaron.Handler {
264 264
 		ctx.Data["ShowRegistrationButton"] = setting.Service.ShowRegistrationButton
265 265
 		ctx.Data["ShowFooterBranding"] = setting.ShowFooterBranding
266 266
 		ctx.Data["ShowFooterVersion"] = setting.ShowFooterVersion
267
-		ctx.Data["EnableSwaggerEndpoint"] = setting.API.EnableSwaggerEndpoint
267
+		ctx.Data["EnableSwagger"] = setting.API.EnableSwagger
268 268
 		ctx.Data["EnableOpenIDSignIn"] = setting.Service.EnableOpenIDSignIn
269 269
 
270 270
 		c.Map(ctx)

+ 4
- 4
modules/setting/setting.go View File

@@ -527,11 +527,11 @@ var (
527 527
 
528 528
 	// API settings
529 529
 	API = struct {
530
-		EnableSwaggerEndpoint bool
531
-		MaxResponseItems      int
530
+		EnableSwagger    bool
531
+		MaxResponseItems int
532 532
 	}{
533
-		EnableSwaggerEndpoint: true,
534
-		MaxResponseItems:      50,
533
+		EnableSwagger:    true,
534
+		MaxResponseItems: 50,
535 535
 	}
536 536
 
537 537
 	U2F = struct {

+ 14
- 2
modules/templates/dynamic.go View File

@@ -22,8 +22,8 @@ var (
22 22
 	templates = template.New("")
23 23
 )
24 24
 
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 {
27 27
 	return macaron.Renderer(macaron.RenderOptions{
28 28
 		Funcs:     NewFuncMap(),
29 29
 		Directory: path.Join(setting.StaticRootPath, "templates"),
@@ -33,6 +33,18 @@ func Renderer() macaron.Handler {
33 33
 	})
34 34
 }
35 35
 
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
+
36 48
 // Mailer provides the templates required for sending notification mails.
37 49
 func Mailer() *template.Template {
38 50
 	for _, funcs := range NewFuncMap() {

+ 18
- 3
modules/templates/static.go View File

@@ -43,8 +43,7 @@ func (templates templateFileSystem) Get(name string) (io.Reader, error) {
43 43
 	return nil, fmt.Errorf("file '%s' not found", name)
44 44
 }
45 45
 
46
-// Renderer implements the macaron handler for serving the templates.
47
-func Renderer() macaron.Handler {
46
+func NewTemplateFileSystem() templateFileSystem {
48 47
 	fs := templateFileSystem{}
49 48
 	fs.files = make([]macaron.TemplateFile, 0, 10)
50 49
 
@@ -110,9 +109,25 @@ func Renderer() macaron.Handler {
110 109
 		}
111 110
 	}
112 111
 
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 {
113 127
 	return macaron.Renderer(macaron.RenderOptions{
114 128
 		Funcs:              NewFuncMap(),
115
-		TemplateFileSystem: fs,
129
+		TemplateFileSystem: tplFileSys,
130
+		HTMLContentType:    "application/json",
116 131
 	})
117 132
 }
118 133
 

+ 2
- 2
routers/api/v1/api.go View File

@@ -278,13 +278,13 @@ func mustAllowPulls(ctx *context.Context) {
278 278
 func RegisterRoutes(m *macaron.Macaron) {
279 279
 	bind := binding.Bind
280 280
 
281
-	if setting.API.EnableSwaggerEndpoint {
281
+	if setting.API.EnableSwagger {
282 282
 		m.Get("/swagger", misc.Swagger) //Render V1 by default
283 283
 	}
284 284
 
285 285
 	m.Group("/v1", func() {
286 286
 		// Miscellaneous
287
-		if setting.API.EnableSwaggerEndpoint {
287
+		if setting.API.EnableSwagger {
288 288
 			m.Get("/swagger", misc.Swagger)
289 289
 		}
290 290
 		m.Get("/version", misc.Version)

+ 1
- 1
routers/api/v1/misc/swagger.go View File

@@ -10,7 +10,7 @@ import (
10 10
 )
11 11
 
12 12
 // tplSwagger swagger page template
13
-const tplSwagger base.TplName = "swagger"
13
+const tplSwagger base.TplName = "swagger/ui"
14 14
 
15 15
 // Swagger render swagger-ui page with v1 json
16 16
 func Swagger(ctx *context.Context) {

+ 5
- 1
routers/routes/routes.go View File

@@ -79,7 +79,7 @@ func NewMacaron() *macaron.Macaron {
79 79
 		},
80 80
 	))
81 81
 
82
-	m.Use(templates.Renderer())
82
+	m.Use(templates.HTMLRenderer())
83 83
 	models.InitMailRender(templates.Mailer())
84 84
 
85 85
 	localeNames, err := options.Dir("locale")
@@ -755,6 +755,10 @@ func RegisterRoutes(m *macaron.Macaron) {
755 755
 		m.Post("/purge", user.NotificationPurgePost)
756 756
 	}, reqSignIn)
757 757
 
758
+	if setting.API.EnableSwagger {
759
+		m.Get("/swagger.v1.json", templates.JSONRenderer(), routers.SwaggerV1Json)
760
+	}
761
+
758 762
 	m.Group("/api", func() {
759 763
 		apiv1.RegisterRoutes(m)
760 764
 	}, ignSignIn)

+ 14
- 0
routers/swagger_json.go View File

@@ -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
+}

+ 1
- 1
templates/base/footer.tmpl View File

@@ -29,7 +29,7 @@
29 29
 					</div>
30 30
 				</div>
31 31
 				<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}}
33 33
 				<a target="_blank" rel="noopener noreferrer" href="https://gitea.io">{{.i18n.Tr "website"}}</a>
34 34
 				{{if (or .ShowFooterVersion .PageIsAdmin)}}<span class="version">{{GoVer}}</span>{{end}}
35 35
 			</div>

templates/swagger.tmpl → templates/swagger/ui.tmpl View File


public/swagger.v1.json → templates/swagger/v1_json.tmpl View File

@@ -21,7 +21,7 @@
21 21
     },
22 22
     "version": "1.1.1"
23 23
   },
24
-  "basePath": "/api/v1",
24
+  "basePath": "{{AppSubUrl}}/api/v1",
25 25
   "paths": {
26 26
     "/admin/users": {
27 27
       "post": {

Loading…
Cancel
Save