From 26ea60a74d3d1999ad318facb0393ce1b8171c98 Mon Sep 17 00:00:00 2001 From: StellarisW Date: Sat, 4 Mar 2023 09:09:31 +0800 Subject: [PATCH] optimize: merge gorm init with initialisation code --- docs/layout.md | 12 ---- tpl/hertz/standard/layout.yaml | 78 ----------------------- tpl/kitex/server/standard/dal_init.yaml | 15 ----- tpl/kitex/server/standard/mysql.yaml | 29 --------- tpl/kitex/server/standard/readme_tpl.yaml | 1 - tpl/kitex/server/standard/redis.yaml | 27 -------- 6 files changed, 162 deletions(-) delete mode 100644 tpl/kitex/server/standard/dal_init.yaml delete mode 100644 tpl/kitex/server/standard/mysql.yaml delete mode 100644 tpl/kitex/server/standard/redis.yaml diff --git a/docs/layout.md b/docs/layout.md index 2ed32d2e..598bd659 100644 --- a/docs/layout.md +++ b/docs/layout.md @@ -7,12 +7,6 @@ cwgo 工具支持目前支持生成 MVC Layout,未来还会拓展更多的模 ``` ├── biz // 业务逻辑目录 -│   ├── dal // 数据访问层 -│   │   ├── init.go -│   │   ├── mysql -│   │   │   └── init.go -│   │   └── redis -│   │   └── init.go │   ├── handler // view 层 │   │   └── hello │   │   └── example @@ -48,12 +42,6 @@ RPC 项目目录如下 ``` ├── biz // 业务逻辑目录 -│   ├── dal // 数据访问层 -│   │   ├── init.go -│   │   ├── mysql -│   │   │   └── init.go -│   │   └── redis -│   │   └── init.go │   └── service // service 层,业务逻辑存放的地方。更新时,新的方法会追加文件。 │   ├── HelloMethod.go │   └── HelloMethod_test.go diff --git a/tpl/hertz/standard/layout.yaml b/tpl/hertz/standard/layout.yaml index 1d8054d7..55e50cea 100644 --- a/tpl/hertz/standard/layout.yaml +++ b/tpl/hertz/standard/layout.yaml @@ -28,8 +28,6 @@ layouts: ) func main() { - // init dal - // dal.Init() address := conf.GetConf().Hertz.Address h := server.New(server.WithHostPorts(address)) @@ -292,81 +290,6 @@ layouts: address: "127.0.0.1:6379" password: "" - - - path: biz/dal/init.go - delims: - - "" - - "" - body: |- - package dal - - import ( - "{{.GoModule}}/biz/dal/mysql" - "{{.GoModule}}/biz/dal/redis" - ) - - func Init() { - redis.Init() - mysql.Init() - } - - - path: biz/dal/mysql/init.go - delims: - - "" - - "" - body: |- - package mysql - - import ( - "{{.GoModule}}/conf" - "gorm.io/driver/mysql" - "gorm.io/gorm" - ) - - var ( - DB *gorm.DB - err error - ) - - func Init() { - DB, err = gorm.Open(mysql.Open(conf.GetConf().MySQL.DSN), - &gorm.Config{ - PrepareStmt: true, - SkipDefaultTransaction: true, - }, - ) - if err != nil { - panic(err) - } - } - - - - path: biz/dal/redis/init.go - delims: - - "" - - "" - body: |- - package redis - - import ( - "context" - - "github.com/go-redis/redis/v8" - "{{.GoModule}}/conf" - ) - - var RedisClient *redis.Client - - func Init() { - RedisClient = redis.NewClient(&redis.Options{ - Addr: conf.GetConf().Redis.Address, - Password: conf.GetConf().Redis.Password, - }) - if err := RedisClient.Ping(context.Background()).Err(); err != nil { - panic(err) - } - } - - path: docker-compose.yaml delims: - "" @@ -412,7 +335,6 @@ layouts: | hertz_gen | Hertz generated model | | biz/handler | Used for request processing, validation and return of response. | | biz/service | The actual business logic. | - | biz/dal | Logic for operating the storage layer | | biz/route | Routing and middleware registration | | biz/utils | Wrapped some common methods | diff --git a/tpl/kitex/server/standard/dal_init.yaml b/tpl/kitex/server/standard/dal_init.yaml deleted file mode 100644 index 37190664..00000000 --- a/tpl/kitex/server/standard/dal_init.yaml +++ /dev/null @@ -1,15 +0,0 @@ -path: biz/dal/init.go -update_behavior: - type: skip -body: |- - package dal - - import ( - "{{.Module}}/biz/dal/mysql" - "{{.Module}}/biz/dal/redis" - ) - - func Init() { - redis.Init() - mysql.Init() - } \ No newline at end of file diff --git a/tpl/kitex/server/standard/mysql.yaml b/tpl/kitex/server/standard/mysql.yaml deleted file mode 100644 index 8e2106f2..00000000 --- a/tpl/kitex/server/standard/mysql.yaml +++ /dev/null @@ -1,29 +0,0 @@ -path: biz/dal/mysql/init.go -update_behavior: - type: skip -body: |- - package mysql - - import ( - "{{.Module}}/conf" - - "gorm.io/driver/mysql" - "gorm.io/gorm" - ) - - var ( - DB *gorm.DB - err error - ) - - func Init() { - DB, err = gorm.Open(mysql.Open(conf.GetConf().MySQL.DSN), - &gorm.Config{ - PrepareStmt: true, - SkipDefaultTransaction: true, - }, - ) - if err != nil { - panic(err) - } - } \ No newline at end of file diff --git a/tpl/kitex/server/standard/readme_tpl.yaml b/tpl/kitex/server/standard/readme_tpl.yaml index 4407d477..0058132a 100644 --- a/tpl/kitex/server/standard/readme_tpl.yaml +++ b/tpl/kitex/server/standard/readme_tpl.yaml @@ -20,7 +20,6 @@ body: |- | handler.go | Used for request processing return of response. | | kitex_gen | kitex generated code | | biz/service | The actual business logic. | - | biz/dal | Logic for operating the storage layer | ## How to run diff --git a/tpl/kitex/server/standard/redis.yaml b/tpl/kitex/server/standard/redis.yaml deleted file mode 100644 index cdb93679..00000000 --- a/tpl/kitex/server/standard/redis.yaml +++ /dev/null @@ -1,27 +0,0 @@ -path: biz/dal/redis/init.go -update_behavior: - type: skip -body: |- - package redis - - import ( - "context" - - "github.com/go-redis/redis/v8" - "{{.Module}}/conf" - ) - - var ( - RedisClient *redis.Client - ) - - func Init() { - RedisClient = redis.NewClient(&redis.Options{ - Addr: conf.GetConf().Redis.Address, - Username: conf.GetConf().Redis.Username, - Password: conf.GetConf().Redis.Password, - }) - if err := RedisClient.Ping(context.Background()).Err(); err != nil { - panic(err) - } - } \ No newline at end of file