From cf97d854d86e358ecd5b6c4876b432af1ac107b8 Mon Sep 17 00:00:00 2001 From: PandaGoAdmin <18610165312@163.com> Date: Fri, 19 Aug 2022 17:58:02 +0800 Subject: [PATCH] =?UTF-8?q?=E3=80=90=E6=9B=B4=E6=96=B0=E3=80=91=E6=9B=B4?= =?UTF-8?q?=E6=96=B0pg=E4=BB=A3=E7=A0=81=E7=94=9F=E6=88=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- apps/develop/services/gen_table.go | 2 +- pkg/config/app.go | 12 +++++ pkg/config/casbin.go | 5 +++ pkg/config/config.go | 65 +++++++++++++++++++++++++++ pkg/config/db.go | 33 ++++++++++++++ pkg/config/gen.go | 11 +++++ pkg/config/jwt.go | 15 +++++++ pkg/config/log.go | 30 +++++++++++++ pkg/config/redis.go | 8 ++++ pkg/config/server.go | 41 +++++++++++++++++ resource/template/go/service.template | 11 ++++- 11 files changed, 231 insertions(+), 2 deletions(-) create mode 100644 pkg/config/app.go create mode 100644 pkg/config/casbin.go create mode 100644 pkg/config/config.go create mode 100644 pkg/config/db.go create mode 100644 pkg/config/gen.go create mode 100644 pkg/config/jwt.go create mode 100644 pkg/config/log.go create mode 100644 pkg/config/redis.go create mode 100644 pkg/config/server.go diff --git a/apps/develop/services/gen_table.go b/apps/develop/services/gen_table.go index 2fdf7c6..0514c6f 100644 --- a/apps/develop/services/gen_table.go +++ b/apps/develop/services/gen_table.go @@ -187,7 +187,7 @@ func (m *devGenTableModelImpl) Update(data entity.DevGenTable) *entity.DevGenTab t, ok := tableMap[data.Columns[i].LinkTableName] if ok { data.Columns[i].LinkTableClass = t.ClassName - data.Columns[i].LinkTablePackage = t.PackageName + data.Columns[i].LinkTablePackage = t.BusinessName data.Columns[i].LinkLabelId = t.PkColumn data.Columns[i].LinkLabelName = t.PkGoField } diff --git a/pkg/config/app.go b/pkg/config/app.go new file mode 100644 index 0000000..bef8b38 --- /dev/null +++ b/pkg/config/app.go @@ -0,0 +1,12 @@ +package config + +import "fmt" + +type App struct { + Name string `yaml:"name"` + Version string `yaml:"version"` +} + +func (a *App) GetAppInfo() string { + return fmt.Sprintf("[%s:%s]", a.Name, a.Version) +} diff --git a/pkg/config/casbin.go b/pkg/config/casbin.go new file mode 100644 index 0000000..4b5b196 --- /dev/null +++ b/pkg/config/casbin.go @@ -0,0 +1,5 @@ +package config + +type Casbin struct { + ModelPath string `mapstructure:"model-path" json:"model-path" yaml:"model-path"` +} diff --git a/pkg/config/config.go b/pkg/config/config.go new file mode 100644 index 0000000..8dedb78 --- /dev/null +++ b/pkg/config/config.go @@ -0,0 +1,65 @@ +package config + +import ( + "flag" + "fmt" + "github.com/XM-GO/PandaKit/biz" + "github.com/XM-GO/PandaKit/utils" + "path/filepath" +) + +var Conf *Config + +func InitConfig(configFilePath string) *Config { + // 获取启动参数中,配置文件的绝对路径 + path, _ := filepath.Abs(configFilePath) + startConfigParam = &CmdConfigParam{ConfigFilePath: path} + // 读取配置文件信息 + yc := &Config{} + if err := utils.LoadYml(startConfigParam.ConfigFilePath, yc); err != nil { + panic(any(fmt.Sprintf("读取配置文件[%s]失败: %s", startConfigParam.ConfigFilePath, err.Error()))) + } + // 校验配置文件内容信息 + yc.Valid() + + Conf = yc + return yc + +} + +// 启动配置参数 +type CmdConfigParam struct { + ConfigFilePath string // -e 配置文件路径 +} + +// 启动可执行文件时的参数 +var startConfigParam *CmdConfigParam + +// yaml配置文件映射对象 +type Config struct { + App *App `yaml:"app"` + Server *Server `yaml:"server"` + Jwt *Jwt `yaml:"jwt"` + Redis *Redis `yaml:"redis"` + Mysql *Mysql `yaml:"mysql"` + Postgresql *Postgresql `yaml:"postgresql"` + Casbin *Casbin `yaml:"casbin"` + Gen *Gen `yaml:"gen"` + Log *Log `yaml:"log"` +} + +// 配置文件内容校验 +func (c *Config) Valid() { + biz.IsTrue(c.Jwt != nil, "配置文件的[jwt]信息不能为空") + c.Jwt.Valid() +} + +// 获取执行可执行文件时,指定的启动参数 +func getStartConfig() *CmdConfigParam { + configFilePath := flag.String("e", "./config.yml", "配置文件路径,默认为可执行文件目录") + flag.Parse() + // 获取配置文件绝对路径 + path, _ := filepath.Abs(*configFilePath) + sc := &CmdConfigParam{ConfigFilePath: path} + return sc +} diff --git a/pkg/config/db.go b/pkg/config/db.go new file mode 100644 index 0000000..c2a2a26 --- /dev/null +++ b/pkg/config/db.go @@ -0,0 +1,33 @@ +package config + +import "fmt" + +type Mysql struct { + Host string `mapstructure:"host" json:"host" yaml:"host"` + Config string `mapstructure:"config" json:"config" yaml:"config"` + Dbname string `mapstructure:"db-name" json:"dbname" yaml:"db-name"` + Username string `mapstructure:"username" json:"username" yaml:"username"` + Password string `mapstructure:"password" json:"password" yaml:"password"` + MaxIdleConns int `mapstructure:"max-idle-conns" json:"maxIdleConns" yaml:"max-idle-conns"` + MaxOpenConns int `mapstructure:"max-open-conns" json:"maxOpenConns" yaml:"max-open-conns"` + LogMode bool `mapstructure:"log-mode" json:"logMode" yaml:"log-mode"` + LogZap string `mapstructure:"log-zap" json:"logZap" yaml:"log-zap"` +} + +func (m *Mysql) Dsn() string { + return m.Username + ":" + m.Password + "@tcp(" + m.Host + ")/" + m.Dbname + "?" + m.Config +} + +type Postgresql struct { + Host string `mapstructure:"host" json:"host" yaml:"host"` + Port int `mapstructure:"port" json:"port" yaml:"port"` + Dbname string `mapstructure:"db-name" json:"dbname" yaml:"db-name"` + Username string `mapstructure:"username" json:"username" yaml:"username"` + Password string `mapstructure:"password" json:"password" yaml:"password"` + MaxIdleConns int `mapstructure:"max-idle-conns" json:"maxIdleConns" yaml:"max-idle-conns"` + MaxOpenConns int `mapstructure:"max-open-conns" json:"maxOpenConns" yaml:"max-open-conns"` +} + +func (m *Postgresql) PgDsn() string { + return fmt.Sprintf("host=%s port=%d user=%s password=%s dbname=%s sslmode=disable", m.Host, m.Port, m.Username, m.Password, m.Dbname) +} diff --git a/pkg/config/gen.go b/pkg/config/gen.go new file mode 100644 index 0000000..fc6468c --- /dev/null +++ b/pkg/config/gen.go @@ -0,0 +1,11 @@ +package config + +/** + * @Description + * @Author Panda + * @Date 2021/12/31 15:13 + **/ +type Gen struct { + Dbname string `mapstructure:"dbname" json:"dbname" yaml:"dbname"` + Frontpath string `mapstructure:"frontpath" json:"frontpath" yaml:"frontpath"` +} diff --git a/pkg/config/jwt.go b/pkg/config/jwt.go new file mode 100644 index 0000000..c13b546 --- /dev/null +++ b/pkg/config/jwt.go @@ -0,0 +1,15 @@ +package config + +import ( + "github.com/XM-GO/PandaKit/biz" +) + +type Jwt struct { + Key string `yaml:"key"` + ExpireTime int64 `yaml:"expire-time"` // 过期时间,单位分钟 +} + +func (j *Jwt) Valid() { + biz.IsTrue(j.Key != "", "config.yml之 [jwt.key] 不能为空") + biz.IsTrue(j.ExpireTime != 0, "config.yml之 [jwt.expire-time] 不能为空") +} diff --git a/pkg/config/log.go b/pkg/config/log.go new file mode 100644 index 0000000..ac9815b --- /dev/null +++ b/pkg/config/log.go @@ -0,0 +1,30 @@ +package config + +import "path" + +type Log struct { + Level string `yaml:"level"` + File *LogFile `yaml:"file"` +} + +type LogFile struct { + Name string `yaml:"name"` + Path string `yaml:"path"` +} + +// 获取完整路径文件名 +func (l *LogFile) GetFilename() string { + var filepath, filename string + if fp := l.Path; fp == "" { + filepath = "./" + } else { + filepath = fp + } + if fn := l.Name; fn == "" { + filename = "default.log" + } else { + filename = fn + } + + return path.Join(filepath, filename) +} diff --git a/pkg/config/redis.go b/pkg/config/redis.go new file mode 100644 index 0000000..10316a4 --- /dev/null +++ b/pkg/config/redis.go @@ -0,0 +1,8 @@ +package config + +type Redis struct { + Host string `yaml:"host"` + Port int `yaml:"port"` + Password string `yaml:"password"` + Db int `yaml:"db"` +} diff --git a/pkg/config/server.go b/pkg/config/server.go new file mode 100644 index 0000000..2424a61 --- /dev/null +++ b/pkg/config/server.go @@ -0,0 +1,41 @@ +package config + +import "fmt" + +type Server struct { + Port int `yaml:"port"` + Model string `yaml:"model"` + Cors bool `yaml:"cors"` + Rate *Rate `yaml:"rate"` + IsInitTable bool `yaml:"isInitTable"` + DbType string `yaml:"db-type"` + ExcelDir string `yaml:"excel-dir"` + Tls *Tls `yaml:"tls"` + Static *[]*Static `yaml:"static"` + StaticFile *[]*StaticFile `yaml:"static-file"` +} + +func (s *Server) GetPort() string { + return fmt.Sprintf(":%d", s.Port) +} + +type Static struct { + RelativePath string `yaml:"relative-path"` + Root string `yaml:"root"` +} + +type StaticFile struct { + RelativePath string `yaml:"relative-path"` + Filepath string `yaml:"filepath"` +} + +type Tls struct { + Enable bool `yaml:"enable"` // 是否启用tls + KeyFile string `yaml:"key-file"` // 私钥文件路径 + CertFile string `yaml:"cert-file"` // 证书文件路径 +} + +type Rate struct { + Enable bool `yaml:"enable"` // 是否限流 + RateNum float64 `yaml:"rate-num"` // 限流数量 +} diff --git a/resource/template/go/service.template b/resource/template/go/service.template index da76ff0..3113f13 100644 --- a/resource/template/go/service.template +++ b/resource/template/go/service.template @@ -40,7 +40,13 @@ func (m *{{.BusinessName}}ModelImpl) Insert(data entity.{{$model}}) *entity.{{$m func (m *{{.BusinessName}}ModelImpl) FindOne({{.PkJsonField}} int64) *entity.{{$model}} { resData := new(entity.{{$model}}) - err := global.Db.Table(m.table).Where("{{.PkColumn}} = ?", {{.PkJsonField}}).First(resData).Error + db := global.Db.Table(m.table).Where("{{.PkColumn}} = ?", {{.PkJsonField}}) + {{- range $index, $column := .Columns -}} + {{- if ne $column.LinkTableName "" }} + db.Preload("{{$column.LinkTableClass}}") + {{- end -}} + {{- end}} + err := db.First(resData).Error biz.ErrIsNil(err, "查询{{.TableComment}}失败") return resData } @@ -179,6 +185,9 @@ func (m *{{.BusinessName}}ModelImpl) FindList(data entity.{{$model}}) *[]entity. {{- if eq $column.ColumnName "delete_time" }} db.Where("delete_time IS NULL") {{- end -}} + {{- if ne $column.LinkTableName "" }} + db.Preload("{{$column.LinkTableClass}}") + {{- end -}} {{- end}} biz.ErrIsNil(db.Order("create_time").Find(&list).Error, "查询{{.TableComment}}列表失败") return &list