后端服务模板 使用 laya 搭建 旨在快速搭建中小型应用服务, restfulApi, rpc服务 ==
Please note that this repository is out-of-the-box template
- 参考mian.go
// ServerSetup 初始化服务设置
func ServerSetup() *xgs.App {
app := xgs.NewApp(xgs.SetLogger)
//app.Use(conf.Load)
return app
}
func main() {
app := ServerSetup()
// your code
app.Use(listenHttp)
}
func listenHttp() {
http.HandleFunc("/reload", func(w http.ResponseWriter, r *http.Request) {
_, _ = w.Write([]byte(utils.RespSuc))
})
http.HandleFunc("/ready", func(w http.ResponseWriter, r *http.Request) {
_, _ = w.Write([]byte(utils.RespSuc))
})
http.HandleFunc("/healthz", func(w http.ResponseWriter, r *http.Request) {
_, _ = w.Write([]byte(utils.RespSuc))
})
log.Printf("http_listen: %s", genv.HttpListen())
_ = http.ListenAndServe(genv.HttpListen(), nil)
}
- 运行服务时配置,具体配置请参照conf/app.toml,按照结构配置,base和log和mem提供默认配置可以不配
- 别问为什么用json,问就是不知道
- 参考配置说明
- github链接
- 总的来说就是自己的日常开发形成的一套习惯,代码拉下来就可以运行,方便新api或者web项目的快速搭建 组件都是目前golang比较火的组件
- clone代码
git clone https://github.com/layasugar/laya-template.git
- 修改配置conf/app.json
- 启动
go run github.com/layasugar/laya-template
- docker方式启动
docker build -t laya-go:1.0 . && docker run --name laya-go -p 10080:10080 --network devops --network-alias laya-go laya-go:1.0
- https://github.com/gin-gonic/gin
- gin的原来的方式创建路由没有任何修改
- 参考routes/base.go
- 参考controller/base.go
- 传统的ddd模式
- page业务层
- data数据层
- dao连接或者请求层
- 请参考gin的middleware
app.WebServer.Use(middleware.LogInParams)
- uber开源的zap日志github链接
- 使用方式一(会打印request_id)
glogs.InfoFR(c,format,...interface)
glogs.WarnFR(c,format,...interface)
glogs.ErrorFR(c,format,...interface)
- 使用方式二(不会打印request_id)
glogs.InfoF(format,...interface)
glogs.WarnF(format,...interface)
- 可自己拓展
- 开源的zipkin的sdkgithub链接
- 使用
span := glogs.StartTrace(c,funcName)
your code ...
glogs.StopTrace(span)
- 使用
var d = &glogs.AlarmData{
RobotKey: robotKey,
RobotHost: robotHost,
Title: "这是标题",
Description: "这是问题描述",
Content: map[string]interface{}{
"adasdasd": "sadasssss",
"sadddd": "11111111",
}}
glogs.SendAlarm(d)
- github链接
- 使用
data.GetMem(),data.SetMem(),data.DelMem()
- 定义在global/errno/里面
- 使用utils.SystemErr
- 返回的是error类型,response里面会根据error不同而加载错误信息
- time.go是基于sql.nulltime自己实现的
- helps是辅助工具类
- redis_lock是实现分布式的redis锁
- ants.pool
p, _ := ants.NewPoolWithFunc(runtime.NumCPU()*2, func(data interface{}) {
if pData, ok := data.([]OtsOrder); ok {
err := batchWriteOtsOrder(pData)
if err != nil {
glogs.Error("WorkerRunOnlineCourse err:%v", err.Error())
}
}
})
defer p.Release()
// Submit tasks one by one.
for data := range BatchOtsOrderChan {
_ = p.Invoke(data)
}
- github链接
db2struct --host localhost -d database --package db -p 123456 --user root --guregu --gorm -t tableName --struct structName
- github链接
hey -n 100 -c 1000
//replace github.com/layasugar/laya => ./../laya // //replace github.com/layasugar/laya/glogs => ./../glogs