0


WEB框架一键生成神器:WEBClient&SERVER

目录

**统一Web服务框架讲解: **

web-client&server

  1. 概要

  1. 一键生成神器1. 安装webcli120工具

go install git.ichub.com/general/webcli120/cmd/webcli120

go get git.ichub.com/general/webcli120/cmd/webcli120

    1. 一键生成WEB工程
  1. 创建一个工程,有go.mod

go.mod添加replace google.golang.org/grpc => google.golang.org/grpc v1.26.0

c9dc6a8195c14cc497df193eb76e2758.png

  1. 创建config文件

go get git.ichub.com/general/webcli120/cmd/webcli120

Webcli120 install config

bc4122ba620541b892dfe5bf5d8cde91.png

  1. 创建整个工程文件

Webcli120 install all

(func installAll(cmd *cobra.Command, args []string) {
installConfig(cmd, args)
installWebServer(cmd, args)
installCmd(cmd, args)
installDocker(cmd, args)
installDocx(cmd, args)
installTest(cmd, args)
}

e200439b69a74a798eab53c273de5d41.png

  1. 执行:

Go mod tidy

Goinstall.bat

e04f907cc9aa4727a50ee3682b773cc0.png

  1. 验证

awesomeProject stat:查询性能统计

1c76254f64ff427a8d2f13d89c23683c.png

  1. **配置文件 **1. 切换环境:

Env: ${ICHUB_ENV:dev} 代表使用ichub-dev.yml配置

支持多个配置文件切换,

支持环境变量:

hosturl: ${ETCD_HOSTURL:192.168.27.27:2379}

Docker 容器配置ETCD_HOSTURL 则可修改配置

支持加密

password: ${ES_PASSWORD:enc(8CfGuLQ79iK6e6+UU/yXiA==)}

webcli120 enc 123456

8CfGuLQ79iK6e6+UU/yXiA== -》enc(8CfGuLQ79iK6e6+UU/yXiA==)

****支持指定服务地址: ****

client:
name: www.general.website.com
testUrl: ${ICHUB_TESTURL:http://localhost:100}

指向配置的 url

testUrl: #${ICHUB_TESTURL:http://localhost:100}

testUrl通过etcd注册中心查询www.general.website.com服务地址。

    1. 配置文件 ichub-dev.yml

etcd: 环境切换只要改etcd,
server:
hosturl: ${ETCD_HOSTURL:192.168.27.27:2379}
web: --配置
server: 本服务server 配置
name: ${WEB_SERVER_HOST:www.general.website.com}
port: 100

client: --本服务CLIENT配置
name: ${WEB_CLIENT_HOST:www.general.website.com}
testUrl: ${ICHUB_TESTURL:http://localhost:100}
webTimeout: 30

swagger: 本服务swagger配置
enable: ${SWAGGER_ENABLE:true}
host: ${SWAGGER_HOST:localhost:100}
basePath: /
version: v1.0
title: ${SWAGGER_TITLE:网站管理服务-WebsiteServer}

    1. 连接其它服务配置客户端

esclient: 连接esserver
name: web.esserver.com
testUrl: ${WEB_ESCLIENT_TESTURL:http://localhost:88}
webTimeout: 30
ruleclient: 连接规则引擎
name: web.ruleserver.com
testUrl: ${WEB_RULECLIENT_TESTURL:http://localhost:89}
webTimeout: 30

  1. 前端服务路由配置:继承原来

general_website:

name: www.general.website.com

address: 0.0.0.0

port: 100

运行模式(debug:调试,test:测试,release:正式)

runMode: debug

  1. godi依赖****注入1. 编写****代码:单例 basedto.BaseEntitySingle

type ****SingleEntity ****struct {
basedto.BaseEntitySingle
MultiBindNew *multi.MultiEntity godi:"bind"

 Id   int
 Name string

}

func NewSingleEntity() *SingleEntity {
var s = &SingleEntity{
Name: "single",
}
s.InitProxy(s)
return s
}

    1. 编写****代码:多实例 basedto.BaseEntity

type ****MultiEntity ****struct {
basedto.BaseEntity

 Id   int
 Name string

 myeentity *MyEntity `godi:"auto"`

}

func NewMultiEntity() *MultiEntity {
var s = &MultiEntity{
Name: "multi",
}
s.InitProxy(s)
return s
}

    1. 执行生成goid注入****代码:

Webcli120 godi all

1e8ced2987fe4f39a81c96cd65f7720a.png

使用测试:多实例

func Test001_FindMulti(t *testing.T) { //var bean1 = FindBeanMultiEntity()

 var bean2 = FindBeanMultiEntity()
 var bean3 = FindBeanMultiEntity()
 bean2.Name = "123"
 logrus.Info(bean2, bean3)
 assert.Equal(t, bean3.Name != bean2.Name, true)

}

单实例

func Test002_FindSingle(t *testing.T) {

 var singleEntity = FindBeanSingleEntity()

 singleEntity.Name = "aaa"
 logrus.Info(singleEntity.ToPrettyString())
 var sa = FindBeanSingleEntity()
 var sb = FindBeanSingleEntity()
 sb.Name = "bbb"
 logrus.Info(singleEntity, sa, sb)
 assert.Equal(t, sa.Name == singleEntity.Name, true)
 assert.Equal(t, sb.Name == singleEntity.Name, true)

}

  1. 根据struct自动生成测试套

webcli120 suite ConfigAllFuncService

f5f74405feeb447f955e0fd7ad13656a.png

  1. 编写WEB REST代码

Server/service目录创建一个目录如website

创建一个接口文件,一个接口一个struct

f1ac627d183b44d68645128a7312f818.png

配置路由func (this *WebSiteFuncService) init() *WebSiteFuncService {
this.WebGroupPath = "/raymond"
this.InitRoute(this, http.MethodGet, "/website")
return this
}

编写swagger 注释

// @Summary website
// @Tags 网站管理
// @Produce json
// @Success 200 {object} basedto.IchubResult "成功"
// @Failure 400 {object} string "请求错误"
// @Failure 500 {object} string "内部错误"
// @Router /website [get]
func (this *WebSiteFuncService) Execute(c *gin.Context) {
c.IndentedJSON(http.StatusOK, (&basedto.IchubResult{}).SuccessData("ok"))

}

注册

func Register() {
NewWebSiteFuncService().Register()

}

router目录下总注册

func Register() *webserver.WebHandlers {

 file.Register()
 config.Register()
 gocenter.Register()
 website.Register()

 return funchandler.Build()

}

  1. 生成****swagger

进入server目录执行swagini.bat生成

93ea162cd90c480cac4e2316eeec5ed6.png

  1. 运行服务

Go mod tidy

Goinstall.bat

25520b651ce04413b3b54e18121d3166.png

  1. 执行Swagger

12ea316f45e842eea461d91259b69773.png

  1. 测试代码 :

5faa9e1c8dfa4a5ba63f879b22cf1ab7.png

    1. Webclient: WEB客户端

func TestWebSuites(t *testing.T) {
suite.Run(t, new(TestWebSuite))
}

// 每个测试运行前,会执行
func (suite *TestWebSuite) SetupTest() {
ichublog.InitLogrus()
fileutils.FindRootDir()
logrus.Info("SetupTest")

 var config = ichubconfig.Default()
 clientDto := config.ReadIchubWebClient()
 suite.webclient = webclient.New(clientDto)
 suite.statwebcli = webclient.DefaultStat()

}

func (suite *TestWebSuite) Test0001_WebClient() {
logrus.Info(suite.webclient)

 var result = suite.webclient.Get("")
 logrus.Info(result)

}

    1. ESWebClient: ES客户端

go get -u gitlab.ichub.com/general/esserver

Goinstall.bat

9d513268a6f5410790440149f6418edc.png

通用查询

func (this *TestWebQuerySuite) Test003_ShouldQueryDept() {
var queryRequest = pagereq.NewQueryRequest()
queryRequest.PageSize = 2
queryRequest.IndexName = "ichub_sys_dept"
queryRequest.EsTerms("dept_name", "若依科技", "kl")
queryRequest.EsTerms("dept_name", "若依科技", "kl")
queryRequest.Source = "dept_id,dept_name,order_nm,create_time"
queryRequest.OrderByDesc("create_time")

 var result = this.EsWebClient.Should(queryRequest).As(&[]*model.SysDept{})

 queryRequest.Save2File(queryRequest, result)
 logrus.Info(result)
 //check
 this.Equal(200, result.Code)

}

执行结果

213f87a64b8244c382c4ca76e6688ea5.png

    1. RuleWebClient : 规则引擎客户端

go get -u gitlab.ichub.com/general/ruleserver

Goinstall.bat

6f5e7cb0c2314b30907cbf30df957716.png

动态执行规则脚本

333f09b106974a53a7b5c805a9d095f9.png

  1. git路径1. ES客户端:简化版本

http://git.ichub.com/general/webcli.git

    1. WEB框架全集:

http://git.ichub.com/general/webcli120.git

    1. ESSERVER统一搜索服务

http://[email protected]/ichub2/esserver.git

    1. RULESERVER统一规则引擎服务

http://[email protected]/ichub2/ruleserver.git

    1. 网站管理独立微服务

http://[email protected]/general/website-management.git

vv

标签: golang

本文转载自: https://blog.csdn.net/leijmdas/article/details/140853440
版权归原作者 leijmdas 所有, 如有侵权,请联系我们删除。

“WEB框架一键生成神器:WEBClient&SERVER”的评论:

还没有评论