[优化] 更新kit包

This commit is contained in:
PandaGoAdmin
2022-08-19 22:24:20 +08:00
parent 645d30adf3
commit 9522270543
18 changed files with 395 additions and 13 deletions

12
pkg/config/app.go Normal file
View File

@@ -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)
}

5
pkg/config/casbin.go Normal file
View File

@@ -0,0 +1,5 @@
package config
type Casbin struct {
ModelPath string `mapstructure:"model-path" json:"model-path" yaml:"model-path"`
}

65
pkg/config/config.go Normal file
View File

@@ -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
}

33
pkg/config/db.go Normal file
View File

@@ -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)
}

11
pkg/config/gen.go Normal file
View File

@@ -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"`
}

15
pkg/config/jwt.go Normal file
View File

@@ -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] 不能为空")
}

33
pkg/config/log.go Normal file
View File

@@ -0,0 +1,33 @@
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 l == nil {
return ""
}
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)
}

8
pkg/config/redis.go Normal file
View File

@@ -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"`
}

41
pkg/config/server.go Normal file
View File

@@ -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"` // 限流数量
}