fix working example/basic/api function (#123)

This commit is contained in:
Anton Dzyk
2020-08-03 14:17:43 +03:00
committed by GitHub
parent bbeaf5b73c
commit 4199ce8e17
4 changed files with 75 additions and 33 deletions
+62 -31
View File
@@ -1,18 +1,23 @@
// GENERATED BY THE COMMAND ABOVE; DO NOT EDIT // GENERATED BY THE COMMAND ABOVE; DO NOT EDIT
// This file was generated by swaggo/swag at // This file was generated by swaggo/swag
// 2017-06-25 01:25:37.872454531 +0800 CST
package docs package docs
import ( import (
"bytes"
"encoding/json"
"strings"
"github.com/alecthomas/template"
"github.com/swaggo/swag" "github.com/swaggo/swag"
) )
var doc = `{ var doc = `{
"schemes": {{ marshal .Schemes }},
"swagger": "2.0", "swagger": "2.0",
"info": { "info": {
"description": "This is a sample server Petstore server.", "description": "{{.Description}}",
"title": "Swagger Example API", "title": "{{.Title}}",
"termsOfService": "http://swagger.io/terms/", "termsOfService": "http://swagger.io/terms/",
"contact": { "contact": {
"name": "API Support", "name": "API Support",
@@ -23,10 +28,10 @@ var doc = `{
"name": "Apache 2.0", "name": "Apache 2.0",
"url": "http://www.apache.org/licenses/LICENSE-2.0.html" "url": "http://www.apache.org/licenses/LICENSE-2.0.html"
}, },
"version": "1.0" "version": "{{.Version}}"
}, },
"host": "petstore.swagger.io", "host": "{{.Host}}",
"basePath": "/v2", "basePath": "{{.BasePath}}",
"paths": { "paths": {
"/testapi/get-string-by-int/{some_id}": { "/testapi/get-string-by-int/{some_id}": {
"get": { "get": {
@@ -40,13 +45,11 @@ var doc = `{
"summary": "Add a new pet to the store", "summary": "Add a new pet to the store",
"parameters": [ "parameters": [
{ {
"type": "integer",
"description": "Some ID", "description": "Some ID",
"name": "some_id", "name": "some_id",
"in": "path", "in": "path",
"required": true, "required": true
"schema": {
"type": "int"
}
} }
], ],
"responses": { "responses": {
@@ -59,14 +62,12 @@ var doc = `{
"400": { "400": {
"description": "We need ID!!", "description": "We need ID!!",
"schema": { "schema": {
"type": "object",
"$ref": "#/definitions/web.APIError" "$ref": "#/definitions/web.APIError"
} }
}, },
"404": { "404": {
"description": "Can not find ID", "description": "Can not find ID",
"schema": { "schema": {
"type": "object",
"$ref": "#/definitions/web.APIError" "$ref": "#/definitions/web.APIError"
} }
} }
@@ -84,31 +85,25 @@ var doc = `{
], ],
"parameters": [ "parameters": [
{ {
"type": "string",
"description": "Some ID", "description": "Some ID",
"name": "some_id", "name": "some_id",
"in": "path", "in": "path",
"required": true, "required": true
"schema": {
"type": "string"
}
}, },
{ {
"type": "integer",
"description": "Offset", "description": "Offset",
"name": "offset", "name": "offset",
"in": "query", "in": "query",
"required": true, "required": true
"schema": {
"type": "int"
}
}, },
{ {
"type": "integer",
"description": "Offset", "description": "Offset",
"name": "limit", "name": "limit",
"in": "query", "in": "query",
"required": true, "required": true
"schema": {
"type": "int"
}
} }
], ],
"responses": { "responses": {
@@ -121,14 +116,12 @@ var doc = `{
"400": { "400": {
"description": "We need ID!!", "description": "We need ID!!",
"schema": { "schema": {
"type": "object",
"$ref": "#/definitions/web.APIError" "$ref": "#/definitions/web.APIError"
} }
}, },
"404": { "404": {
"description": "Can not find ID", "description": "Can not find ID",
"schema": { "schema": {
"type": "object",
"$ref": "#/definitions/web.APIError" "$ref": "#/definitions/web.APIError"
} }
} }
@@ -140,10 +133,10 @@ var doc = `{
"web.APIError": { "web.APIError": {
"type": "object", "type": "object",
"properties": { "properties": {
"ErrorCode": { "errorCode": {
"type": "int" "type": "integer"
}, },
"ErrorMessage": { "errorMessage": {
"type": "string" "type": "string"
} }
} }
@@ -151,11 +144,49 @@ var doc = `{
} }
}` }`
type swaggerInfo struct {
Version string
Host string
BasePath string
Schemes []string
Title string
Description string
}
// SwaggerInfo holds exported Swagger Info so clients can modify it
var SwaggerInfo = swaggerInfo{
Version: "1.0",
Host: "petstore.swagger.io:8080",
BasePath: "/v2",
Schemes: []string{},
Title: "Swagger Example API",
Description: "This is a sample server Petstore server.",
}
type s struct{} type s struct{}
func (s *s) ReadDoc() string { func (s *s) ReadDoc() string {
return doc sInfo := SwaggerInfo
sInfo.Description = strings.Replace(sInfo.Description, "\n", "\\n", -1)
t, err := template.New("swagger_info").Funcs(template.FuncMap{
"marshal": func(v interface{}) string {
a, _ := json.Marshal(v)
return string(a)
},
}).Parse(doc)
if err != nil {
return doc
}
var tpl bytes.Buffer
if err := t.Execute(&tpl, sInfo); err != nil {
return doc
}
return tpl.String()
} }
func init() { func init() {
swag.Register(swag.Name, &s{}) swag.Register(swag.Name, &s{})
} }
+7 -2
View File
@@ -5,6 +5,8 @@ import (
"github.com/swaggo/files" "github.com/swaggo/files"
"github.com/swaggo/gin-swagger" "github.com/swaggo/gin-swagger"
"github.com/swaggo/gin-swagger/example/basic/api"
_ "github.com/swaggo/gin-swagger/example/basic/docs" _ "github.com/swaggo/gin-swagger/example/basic/docs"
) )
@@ -20,12 +22,15 @@ import (
// @license.name Apache 2.0 // @license.name Apache 2.0
// @license.url http://www.apache.org/licenses/LICENSE-2.0.html // @license.url http://www.apache.org/licenses/LICENSE-2.0.html
// @host petstore.swagger.io // @host petstore.swagger.io:8080
// @BasePath /v2 // @BasePath /v2
func main() { func main() {
r := gin.New() r := gin.New()
url := ginSwagger.URL("http://localhost:8080/swagger/doc.json") // The url pointing to API definition r.GET("/v2/testapi/get-string-by-int/:some_id", api.GetStringByInt)
r.GET("/v2/testapi/get-struct-array-by-string/:some_id", api.GetStructArrayByString)
url := ginSwagger.URL("http://petstore.swagger.io:8080/swagger/doc.json") // The url pointing to API definition
r.GET("/swagger/*any", ginSwagger.WrapHandler(swaggerFiles.Handler, url)) r.GET("/swagger/*any", ginSwagger.WrapHandler(swaggerFiles.Handler, url))
r.Run() r.Run()
+4
View File
@@ -1,12 +1,14 @@
module github.com/swaggo/gin-swagger module github.com/swaggo/gin-swagger
require ( require (
github.com/alecthomas/template v0.0.0-20190718012654-fb15b899a751
github.com/gin-contrib/gzip v0.0.1 github.com/gin-contrib/gzip v0.0.1
github.com/gin-contrib/sse v0.1.0 // indirect github.com/gin-contrib/sse v0.1.0 // indirect
github.com/gin-gonic/gin v1.4.0 github.com/gin-gonic/gin v1.4.0
github.com/kr/pretty v0.1.0 // indirect github.com/kr/pretty v0.1.0 // indirect
github.com/mattn/go-isatty v0.0.8 // indirect github.com/mattn/go-isatty v0.0.8 // indirect
github.com/stretchr/testify v1.3.0 github.com/stretchr/testify v1.3.0
github.com/swaggo/files v0.0.0-20190704085106-630677cd5c14
github.com/swaggo/swag v1.5.1 github.com/swaggo/swag v1.5.1
github.com/ugorji/go v1.1.5-pre // indirect github.com/ugorji/go v1.1.5-pre // indirect
golang.org/x/net v0.0.0-20190611141213-3f473d35a33a golang.org/x/net v0.0.0-20190611141213-3f473d35a33a
@@ -15,3 +17,5 @@ require (
golang.org/x/tools v0.0.0-20190611222205-d73e1c7e250b // indirect golang.org/x/tools v0.0.0-20190611222205-d73e1c7e250b // indirect
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 // indirect gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 // indirect
) )
go 1.13
+2
View File
@@ -2,6 +2,7 @@ github.com/PuerkitoBio/purell v1.1.0 h1:rmGxhojJlM0tuKtfdvliR84CFHljx9ag64t2xmVk
github.com/PuerkitoBio/purell v1.1.0/go.mod h1:c11w/QuzBsJSee3cPx9rAFu61PvFxuPbtSwDGJws/X0= github.com/PuerkitoBio/purell v1.1.0/go.mod h1:c11w/QuzBsJSee3cPx9rAFu61PvFxuPbtSwDGJws/X0=
github.com/PuerkitoBio/urlesc v0.0.0-20170810143723-de5bf2ad4578 h1:d+Bc7a5rLufV/sSk/8dngufqelfh6jnri85riMAaF/M= github.com/PuerkitoBio/urlesc v0.0.0-20170810143723-de5bf2ad4578 h1:d+Bc7a5rLufV/sSk/8dngufqelfh6jnri85riMAaF/M=
github.com/PuerkitoBio/urlesc v0.0.0-20170810143723-de5bf2ad4578/go.mod h1:uGdkoq3SwY9Y+13GIhn11/XLaGBb4BfwItxLd5jeuXE= github.com/PuerkitoBio/urlesc v0.0.0-20170810143723-de5bf2ad4578/go.mod h1:uGdkoq3SwY9Y+13GIhn11/XLaGBb4BfwItxLd5jeuXE=
github.com/alecthomas/template v0.0.0-20190718012654-fb15b899a751/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
@@ -53,6 +54,7 @@ github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0Q= github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0Q=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/swaggo/files v0.0.0-20190704085106-630677cd5c14/go.mod h1:gxQT6pBGRuIGunNf/+tSOB5OHvguWi8Tbt82WOkf35E=
github.com/swaggo/swag v1.5.1 h1:2Agm8I4K5qb00620mHq0VJ05/KT4FtmALPIcQR9lEZM= github.com/swaggo/swag v1.5.1 h1:2Agm8I4K5qb00620mHq0VJ05/KT4FtmALPIcQR9lEZM=
github.com/swaggo/swag v1.5.1/go.mod h1:1Bl9F/ZBpVWh22nY0zmYyASPO1lI/zIwRDrpZU+tv8Y= github.com/swaggo/swag v1.5.1/go.mod h1:1Bl9F/ZBpVWh22nY0zmYyASPO1lI/zIwRDrpZU+tv8Y=
github.com/ugorji/go v1.1.4/go.mod h1:uQMGLiO92mf5W77hV/PUCpI3pbzQx3CRekS0kk+RGrc= github.com/ugorji/go v1.1.4/go.mod h1:uQMGLiO92mf5W77hV/PUCpI3pbzQx3CRekS0kk+RGrc=