Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Swagger.v1.json template #3572

Merged
merged 2 commits into from
Jul 28, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ TAGS ?=

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_DBNAME ?= testgitea
TEST_MYSQL_USERNAME ?= root
Expand Down Expand Up @@ -94,11 +98,12 @@ generate-swagger:
@hash swagger > /dev/null 2>&1; if [ $$? -ne 0 ]; then \
$(GO) get -u github.com/go-swagger/go-swagger/cmd/swagger; \
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
swagger-check: generate-swagger
@diff=$$(git diff public/swagger.v1.json); \
@diff=$$(git diff '$(SWAGGER_SPEC)'); \
if [ -n "$$diff" ]; then \
echo "Please run 'make generate-swagger' and commit the result:"; \
echo "$${diff}"; \
Expand All @@ -110,7 +115,9 @@ swagger-validate:
@hash swagger > /dev/null 2>&1; if [ $$? -ne 0 ]; then \
$(GO) get -u github.com/go-swagger/go-swagger/cmd/swagger; \
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
errcheck:
Expand Down
4 changes: 2 additions & 2 deletions custom/conf/app.ini.sample
Original file line number Diff line number Diff line change
Expand Up @@ -581,8 +581,8 @@ DEFAULT_INTERVAL = 8h
MIN_INTERVAL = 10m

[api]
; Enables /api/swagger, /api/v1/swagger etc. endpoints. True or false; default is true.
ENABLE_SWAGGER_ENDPOINT = true
; Enables Swagger. True or false; default is true.
ENABLE_SWAGGER = true
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not against this change but it will need to be indicated as breaking. So we need to add the label kind/breaking on PR.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we need to add the label kind/breaking on PR

Yup.

; Max number of items in a page
MAX_RESPONSE_ITEMS = 50

Expand Down
1 change: 1 addition & 0 deletions docs/.gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
public/
templates/swagger/v1_json.tmpl
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The file should not be ignored as CI check that the file is valid with the generation (re-generate and check if git detect changes)

Copy link
Contributor Author

@0rzech 0rzech Feb 24, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It has been ignored all the time. This file was moved from public directory, which, as you can see, is ignored too. Also, this is docs/.gitignore and not top level .gitignore.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh sorry, I didn't see that it wasn't top-level ...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No problem.

themes/
2 changes: 1 addition & 1 deletion modules/context/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ func Contexter() macaron.Handler {
ctx.Data["ShowRegistrationButton"] = setting.Service.ShowRegistrationButton
ctx.Data["ShowFooterBranding"] = setting.ShowFooterBranding
ctx.Data["ShowFooterVersion"] = setting.ShowFooterVersion
ctx.Data["EnableSwaggerEndpoint"] = setting.API.EnableSwaggerEndpoint
ctx.Data["EnableSwagger"] = setting.API.EnableSwagger
ctx.Data["EnableOpenIDSignIn"] = setting.Service.EnableOpenIDSignIn

c.Map(ctx)
Expand Down
8 changes: 4 additions & 4 deletions modules/setting/setting.go
Original file line number Diff line number Diff line change
Expand Up @@ -527,11 +527,11 @@ var (

// API settings
API = struct {
EnableSwaggerEndpoint bool
MaxResponseItems int
EnableSwagger bool
MaxResponseItems int
}{
EnableSwaggerEndpoint: true,
MaxResponseItems: 50,
EnableSwagger: true,
MaxResponseItems: 50,
}

U2F = struct {
Expand Down
16 changes: 14 additions & 2 deletions modules/templates/dynamic.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ var (
templates = template.New("")
)

// Renderer implements the macaron handler for serving the templates.
func Renderer() macaron.Handler {
// HTMLRenderer implements the macaron handler for serving HTML templates.
func HTMLRenderer() macaron.Handler {
return macaron.Renderer(macaron.RenderOptions{
Funcs: NewFuncMap(),
Directory: path.Join(setting.StaticRootPath, "templates"),
Expand All @@ -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.
func Mailer() *template.Template {
for _, funcs := range NewFuncMap() {
Expand Down
21 changes: 18 additions & 3 deletions modules/templates/static.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,7 @@ func (templates templateFileSystem) Get(name string) (io.Reader, error) {
return nil, fmt.Errorf("file '%s' not found", name)
}

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

Expand Down Expand Up @@ -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{
Funcs: NewFuncMap(),
TemplateFileSystem: tplFileSys,
})
}

// JSONRenderer implements the macaron handler for serving JSON templates.
func JSONRenderer() macaron.Handler {
return macaron.Renderer(macaron.RenderOptions{
Funcs: NewFuncMap(),
TemplateFileSystem: fs,
TemplateFileSystem: tplFileSys,
HTMLContentType: "application/json",
})
}

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

if setting.API.EnableSwaggerEndpoint {
if setting.API.EnableSwagger {
m.Get("/swagger", misc.Swagger) //Render V1 by default
}

m.Group("/v1", func() {
// Miscellaneous
if setting.API.EnableSwaggerEndpoint {
if setting.API.EnableSwagger {
m.Get("/swagger", misc.Swagger)
}
m.Get("/version", misc.Version)
Expand Down
2 changes: 1 addition & 1 deletion routers/api/v1/misc/swagger.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
)

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

// Swagger render swagger-ui page with v1 json
func Swagger(ctx *context.Context) {
Expand Down
6 changes: 5 additions & 1 deletion routers/routes/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ func NewMacaron() *macaron.Macaron {
},
))

m.Use(templates.Renderer())
m.Use(templates.HTMLRenderer())
models.InitMailRender(templates.Mailer())

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

if setting.API.EnableSwagger {
m.Get("/swagger.v1.json", templates.JSONRenderer(), routers.SwaggerV1Json)
}

m.Group("/api", func() {
apiv1.RegisterRoutes(m)
}, ignSignIn)
Expand Down
14 changes: 14 additions & 0 deletions routers/swagger_json.go
Original file line number Diff line number Diff line change
@@ -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)
}
2 changes: 1 addition & 1 deletion templates/base/footer.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
</div>
</div>
<a href="{{AppSubUrl}}/vendor/librejs.html" data-jslicense="1">JavaScript licenses</a>
{{if .EnableSwaggerEndpoint}}<a href="{{AppSubUrl}}/api/swagger">API</a>{{end}}
{{if .EnableSwagger}}<a href="{{AppSubUrl}}/api/swagger">API</a>{{end}}
<a target="_blank" rel="noopener noreferrer" href="https://gitea.io">{{.i18n.Tr "website"}}</a>
{{if (or .ShowFooterVersion .PageIsAdmin)}}<span class="version">{{GoVer}}</span>{{end}}
</div>
Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion public/swagger.v1.json → templates/swagger/v1_json.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
},
"version": "1.1.1"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you also fill the version with the corresponding var ?

Copy link
Contributor Author

@0rzech 0rzech Jul 28, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, but I think it should be done in another PR, if at all.

},
"basePath": "/api/v1",
"basePath": "{{AppSubUrl}}/api/v1",
"paths": {
"/admin/users": {
"post": {
Expand Down