From d0a274096c5ca06a1285fea70b17d695b59ef459 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=99=B6=E5=A3=AB=E6=B6=B5?= <630892807@qq.com> Date: Mon, 19 Oct 2020 23:38:37 +0800 Subject: [PATCH] =?UTF-8?q?API=E6=96=87=E6=A1=A3=E5=A2=9E=E5=8A=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cmd/server.go | 2 +- controller/visitor.go | 6 ++++++ docs/docs.go | 47 +++++++++++++++++++++++++++++++++++-------- docs/swagger.json | 27 ++++++++++++++++--------- docs/swagger.yaml | 19 ++++++++--------- readme.md | 2 +- 6 files changed, 74 insertions(+), 29 deletions(-) diff --git a/cmd/server.go b/cmd/server.go index 1be3cc6..784340b 100644 --- a/cmd/server.go +++ b/cmd/server.go @@ -66,7 +66,7 @@ func run() { //docs.SwaggerInfo.Host = "127.0.0.1:"+port docs.SwaggerInfo.Host = "gofly.sopans.com" docs.SwaggerInfo.BasePath = "/" - //docs.SwaggerInfo.Schemes = []string{"http"} + docs.SwaggerInfo.Schemes = []string{"https"} engine.GET("/docs/*any", ginSwagger.WrapHandler(swaggerFiles.Handler)) //logFile, _ := os.OpenFile("./fatal.log", os.O_CREATE|os.O_APPEND|os.O_RDWR, 0660) diff --git a/controller/visitor.go b/controller/visitor.go index 3647567..f796331 100644 --- a/controller/visitor.go +++ b/controller/visitor.go @@ -175,6 +175,12 @@ func GetVisitorMessage(c *gin.Context) { "result": result, }) } + +// @Summary 获取在线访客列表接口 +// @Produce json +// @Success 200 {object} controller.Response +// @Failure 200 {object} controller.Response +// @Router /visitors_online [get] func GetVisitorOnlines(c *gin.Context) { users := make([]map[string]string, 0) for uid, visitor := range clientList { diff --git a/docs/docs.go b/docs/docs.go index 53780f4..cf600bf 100644 --- a/docs/docs.go +++ b/docs/docs.go @@ -1,17 +1,20 @@ // GENERATED BY THE COMMAND ABOVE; DO NOT EDIT // This file was generated by swaggo/swag at -// 2020-07-23 16:30:50.1490506 +0800 CST m=+0.137007801 +// 2020-10-19 23:32:55.3852666 +0800 CST m=+0.208956401 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": "{{.Description}}", @@ -59,7 +62,6 @@ var doc = `{ "200": { "description": "OK", "schema": { - "type": "object", "$ref": "#/definitions/controller.Response" } } @@ -109,7 +111,6 @@ var doc = `{ "200": { "description": "OK", "schema": { - "type": "object", "$ref": "#/definitions/controller.Response" } } @@ -145,7 +146,6 @@ var doc = `{ "200": { "description": "OK", "schema": { - "type": "object", "$ref": "#/definitions/controller.Response" } } @@ -181,7 +181,22 @@ var doc = `{ "200": { "description": "OK", "schema": { - "type": "object", + "$ref": "#/definitions/controller.Response" + } + } + } + } + }, + "/visitors_online": { + "get": { + "produces": [ + "application/json" + ], + "summary": "获取在线访客列表接口", + "responses": { + "200": { + "description": "OK", + "schema": { "$ref": "#/definitions/controller.Response" } } @@ -211,23 +226,39 @@ 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 +var SwaggerInfo = swaggerInfo{ + Version: "", + Host: "", + BasePath: "", + Schemes: []string{}, + Title: "", + Description: "", +} type s struct{} func (s *s) ReadDoc() string { - t, err := template.New("swagger_info").Parse(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, SwaggerInfo); err != nil { + if err := t.Execute(&tpl, sInfo); err != nil { return doc } diff --git a/docs/swagger.json b/docs/swagger.json index d340b46..a7b5ee4 100644 --- a/docs/swagger.json +++ b/docs/swagger.json @@ -1,14 +1,9 @@ { "swagger": "2.0", "info": { - "description": "{{.Description}}", - "title": "{{.Title}}", "contact": {}, - "license": {}, - "version": "{{.Version}}" + "license": {} }, - "host": "{{.Host}}", - "basePath": "{{.BasePath}}", "paths": { "/check": { "post": { @@ -46,7 +41,6 @@ "200": { "description": "OK", "schema": { - "type": "object", "$ref": "#/definitions/controller.Response" } } @@ -96,7 +90,6 @@ "200": { "description": "OK", "schema": { - "type": "object", "$ref": "#/definitions/controller.Response" } } @@ -132,7 +125,6 @@ "200": { "description": "OK", "schema": { - "type": "object", "$ref": "#/definitions/controller.Response" } } @@ -168,7 +160,22 @@ "200": { "description": "OK", "schema": { - "type": "object", + "$ref": "#/definitions/controller.Response" + } + } + } + } + }, + "/visitors_online": { + "get": { + "produces": [ + "application/json" + ], + "summary": "获取在线访客列表接口", + "responses": { + "200": { + "description": "OK", + "schema": { "$ref": "#/definitions/controller.Response" } } diff --git a/docs/swagger.yaml b/docs/swagger.yaml index 2fcea99..80b52c2 100644 --- a/docs/swagger.yaml +++ b/docs/swagger.yaml @@ -1,4 +1,3 @@ -basePath: '{{.BasePath}}' definitions: controller.Response: properties: @@ -9,13 +8,9 @@ definitions: result: type: object type: object -host: '{{.Host}}' info: contact: {} - description: '{{.Description}}' license: {} - title: '{{.Title}}' - version: '{{.Version}}' paths: /check: post: @@ -44,7 +39,6 @@ paths: description: OK schema: $ref: '#/definitions/controller.Response' - type: object summary: 登陆验证接口 /message: post: @@ -78,7 +72,6 @@ paths: description: OK schema: $ref: '#/definitions/controller.Response' - type: object summary: 发送消息接口 /messages: get: @@ -102,7 +95,6 @@ paths: description: OK schema: $ref: '#/definitions/controller.Response' - type: object summary: 获取访客聊天信息接口 /visitors: get: @@ -126,6 +118,15 @@ paths: description: OK schema: $ref: '#/definitions/controller.Response' - type: object summary: 获取访客列表接口 + /visitors_online: + get: + produces: + - application/json + responses: + "200": + description: OK + schema: + $ref: '#/definitions/controller.Response' + summary: 获取在线访客列表接口 swagger: "2.0" diff --git a/readme.md b/readme.md index c352429..b0a4639 100644 --- a/readme.md +++ b/readme.md @@ -123,4 +123,4 @@ server{ ### 生成文档 1. 需要先安装swag -2. 在根目录swag init +2. 在根目录swag init -g go-fly.go