History

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

Anton Dzyk committed 6 years ago

Commit 4199ce81 parent bbeaf5b4 files · +75 −33Browse files

Showing the changes to example/basic (2 of 4 files). Show all

Unified · Split

2 files
  1. example/basic/docs/docs.go +62 −31
  2. example/basic/main.go +7 −2
example/basic/docs/docs.go+62−31
@@ -1,18 +1,23 @@
11 // GENERATED BY THE COMMAND ABOVE; DO NOT EDIT
2−// This file was generated by swaggo/swag at
3−// 2017-06-25 01:25:37.872454531 +0800 CST
2+// This file was generated by swaggo/swag
43
54 package docs
65
76 import (
7+ "bytes"
8+ "encoding/json"
9+ "strings"
10+
11+ "github.com/alecthomas/template"
812 "github.com/swaggo/swag"
913 )
1014
1115 var doc = `{
16+ "schemes": {{ marshal .Schemes }},
1217 "swagger": "2.0",
1318 "info": {
14− "description": "This is a sample server Petstore server.",
15− "title": "Swagger Example API",
19+ "description": "{{.Description}}",
20+ "title": "{{.Title}}",
1621 "termsOfService": "http://swagger.io/terms/",
1722 "contact": {
1823 "name": "API Support",
@@ -23,10 +28,10 @@ var doc = `{ · 4 unmodified lines
2328 "name": "Apache 2.0",
2429 "url": "http://www.apache.org/licenses/LICENSE-2.0.html"
2530 },
26− "version": "1.0"
31+ "version": "{{.Version}}"
2732 },
28− "host": "petstore.swagger.io",
29− "basePath": "/v2",
33+ "host": "{{.Host}}",
34+ "basePath": "{{.BasePath}}",
3035 "paths": {
3136 "/testapi/get-string-by-int/{some_id}": {
3237 "get": {
@@ -40,13 +45,11 @@ var doc = `{ · 7 unmodified lines
4045 "summary": "Add a new pet to the store",
4146 "parameters": [
4247 {
48+ "type": "integer",
4349 "description": "Some ID",
4450 "name": "some_id",
4551 "in": "path",
46− "required": true,
47− "schema": {
48− "type": "int"
49− }
52+ "required": true
5053 }
5154 ],
5255 "responses": {
@@ -59,14 +62,12 @@ var doc = `{ · 6 unmodified lines
5962 "400": {
6063 "description": "We need ID!!",
6164 "schema": {
62− "type": "object",
6365 "$ref": "#/definitions/web.APIError"
6466 }
6567 },
6668 "404": {
6769 "description": "Can not find ID",
6870 "schema": {
69− "type": "object",
7071 "$ref": "#/definitions/web.APIError"
7172 }
7273 }
@@ -84,31 +85,25 @@ var doc = `{ · 11 unmodified lines
8485 ],
8586 "parameters": [
8687 {
88+ "type": "string",
8789 "description": "Some ID",
8890 "name": "some_id",
8991 "in": "path",
90− "required": true,
91− "schema": {
92− "type": "string"
93− }
92+ "required": true
9493 },
9594 {
95+ "type": "integer",
9696 "description": "Offset",
9797 "name": "offset",
9898 "in": "query",
99− "required": true,
100− "schema": {
101− "type": "int"
102− }
99+ "required": true
103100 },
104101 {
102+ "type": "integer",
105103 "description": "Offset",
106104 "name": "limit",
107105 "in": "query",
108− "required": true,
109− "schema": {
110− "type": "int"
111− }
106+ "required": true
112107 }
113108 ],
114109 "responses": {
@@ -121,14 +116,12 @@ var doc = `{ · 6 unmodified lines
121116 "400": {
122117 "description": "We need ID!!",
123118 "schema": {
124− "type": "object",
125119 "$ref": "#/definitions/web.APIError"
126120 }
127121 },
128122 "404": {
129123 "description": "Can not find ID",
130124 "schema": {
131− "type": "object",
132125 "$ref": "#/definitions/web.APIError"
133126 }
134127 }
@@ -140,10 +133,10 @@ var doc = `{ · 5 unmodified lines
140133 "web.APIError": {
141134 "type": "object",
142135 "properties": {
143− "ErrorCode": {
144− "type": "int"
136+ "errorCode": {
137+ "type": "integer"
145138 },
146− "ErrorMessage": {
139+ "errorMessage": {
147140 "type": "string"
148141 }
149142 }
@@ -151,11 +144,49 @@ var doc = `{ · 1 unmodified line
151144 }
152145 }`
153146
147+type swaggerInfo struct {
148+ Version string
149+ Host string
150+ BasePath string
151+ Schemes []string
152+ Title string
153+ Description string
154+}
155+
156+// SwaggerInfo holds exported Swagger Info so clients can modify it
157+var SwaggerInfo = swaggerInfo{
158+ Version: "1.0",
159+ Host: "petstore.swagger.io:8080",
160+ BasePath: "/v2",
161+ Schemes: []string{},
162+ Title: "Swagger Example API",
163+ Description: "This is a sample server Petstore server.",
164+}
165+
154166 type s struct{}
155167
156168 func (s *s) ReadDoc() string {
157− return doc
169+ sInfo := SwaggerInfo
170+ sInfo.Description = strings.Replace(sInfo.Description, "\n", "\\n", -1)
171+
172+ t, err := template.New("swagger_info").Funcs(template.FuncMap{
173+ "marshal": func(v interface{}) string {
174+ a, _ := json.Marshal(v)
175+ return string(a)
176+ },
177+ }).Parse(doc)
178+ if err != nil {
179+ return doc
180+ }
181+
182+ var tpl bytes.Buffer
183+ if err := t.Execute(&tpl, sInfo); err != nil {
184+ return doc
185+ }
186+
187+ return tpl.String()
158188 }
189+
159190 func init() {
160191 swag.Register(swag.Name, &s{})
161192 }
example/basic/main.go+7−2
@@ -5,6 +5,8 @@ import ( · 4 unmodified lines
55 "github.com/swaggo/files"
66 "github.com/swaggo/gin-swagger"
77
8+ "github.com/swaggo/gin-swagger/example/basic/api"
9+
810 _ "github.com/swaggo/gin-swagger/example/basic/docs"
911 )
1012
@@ -20,12 +22,15 @@ import ( · 9 unmodified lines
2022 // @license.name Apache 2.0
2123 // @license.url http://www.apache.org/licenses/LICENSE-2.0.html
2224
23−// @host petstore.swagger.io
25+// @host petstore.swagger.io:8080
2426 // @BasePath /v2
2527 func main() {
2628 r := gin.New()
2729
28− url := ginSwagger.URL("http://localhost:8080/swagger/doc.json") // The url pointing to API definition
30+ r.GET("/v2/testapi/get-string-by-int/:some_id", api.GetStringByInt)
31+ r.GET("/v2/testapi/get-struct-array-by-string/:some_id", api.GetStructArrayByString)
32+
33+ url := ginSwagger.URL("http://petstore.swagger.io:8080/swagger/doc.json") // The url pointing to API definition
2934 r.GET("/swagger/*any", ginSwagger.WrapHandler(swaggerFiles.Handler, url))
3035
3136 r.Run()

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t