diff --git a/example/basic/docs/docs.go b/example/basic/docs/docs.go index 4b7e83c..97d5001 100644 --- a/example/basic/docs/docs.go +++ b/example/basic/docs/docs.go @@ -1,18 +1,23 @@ // GENERATED BY THE COMMAND ABOVE; DO NOT EDIT -// This file was generated by swaggo/swag at -// 2017-06-25 01:25:37.872454531 +0800 CST +// This file was generated by swaggo/swag package docs import ( + "bytes" + "encoding/json" + "strings" + + "github.com/alecthomas/template" "github.com/swaggo/swag" ) var doc = `{ + "schemes": {{ marshal .Schemes }}, "swagger": "2.0", "info": { - "description": "This is a sample server Petstore server.", - "title": "Swagger Example API", + "description": "{{.Description}}", + "title": "{{.Title}}", "termsOfService": "http://swagger.io/terms/", "contact": { "name": "API Support", @@ -23,10 +28,10 @@ var doc = `{ "name": "Apache 2.0", "url": "http://www.apache.org/licenses/LICENSE-2.0.html" }, - "version": "1.0" + "version": "{{.Version}}" }, - "host": "petstore.swagger.io", - "basePath": "/v2", + "host": "{{.Host}}", + "basePath": "{{.BasePath}}", "paths": { "/testapi/get-string-by-int/{some_id}": { "get": { @@ -40,13 +45,11 @@ var doc = `{ "summary": "Add a new pet to the store", "parameters": [ { + "type": "integer", "description": "Some ID", "name": "some_id", "in": "path", - "required": true, - "schema": { - "type": "int" - } + "required": true } ], "responses": { @@ -59,14 +62,12 @@ var doc = `{ "400": { "description": "We need ID!!", "schema": { - "type": "object", "$ref": "#/definitions/web.APIError" } }, "404": { "description": "Can not find ID", "schema": { - "type": "object", "$ref": "#/definitions/web.APIError" } } @@ -84,31 +85,25 @@ var doc = `{ ], "parameters": [ { + "type": "string", "description": "Some ID", "name": "some_id", "in": "path", - "required": true, - "schema": { - "type": "string" - } + "required": true }, { + "type": "integer", "description": "Offset", "name": "offset", "in": "query", - "required": true, - "schema": { - "type": "int" - } + "required": true }, { + "type": "integer", "description": "Offset", "name": "limit", "in": "query", - "required": true, - "schema": { - "type": "int" - } + "required": true } ], "responses": { @@ -121,14 +116,12 @@ var doc = `{ "400": { "description": "We need ID!!", "schema": { - "type": "object", "$ref": "#/definitions/web.APIError" } }, "404": { "description": "Can not find ID", "schema": { - "type": "object", "$ref": "#/definitions/web.APIError" } } @@ -140,10 +133,10 @@ var doc = `{ "web.APIError": { "type": "object", "properties": { - "ErrorCode": { - "type": "int" + "errorCode": { + "type": "integer" }, - "ErrorMessage": { + "errorMessage": { "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{} 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() { swag.Register(swag.Name, &s{}) } diff --git a/example/basic/main.go b/example/basic/main.go index 95bd678..4b2f659 100644 --- a/example/basic/main.go +++ b/example/basic/main.go @@ -5,6 +5,8 @@ import ( "github.com/swaggo/files" "github.com/swaggo/gin-swagger" + "github.com/swaggo/gin-swagger/example/basic/api" + _ "github.com/swaggo/gin-swagger/example/basic/docs" ) @@ -20,12 +22,15 @@ import ( // @license.name Apache 2.0 // @license.url http://www.apache.org/licenses/LICENSE-2.0.html -// @host petstore.swagger.io +// @host petstore.swagger.io:8080 // @BasePath /v2 func main() { 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.Run() diff --git a/go.mod b/go.mod index 85830d3..d826ee0 100644 --- a/go.mod +++ b/go.mod @@ -1,12 +1,14 @@ module github.com/swaggo/gin-swagger require ( + github.com/alecthomas/template v0.0.0-20190718012654-fb15b899a751 github.com/gin-contrib/gzip v0.0.1 github.com/gin-contrib/sse v0.1.0 // indirect github.com/gin-gonic/gin v1.4.0 github.com/kr/pretty v0.1.0 // indirect github.com/mattn/go-isatty v0.0.8 // indirect 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/ugorji/go v1.1.5-pre // indirect golang.org/x/net v0.0.0-20190611141213-3f473d35a33a @@ -15,3 +17,5 @@ require ( golang.org/x/tools v0.0.0-20190611222205-d73e1c7e250b // indirect gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 // indirect ) + +go 1.13 diff --git a/go.sum b/go.sum index 47d45af..44a70bb 100644 --- a/go.sum +++ b/go.sum @@ -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/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/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.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= 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.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0Q= 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/go.mod h1:1Bl9F/ZBpVWh22nY0zmYyASPO1lI/zIwRDrpZU+tv8Y= github.com/ugorji/go v1.1.4/go.mod h1:uQMGLiO92mf5W77hV/PUCpI3pbzQx3CRekS0kk+RGrc=