diff --git a/api/config.tpl b/api/config.tpl index 55127ef..81a8817 100644 --- a/api/config.tpl +++ b/api/config.tpl @@ -1,9 +1,29 @@ package config import {{.authImport}} - +import type Config struct { rest.RestConf {{.auth}} {{.jwtTrans}} + Mysql struct { + Host string `json:"host"` + Port int `json:"port"` + User string `json:"user"` + Password string `json:"password"` + Database string `json:"database"` + Charset string `json:"charset"` + } + Redis struct { + Host string `json:"host"` + Port int `json:"port"` + Password string `json:"password"` + Database int `json:"database"` + } + Auth struct { + AccessSecret string `json:"accessSecret"` + AccessExpire int64 `json:"accessExpire"` + RefreshSecret string `json:"refreshSecret"` + RefreshExpire int64 `json:"refreshExpire"` + } } diff --git a/api/context.tpl b/api/context.tpl index c15c1e4..5a7eb05 100644 --- a/api/context.tpl +++ b/api/context.tpl @@ -2,16 +2,44 @@ package svc import ( {{.configImport}} + // 默认引入 gorm, 方便后续使用 + "gorm.io/gorm" + // Mysql + "gorm.io/driver/mysql" + // Redis + "github.com/go-redis/redis/v8" + // fmt + "fmt" ) type ServiceContext struct { Config {{.config}} {{.middleware}} + DB *gorm.DB + Redis *redis.Client } func NewServiceContext(c {{.config}}) *ServiceContext { + // 初始化Mysql + mysql_dns := fmt.Sprintf("%s:%s@tcp(%s:%d)/%s?charset=utf8mb4&parseTime=True&loc=Local", c.MySQL.User, c.MySQL.Password, c.MySQL.Host, c.MySQL.Port, c.MySQL.DBName) + db, err := gorm.Open(mysql.Open(mysql_dns), &gorm.Config{}) + if err != nil { + panic(fmt.Sprintf("failed to connect to database: %v", err)) + } + // 初始化Redis + redisClient := redis.NewClient(&redis.Options{ + Addr: fmt.Sprintf("%s:%d", c.Redis.Host, c.Redis.Port), + Password: c.Redis.Password, // no password set + DB: c.Redis.DB, // use default DB + }) + _, err = redisClient.Ping(c.Context).Result() + if err != nil { + panic(fmt.Sprintf("failed to connect to redis: %v", err)) + } return &ServiceContext{ Config: c, {{.middlewareAssignment}} + DB: db, + Redis: redisClient, } } diff --git a/api/etc.tpl b/api/etc.tpl index ed55cf1..645a368 100644 --- a/api/etc.tpl +++ b/api/etc.tpl @@ -1,3 +1,21 @@ Name: {{.serviceName}} Host: {{.host}} Port: {{.port}} +Mysql: + Host: 127.0.0.1 + Port: 3306 + User: root + Password: root + Database: Database + Charset: utf8mb4 +Redis: + Host: 127.0.0.1 + Port: 6379 + Password: root + Database: 0 +Auth: + AccessSecret: dev.gxxhygroup.com + AccessExpire: 604800 + RefreshSecret: dev.gxxhygroup.com + RefreshExpire: 2592000 + diff --git a/api/logic.tpl b/api/logic.tpl index 94611c5..5db94f8 100644 --- a/api/logic.tpl +++ b/api/logic.tpl @@ -2,6 +2,10 @@ package {{.pkgName}} import ( {{.imports}} + // github.com/spf13/cast // 类型转换 + // github.com/golang-module/carbon/v2 // 日期时间处理 + // github.com/jinzhu/copier/v2 // 结构体复制 + // orm 目录下的 orm 包 ) type {{.logic}} struct { diff --git a/api/main.tpl b/api/main.tpl index ad1a46d..b64b7d5 100644 --- a/api/main.tpl +++ b/api/main.tpl @@ -14,7 +14,7 @@ func main() { var c config.Config conf.MustLoad(*configFile, &c) - + server := rest.MustNewServer(c.RestConf) defer server.Stop()