Turn swagger.v1.json into template
This commit is contained in:
parent
ec43e5619b
commit
0661ed94d5
13
Makefile
13
Makefile
|
@ -42,6 +42,10 @@ TAGS ?=
|
||||||
|
|
||||||
TMPDIR := $(shell mktemp -d 2>/dev/null || mktemp -d -t 'gitea-temp')
|
TMPDIR := $(shell mktemp -d 2>/dev/null || mktemp -d -t 'gitea-temp')
|
||||||
|
|
||||||
|
SWAGGER_SPEC := templates/swagger/v1_json.tmpl
|
||||||
|
SWAGGER_SPEC_S_TMPL := s|"basePath":\s*"/api/v1"|"basePath": "{{AppSubUrl}}/api/v1"|g
|
||||||
|
SWAGGER_SPEC_S_JSON := s|"basePath":\s*"{{AppSubUrl}}/api/v1"|"basePath": "/api/v1"|g
|
||||||
|
|
||||||
TEST_MYSQL_HOST ?= mysql:3306
|
TEST_MYSQL_HOST ?= mysql:3306
|
||||||
TEST_MYSQL_DBNAME ?= testgitea
|
TEST_MYSQL_DBNAME ?= testgitea
|
||||||
TEST_MYSQL_USERNAME ?= root
|
TEST_MYSQL_USERNAME ?= root
|
||||||
|
@ -94,11 +98,12 @@ generate-swagger:
|
||||||
@hash swagger > /dev/null 2>&1; if [ $$? -ne 0 ]; then \
|
@hash swagger > /dev/null 2>&1; if [ $$? -ne 0 ]; then \
|
||||||
$(GO) get -u github.com/go-swagger/go-swagger/cmd/swagger; \
|
$(GO) get -u github.com/go-swagger/go-swagger/cmd/swagger; \
|
||||||
fi
|
fi
|
||||||
swagger generate spec -o ./public/swagger.v1.json
|
swagger generate spec -o './$(SWAGGER_SPEC)'
|
||||||
|
$(SED_INPLACE) '$(SWAGGER_SPEC_S_TMPL)' './$(SWAGGER_SPEC)'
|
||||||
|
|
||||||
.PHONY: swagger-check
|
.PHONY: swagger-check
|
||||||
swagger-check: generate-swagger
|
swagger-check: generate-swagger
|
||||||
@diff=$$(git diff public/swagger.v1.json); \
|
@diff=$$(git diff '$(SWAGGER_SPEC)'); \
|
||||||
if [ -n "$$diff" ]; then \
|
if [ -n "$$diff" ]; then \
|
||||||
echo "Please run 'make generate-swagger' and commit the result:"; \
|
echo "Please run 'make generate-swagger' and commit the result:"; \
|
||||||
echo "$${diff}"; \
|
echo "$${diff}"; \
|
||||||
|
@ -110,7 +115,9 @@ swagger-validate:
|
||||||
@hash swagger > /dev/null 2>&1; if [ $$? -ne 0 ]; then \
|
@hash swagger > /dev/null 2>&1; if [ $$? -ne 0 ]; then \
|
||||||
$(GO) get -u github.com/go-swagger/go-swagger/cmd/swagger; \
|
$(GO) get -u github.com/go-swagger/go-swagger/cmd/swagger; \
|
||||||
fi
|
fi
|
||||||
swagger validate ./public/swagger.v1.json
|
$(SED_INPLACE) '$(SWAGGER_SPEC_S_JSON)' './$(SWAGGER_SPEC)'
|
||||||
|
swagger validate './$(SWAGGER_SPEC)'
|
||||||
|
$(SED_INPLACE) '$(SWAGGER_SPEC_S_TMPL)' './$(SWAGGER_SPEC)'
|
||||||
|
|
||||||
.PHONY: errcheck
|
.PHONY: errcheck
|
||||||
errcheck:
|
errcheck:
|
||||||
|
|
|
@ -579,7 +579,7 @@ DEFAULT_INTERVAL = 8h
|
||||||
MIN_INTERVAL = 10m
|
MIN_INTERVAL = 10m
|
||||||
|
|
||||||
[api]
|
[api]
|
||||||
; Enables /api/swagger, /api/v1/swagger etc. endpoints. True or false; default is true.
|
; Enables Swagger. True or false; default is true.
|
||||||
ENABLE_SWAGGER_ENDPOINT = true
|
ENABLE_SWAGGER_ENDPOINT = true
|
||||||
; Max number of items in a page
|
; Max number of items in a page
|
||||||
MAX_RESPONSE_ITEMS = 50
|
MAX_RESPONSE_ITEMS = 50
|
||||||
|
|
1
docs/.gitignore
vendored
1
docs/.gitignore
vendored
|
@ -1,2 +1,3 @@
|
||||||
public/
|
public/
|
||||||
|
templates/swagger/v1_json.tmpl
|
||||||
themes/
|
themes/
|
||||||
|
|
|
@ -22,8 +22,8 @@ var (
|
||||||
templates = template.New("")
|
templates = template.New("")
|
||||||
)
|
)
|
||||||
|
|
||||||
// Renderer implements the macaron handler for serving the templates.
|
// HTMLRenderer implements the macaron handler for serving HTML templates.
|
||||||
func Renderer() macaron.Handler {
|
func HTMLRenderer() macaron.Handler {
|
||||||
return macaron.Renderer(macaron.RenderOptions{
|
return macaron.Renderer(macaron.RenderOptions{
|
||||||
Funcs: NewFuncMap(),
|
Funcs: NewFuncMap(),
|
||||||
Directory: path.Join(setting.StaticRootPath, "templates"),
|
Directory: path.Join(setting.StaticRootPath, "templates"),
|
||||||
|
@ -33,6 +33,18 @@ func Renderer() macaron.Handler {
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// JSONRenderer implements the macaron handler for serving JSON templates.
|
||||||
|
func JSONRenderer() macaron.Handler {
|
||||||
|
return macaron.Renderer(macaron.RenderOptions{
|
||||||
|
Funcs: NewFuncMap(),
|
||||||
|
Directory: path.Join(setting.StaticRootPath, "templates"),
|
||||||
|
AppendDirectories: []string{
|
||||||
|
path.Join(setting.CustomPath, "templates"),
|
||||||
|
},
|
||||||
|
HTMLContentType: "application/json",
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
// Mailer provides the templates required for sending notification mails.
|
// Mailer provides the templates required for sending notification mails.
|
||||||
func Mailer() *template.Template {
|
func Mailer() *template.Template {
|
||||||
for _, funcs := range NewFuncMap() {
|
for _, funcs := range NewFuncMap() {
|
||||||
|
|
|
@ -43,8 +43,7 @@ func (templates templateFileSystem) Get(name string) (io.Reader, error) {
|
||||||
return nil, fmt.Errorf("file '%s' not found", name)
|
return nil, fmt.Errorf("file '%s' not found", name)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Renderer implements the macaron handler for serving the templates.
|
func NewTemplateFileSystem() templateFileSystem {
|
||||||
func Renderer() macaron.Handler {
|
|
||||||
fs := templateFileSystem{}
|
fs := templateFileSystem{}
|
||||||
fs.files = make([]macaron.TemplateFile, 0, 10)
|
fs.files = make([]macaron.TemplateFile, 0, 10)
|
||||||
|
|
||||||
|
@ -110,9 +109,25 @@ func Renderer() macaron.Handler {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return fs
|
||||||
|
}
|
||||||
|
|
||||||
|
var tplFileSys = NewTemplateFileSystem()
|
||||||
|
|
||||||
|
// HTMLRenderer implements the macaron handler for serving HTML templates.
|
||||||
|
func HTMLRenderer() macaron.Handler {
|
||||||
return macaron.Renderer(macaron.RenderOptions{
|
return macaron.Renderer(macaron.RenderOptions{
|
||||||
Funcs: NewFuncMap(),
|
Funcs: NewFuncMap(),
|
||||||
TemplateFileSystem: fs,
|
TemplateFileSystem: tplFileSys,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
// JSONRenderer implements the macaron handler for serving JSON templates.
|
||||||
|
func JSONRenderer() macaron.Handler {
|
||||||
|
return macaron.Renderer(macaron.RenderOptions{
|
||||||
|
Funcs: NewFuncMap(),
|
||||||
|
TemplateFileSystem: tplFileSys,
|
||||||
|
HTMLContentType: "application/json",
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -10,7 +10,7 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
// tplSwagger swagger page template
|
// tplSwagger swagger page template
|
||||||
const tplSwagger base.TplName = "swagger"
|
const tplSwagger base.TplName = "swagger/ui"
|
||||||
|
|
||||||
// Swagger render swagger-ui page with v1 json
|
// Swagger render swagger-ui page with v1 json
|
||||||
func Swagger(ctx *context.Context) {
|
func Swagger(ctx *context.Context) {
|
||||||
|
|
|
@ -79,7 +79,7 @@ func NewMacaron() *macaron.Macaron {
|
||||||
},
|
},
|
||||||
))
|
))
|
||||||
|
|
||||||
m.Use(templates.Renderer())
|
m.Use(templates.HTMLRenderer())
|
||||||
models.InitMailRender(templates.Mailer())
|
models.InitMailRender(templates.Mailer())
|
||||||
|
|
||||||
localeNames, err := options.Dir("locale")
|
localeNames, err := options.Dir("locale")
|
||||||
|
@ -755,6 +755,10 @@ func RegisterRoutes(m *macaron.Macaron) {
|
||||||
m.Post("/purge", user.NotificationPurgePost)
|
m.Post("/purge", user.NotificationPurgePost)
|
||||||
}, reqSignIn)
|
}, reqSignIn)
|
||||||
|
|
||||||
|
if setting.API.EnableSwaggerEndpoint {
|
||||||
|
m.Get("/swagger.v1.json", templates.JSONRenderer(), routers.SwaggerV1Json)
|
||||||
|
}
|
||||||
|
|
||||||
m.Group("/api", func() {
|
m.Group("/api", func() {
|
||||||
apiv1.RegisterRoutes(m)
|
apiv1.RegisterRoutes(m)
|
||||||
}, ignSignIn)
|
}, ignSignIn)
|
||||||
|
|
14
routers/swagger_json.go
Normal file
14
routers/swagger_json.go
Normal file
|
@ -0,0 +1,14 @@
|
||||||
|
package routers
|
||||||
|
|
||||||
|
import (
|
||||||
|
"code.gitea.io/gitea/modules/base"
|
||||||
|
"code.gitea.io/gitea/modules/context"
|
||||||
|
)
|
||||||
|
|
||||||
|
// tplSwaggerV1Json swagger v1 json template
|
||||||
|
const tplSwaggerV1Json base.TplName = "swagger/v1_json"
|
||||||
|
|
||||||
|
// SwaggerV1Json render swagger v1 json
|
||||||
|
func SwaggerV1Json(ctx *context.Context) {
|
||||||
|
ctx.HTML(200, tplSwaggerV1Json)
|
||||||
|
}
|
|
@ -21,7 +21,7 @@
|
||||||
},
|
},
|
||||||
"version": "1.1.1"
|
"version": "1.1.1"
|
||||||
},
|
},
|
||||||
"basePath": "/api/v1",
|
"basePath": "{{AppSubUrl}}/api/v1",
|
||||||
"paths": {
|
"paths": {
|
||||||
"/admin/users": {
|
"/admin/users": {
|
||||||
"post": {
|
"post": {
|
Loading…
Reference in New Issue
Block a user