moneydodo.service

command module
v0.0.0-...-4aec9fb Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 27, 2019 License: MIT Imports: 13 Imported by: 0

README

MoneyDodo.service

Build Status Coverage Status Analytics

此项目为MoneyDodo.web和MoneyDodo.wechat的后台服务实现,采用微服务,使用go-kit框架,代码主体利用GoKit CLI开源工具快速生成,便于将开发重心放到业务逻辑上。

一、代码结构介绍

1. conf: 存放数据库配置文件,具体配置方法参考conf.example.yml

2. db: 存放数据库相关内容,初始化xorm、MySQL引擎

3. model: 存放数据结构,包括user、task等

4. middleware: 中间件,目前包括jwt认证中间件。

5. swagger: swaggerui,API的可视化界面,便于前后端进行交互

6. authentication: 用户登录认证相关服务,主要处理用户登陆的认证,token获取,退出等请求

7. user: 用户系统微服务,主要处理用户信息的Get、Post、Put、Patch、Delete等请求

8. personalTasks: 用户task相关微服务,主要处理用户发布任务,删除任务,领取任务,查询任务等请求

9. certify: 用户实名认知相关服务,主要处理用户上传实名认证信息,查询信息等请求。

...其他微服务,待完成

之后,代码结构可以再次调整,将共用代码进行提取,减少代码冗余。

二、GoKit CLI使用方法

GoKit CLI使用方法可以参考https://medium.com/@kujtimii.h/creating-a-todo-app-using-gokit-cli-20f066a58e1

# 以用户系统微服务为例
$ kit n s user
# 之后定义相关服务(Get、Post等)
$ kit g s user -w --gorilla
#-w generate some default service middleware.
#--gorilla use gorilla/mux instead of the default http handler for the http transport.
$ go run user/cmd/main.go
#ts=2019-04-16T09:52:56.5674053Z caller=service.go:78 tracer=none
#ts=2019-04-16T09:52:56.5714055Z caller=service.go:100 transport=HTTP addr=:8081
#ts=2019-04-16T09:52:56.5714055Z caller=service.go:134 transport=debug/HTTP addr=:8080
#端口为8081

三、swaggerui使用方法

参考链接: https://swagger.io/docs/specification/paths-and-operations/ https://studygolang.com/articles/12354?fr=sidebar

以user服务中PUT请求为例:
A.注释规范
// swagger:operation PUT /api/users/{userid} users swaggPutReq
// ---
// summary: Update the user profile
// description: Update the user profile with the profile. Also, you need to specify the user ID.
// parameters:
// - name: userid
//   in: path
//   description: id of user
//   type: string
//   required: true
// - name: Body
//   in: body
//   schema:
//     "$ref": "#/definitions/User"
//   required: true
// responses:
//   "200":
//	   "$ref": "#/responses/swaggNoReturnValue"
//   "400":
//	   "$ref": "#/responses/swaggBadReq"

1. swagger:operation - 为提示符,表示一个请求操作

2. PUT - HTTP方法

3. /api/users/{userid} - 路径

4. users - 类似于路由分隔标签,将相同的分隔标签的请求归到同一组

5. swaggPutReq - 此参数没有具体意义,单参数是强制性的,但是推荐不同请求使用不同的参数。命名格式可采用swaggXXXReq,若不同请求该参数一样,会出现很多bug。

6. --- - 分隔符,下方代码为YAML格式的swagger规范,缩进必须保持一致且正确,推荐使用两格缩进。否则将无法正常解析。

7. summary - 标题,API的概括描述

8. description - 描述,API的详细描述

9. parameters - URL参数,此例子中为{userId},如果需要query的,可使用?name={name}来表示

10. - name - 指定参数,此例子中为URL中的userId

11. in - 表示此参数位于哪个部分,path表示位于URL路径中,body表示位于上传的request body

12. description - 参数说明

13. type - 指定参数类型

14. required - 是否一定需要此参数

15. schema - 当参数位于body中需要此参数,指定参数的数据结构,**"$ref": "#/definitions/XXX"**按照此种格式写即可,具体原因尚未搞懂。

16. responses - 说明返回类型。

17. "200" - 200表示状态码,我用200表示成功的请求;"$ref": "#/responses/swaggNoReturnValue"按照此格式来书写,其中swaggNoReturnValue定义在swagger/model.go文件中:

// HTTP status code 200 and no return value
// swagger:response swaggNoReturnValue
type swaggNoReturnValue struct {
	// in:body
	Body struct {
		// HTTP Status Code 200
		Status bool `json:"status"`
		// Detailed error message
		Errinfo string `json:"errinfo"`
	}
}
  • 第一行注释:尽量书写,会体现在swaggerui
  • 第二行指数:swagger:response为提示符,swaggNoReturnValue为下方数据类型的一个tag,这两者共同组成了**"$ref": "#/responses/swaggNoReturnValue"**
  • 数据结构中,有三个属性:StatusErrinfoData,上述结构中没有返回值,所以取消了最后一个参数。

18. "400" - 400表示状态码,我用400来表示失败的请求。返回格式说明与上述过程一致。

更加详细的说明参看官方文档

B. 生成swagger.user.json
  • 注意在user/cmd/main.go文件中import _ "github.com/money-hub/MoneyDodo.service/swagger",只有这样,上述注释中的#/responses/才会被识别。

  • 安装go-swagger:

    go get github.com\go-swagger\go-swagger\cmd\swagger
    
  • 生成spec

    # 当前路径:根目录
    $ cd user/cmd
    # 此命令会从当前文件及下的main函数入口递归搜索所有文件的swagger注释,最终生成指定的`swagger.users.json`
    $ swagger generate spec -o ../../swagger/swaggerui/dist/swagger.user.json
    $ cd ../..
    # 开启的默认端口为8000,注意不要占用此端口
    $ go run swagger/swaggerui/main.go
    
C. 说明

每个服务各自生成相应的文档,命名格式为swagger.XXX.jsonXXX为服务名称,方便查询。

四、镜像管理

dockerfiles

此文件夹中包含各个微服务的dockerfile,用于构建镜像。

# 栗子cpt:
#源镜像
FROM golang:1.12
#设置镜像工作目录
WORKDIR $GOPATH/src/github.com/money-hub/MoneyDodo.service
#将宿主机的go工程代码加入到docker容器中
ADD . $GOPATH/src/github.com/money-hub/MoneyDodo.service
# 安装依赖包
RUN go get ./...
#暴露端口
EXPOSE 8005
#最终运行docker的命令
ENTRYPOINT  ["go", "run", "./cpt/cmd/main.go"]
构建镜像

在项目根目录(MoneyDodo.service/)下运行以下命令:

$ sudo docker build -f dockerfiles/cpt.Dockerfile -t moneydodo.cpt . 
测试服务
$ sudo docker run -p 8005:8005 --name moneydodo.cpt --link moneydodo.db:moneydodo.db -d moneydodo.cpt

之后即可使用8005端口来测试此服务。 【注】在启动上述服务之前,需要确保moneydodo.db已经启动,启动方法类似:

$ sudo docker build -f dockerfiles/mysql.dockerfile -t moneydodo.db . 
$ sudo docker run -p 3306:3306 --name moneydodo.db -d moneydodo.db

Documentation

The Go Gopher

There is no documentation for this package.

Directories

Path Synopsis
authentication
cmd
money-hub MoneyDodo/auth This documentation describes example APIs found under https://github.com/money-hub/MoneyDodo.service Schemes: http Version: 1.0.0 License: MIT http://opensource.org/licenses/MIT Consumes: - application/json Produces: - application/json Security: - bearer SecurityDefinitions: bearer: type: apiKey name: Authorization in: header swagger:meta
money-hub MoneyDodo/auth This documentation describes example APIs found under https://github.com/money-hub/MoneyDodo.service Schemes: http Version: 1.0.0 License: MIT http://opensource.org/licenses/MIT Consumes: - application/json Produces: - application/json Security: - bearer SecurityDefinitions: bearer: type: apiKey name: Authorization in: header swagger:meta
cmd/service
THIS FILE IS AUTO GENERATED BY GK-CLI DO NOT EDIT!!
THIS FILE IS AUTO GENERATED BY GK-CLI DO NOT EDIT!!
pkg/endpoint
THIS FILE IS AUTO GENERATED BY GK-CLI DO NOT EDIT!!
THIS FILE IS AUTO GENERATED BY GK-CLI DO NOT EDIT!!
pkg/http
THIS FILE IS AUTO GENERATED BY GK-CLI DO NOT EDIT!!
THIS FILE IS AUTO GENERATED BY GK-CLI DO NOT EDIT!!
certify
cmd
cmd/service
THIS FILE IS AUTO GENERATED BY GK-CLI DO NOT EDIT!!
THIS FILE IS AUTO GENERATED BY GK-CLI DO NOT EDIT!!
pkg/endpoint
THIS FILE IS AUTO GENERATED BY GK-CLI DO NOT EDIT!!
THIS FILE IS AUTO GENERATED BY GK-CLI DO NOT EDIT!!
pkg/http
THIS FILE IS AUTO GENERATED BY GK-CLI DO NOT EDIT!!
THIS FILE IS AUTO GENERATED BY GK-CLI DO NOT EDIT!!
cmd
money-hub MoneyDodo/charge This documentation describes example APIs found under https://github.com/money-hub/MoneyDodo.service Schemes: http Version: 1.0.0 License: MIT http://opensource.org/licenses/MIT Consumes: - application/json Produces: - application/json Security: - bearer SecurityDefinitions: bearer: type: apiKey name: Authorization in: header swagger:meta
money-hub MoneyDodo/charge This documentation describes example APIs found under https://github.com/money-hub/MoneyDodo.service Schemes: http Version: 1.0.0 License: MIT http://opensource.org/licenses/MIT Consumes: - application/json Produces: - application/json Security: - bearer SecurityDefinitions: bearer: type: apiKey name: Authorization in: header swagger:meta
comment
cmd
money-hub MoneyDodo/comment This documentation describes example APIs found under https://github.com/money-hub/MoneyDodo.service Schemes: http Version: 1.0.0 License: MIT http://opensource.org/licenses/MIT Consumes: - application/json Produces: - application/json Security: - bearer SecurityDefinitions: bearer: type: apiKey name: Authorization in: header swagger:meta
money-hub MoneyDodo/comment This documentation describes example APIs found under https://github.com/money-hub/MoneyDodo.service Schemes: http Version: 1.0.0 License: MIT http://opensource.org/licenses/MIT Consumes: - application/json Produces: - application/json Security: - bearer SecurityDefinitions: bearer: type: apiKey name: Authorization in: header swagger:meta
cmd/service
THIS FILE IS AUTO GENERATED BY GK-CLI DO NOT EDIT!!
THIS FILE IS AUTO GENERATED BY GK-CLI DO NOT EDIT!!
pkg/endpoint
THIS FILE IS AUTO GENERATED BY GK-CLI DO NOT EDIT!!
THIS FILE IS AUTO GENERATED BY GK-CLI DO NOT EDIT!!
pkg/http
THIS FILE IS AUTO GENERATED BY GK-CLI DO NOT EDIT!!
THIS FILE IS AUTO GENERATED BY GK-CLI DO NOT EDIT!!
cpt
cmd
money-hub MoneyDodo/cpt This documentation describes example APIs found under https://github.com/money-hub/MoneyDodo.service Schemes: http Version: 1.0.0 License: MIT http://opensource.org/licenses/MIT Consumes: - application/json Produces: - application/json Security: - bearer SecurityDefinitions: bearer: type: apiKey name: Authorization in: header swagger:meta
money-hub MoneyDodo/cpt This documentation describes example APIs found under https://github.com/money-hub/MoneyDodo.service Schemes: http Version: 1.0.0 License: MIT http://opensource.org/licenses/MIT Consumes: - application/json Produces: - application/json Security: - bearer SecurityDefinitions: bearer: type: apiKey name: Authorization in: header swagger:meta
deal
cmd
cmd/service
THIS FILE IS AUTO GENERATED BY GK-CLI DO NOT EDIT!!
THIS FILE IS AUTO GENERATED BY GK-CLI DO NOT EDIT!!
pkg/endpoint
THIS FILE IS AUTO GENERATED BY GK-CLI DO NOT EDIT!!
THIS FILE IS AUTO GENERATED BY GK-CLI DO NOT EDIT!!
pkg/http
THIS FILE IS AUTO GENERATED BY GK-CLI DO NOT EDIT!!
THIS FILE IS AUTO GENERATED BY GK-CLI DO NOT EDIT!!
review
cmd
cmd/service
THIS FILE IS AUTO GENERATED BY GK-CLI DO NOT EDIT!!
THIS FILE IS AUTO GENERATED BY GK-CLI DO NOT EDIT!!
pkg/endpoint
THIS FILE IS AUTO GENERATED BY GK-CLI DO NOT EDIT!!
THIS FILE IS AUTO GENERATED BY GK-CLI DO NOT EDIT!!
pkg/http
THIS FILE IS AUTO GENERATED BY GK-CLI DO NOT EDIT!!
THIS FILE IS AUTO GENERATED BY GK-CLI DO NOT EDIT!!
task
cmd
cmd/service
THIS FILE IS AUTO GENERATED BY GK-CLI DO NOT EDIT!!
THIS FILE IS AUTO GENERATED BY GK-CLI DO NOT EDIT!!
pkg/endpoint
THIS FILE IS AUTO GENERATED BY GK-CLI DO NOT EDIT!!
THIS FILE IS AUTO GENERATED BY GK-CLI DO NOT EDIT!!
pkg/http
THIS FILE IS AUTO GENERATED BY GK-CLI DO NOT EDIT!!
THIS FILE IS AUTO GENERATED BY GK-CLI DO NOT EDIT!!
user
cmd
money-hub MoneyDodo/user This documentation describes example APIs found under https://github.com/money-hub/MoneyDodo.service Schemes: http Version: 1.0.0 License: MIT http://opensource.org/licenses/MIT Consumes: - application/json Produces: - application/json Security: - bearer SecurityDefinitions: bearer: type: apiKey name: Authorization in: header swagger:meta
money-hub MoneyDodo/user This documentation describes example APIs found under https://github.com/money-hub/MoneyDodo.service Schemes: http Version: 1.0.0 License: MIT http://opensource.org/licenses/MIT Consumes: - application/json Produces: - application/json Security: - bearer SecurityDefinitions: bearer: type: apiKey name: Authorization in: header swagger:meta
cmd/service
THIS FILE IS AUTO GENERATED BY GK-CLI DO NOT EDIT!!
THIS FILE IS AUTO GENERATED BY GK-CLI DO NOT EDIT!!
pkg/endpoint
THIS FILE IS AUTO GENERATED BY GK-CLI DO NOT EDIT!!
THIS FILE IS AUTO GENERATED BY GK-CLI DO NOT EDIT!!
pkg/http
THIS FILE IS AUTO GENERATED BY GK-CLI DO NOT EDIT!!
THIS FILE IS AUTO GENERATED BY GK-CLI DO NOT EDIT!!

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL