This commit is contained in:
@@ -1,121 +1,123 @@
|
||||
package coolconfig
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"github.com/gogf/gf/v2/frame/g"
|
||||
"github.com/gogf/gf/v2/util/gconv"
|
||||
)
|
||||
|
||||
// cool config
|
||||
type sConfig struct {
|
||||
AutoMigrate bool `json:"auto_migrate,omitempty"` // 是否自动创建表
|
||||
Eps bool `json:"eps,omitempty"` // 是否开启eps
|
||||
File *file `json:"file,omitempty"` // 文件上传配置
|
||||
Name string `json:"name"` // 项目名称
|
||||
// LoginPort string `json:"port"`
|
||||
GameOnlineID uint32 `json:"port_bl"` //这个是命令行输入的参数
|
||||
ServerInfo ServerList
|
||||
|
||||
Address string //rpc端口
|
||||
|
||||
}
|
||||
type ServerList struct {
|
||||
OnlineID uint32 `gorm:"column:online_id;comment:'在线ID';uniqueIndex" json:"online_id"`
|
||||
//服务器名称Desc
|
||||
Name string `gorm:"comment:'服务器名称'" json:"name"`
|
||||
IP string `gorm:"type:string;comment:'服务器IP'" json:"ip"`
|
||||
Port uint32 `gorm:"comment:'端口号,通常是小整数'" json:"port"`
|
||||
IsOpen uint8 `gorm:"default:0;not null;comment:'是否开启'" json:"is_open"`
|
||||
//登录地址
|
||||
LoginAddr string `gorm:"type:string;comment:'登录地址'" json:"login_addr"`
|
||||
//账号
|
||||
Account string `gorm:"type:string;comment:'账号'" json:"account"`
|
||||
//密码
|
||||
Password string `gorm:"type:string;comment:'密码'" json:"password"`
|
||||
CanPort []uint32 `gorm:"type:jsonb;comment:'可连接端口'" json:"can_port"`
|
||||
//是否测试服
|
||||
IsVip uint32 `gorm:"default:0;not null;comment:'是否为VIP服务器'" json:"is_vip"`
|
||||
//isdebug 是否本地服
|
||||
IsDebug uint8 `gorm:"default:0;comment:'是否为调试模式'" json:"is_debug"`
|
||||
|
||||
//服务器属主Desc
|
||||
Owner uint32 `gorm:"comment:'服务器属主'" json:"owner"`
|
||||
Desc string `gorm:"comment:'服务器描述'" json:"desc"`
|
||||
OldScreen string `gorm:"comment:'服务器screen参数'" json:"old_screen"`
|
||||
//到期时间ServerList
|
||||
ExpireTime time.Time `gorm:"default:0;comment:'到期时间'" json:"expire_time"`
|
||||
}
|
||||
|
||||
func (s *ServerList) GetID() string {
|
||||
return gconv.String(100000*s.OnlineID + s.Port)
|
||||
}
|
||||
|
||||
// OSS相关配置
|
||||
type oss struct {
|
||||
Endpoint string `json:"endpoint"`
|
||||
AccessKeyID string `json:"accessKeyID"`
|
||||
SecretAccessKey string `json:"secretAccessKey"`
|
||||
UseSSL bool `json:"useSSL"`
|
||||
BucketName string `json:"bucketName"`
|
||||
Location string `json:"location"`
|
||||
}
|
||||
|
||||
// 文件上传配置
|
||||
type file struct {
|
||||
Mode string `json:"mode"` // 模式 local oss
|
||||
Domain string `json:"domain"` // 域名 http://
|
||||
Oss *oss `json:"oss,omitempty"`
|
||||
}
|
||||
|
||||
// NewConfig new config
|
||||
func newConfig() *sConfig {
|
||||
var ctx g.Ctx
|
||||
config := &sConfig{
|
||||
AutoMigrate: GetCfgWithDefault(ctx, "blazing.autoMigrate", g.NewVar(false)).Bool(),
|
||||
Name: GetCfgWithDefault(ctx, "server.name", g.NewVar("")).String(),
|
||||
|
||||
Eps: GetCfgWithDefault(ctx, "blazing.eps", g.NewVar(false)).Bool(),
|
||||
// LoginPort: string(GetCfgWithDefault(ctx, "server.port", g.NewVar("8080")).String()),
|
||||
Address: GetCfgWithDefault(ctx, "server.address", g.NewVar("8080")).String(),
|
||||
//GamePort: GetCfgWithDefault(ctx, "server.game", g.NewVar("8080")).Uint64s(),
|
||||
|
||||
File: &file{
|
||||
Mode: GetCfgWithDefault(ctx, "blazing.file.mode", g.NewVar("none")).String(),
|
||||
Domain: GetCfgWithDefault(ctx, "blazing.file.domain", g.NewVar("http://127.0.0.1:8300")).String(),
|
||||
Oss: &oss{
|
||||
Endpoint: GetCfgWithDefault(ctx, "blazing.file.oss.endpoint", g.NewVar("127.0.0.1:9000")).String(),
|
||||
AccessKeyID: GetCfgWithDefault(ctx, "blazing.file.oss.accessKeyID", g.NewVar("")).String(),
|
||||
SecretAccessKey: GetCfgWithDefault(ctx, "blazing.file.oss.secretAccessKey", g.NewVar("")).String(),
|
||||
UseSSL: GetCfgWithDefault(ctx, "blazing.file.oss.useSSL", g.NewVar(false)).Bool(),
|
||||
BucketName: GetCfgWithDefault(ctx, "blazing.file.oss.bucketName", g.NewVar("blazing")).String(),
|
||||
Location: GetCfgWithDefault(ctx, "blazing.file.oss.location", g.NewVar("us-east-1")).String(),
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
return config
|
||||
}
|
||||
|
||||
// qiniu 七牛云配置
|
||||
type qiniu struct {
|
||||
AccessKey string `json:"ak"`
|
||||
SecretKey string `json:"sk"`
|
||||
Bucket string `json:"bucket"`
|
||||
CDN string `json:"cdn"`
|
||||
}
|
||||
|
||||
// Config config
|
||||
var Config = newConfig()
|
||||
|
||||
// GetCfgWithDefault get config with default value
|
||||
func GetCfgWithDefault(ctx g.Ctx, key string, defaultValue *g.Var) *g.Var {
|
||||
value, err := g.Cfg().GetWithEnv(ctx, key)
|
||||
if err != nil {
|
||||
return defaultValue
|
||||
}
|
||||
if value.IsEmpty() || value.IsNil() {
|
||||
return defaultValue
|
||||
}
|
||||
return value
|
||||
}
|
||||
package coolconfig
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"github.com/gogf/gf/v2/frame/g"
|
||||
"github.com/gogf/gf/v2/util/gconv"
|
||||
)
|
||||
|
||||
// cool config
|
||||
type sConfig struct {
|
||||
AutoMigrate bool `json:"auto_migrate,omitempty"` // 是否自动创建表
|
||||
Eps bool `json:"eps,omitempty"` // 是否开启eps
|
||||
File *file `json:"file,omitempty"` // 文件上传配置
|
||||
Name string `json:"name"` // 项目名称
|
||||
// LoginPort string `json:"port"`
|
||||
GameOnlineID uint32 `json:"port_bl"` //这个是命令行输入的参数
|
||||
ServerInfo ServerList
|
||||
|
||||
Address string //rpc端口
|
||||
|
||||
}
|
||||
type ServerList struct {
|
||||
OnlineID uint32 `gorm:"column:online_id;comment:'在线ID';uniqueIndex" json:"online_id"`
|
||||
//服务器名称Desc
|
||||
Name string `gorm:"comment:'服务器名称'" json:"name"`
|
||||
IP string `gorm:"type:string;comment:'服务器IP'" json:"ip"`
|
||||
Port uint32 `gorm:"comment:'端口号,通常是小整数'" json:"port"`
|
||||
IsOpen uint8 `gorm:"default:0;not null;comment:'是否开启'" json:"is_open"`
|
||||
//登录地址
|
||||
LoginAddr string `gorm:"type:string;comment:'登录地址'" json:"login_addr"`
|
||||
//账号
|
||||
Account string `gorm:"type:string;comment:'账号'" json:"account"`
|
||||
//密码
|
||||
Password string `gorm:"type:string;comment:'密码'" json:"password"`
|
||||
CanPort []uint32 `gorm:"type:jsonb;comment:'可连接端口'" json:"can_port"`
|
||||
//是否测试服
|
||||
IsVip uint32 `gorm:"default:0;not null;comment:'是否为VIP服务器'" json:"is_vip"`
|
||||
//isdebug 是否本地服
|
||||
IsDebug uint8 `gorm:"default:0;comment:'是否为调试模式'" json:"is_debug"`
|
||||
|
||||
//服务器属主Desc
|
||||
Owner uint32 `gorm:"comment:'服务器属主'" json:"owner"`
|
||||
Desc string `gorm:"comment:'服务器描述'" json:"desc"`
|
||||
OldScreen string `gorm:"comment:'服务器screen参数'" json:"old_screen"`
|
||||
//到期时间ServerList
|
||||
ExpireTime time.Time `gorm:"default:0;comment:'到期时间'" json:"expire_time"`
|
||||
//CDK冠名到期时间ServerList
|
||||
CDKExpireTime time.Time `gorm:"column:cdk_expire_time;default:0;comment:'CDK冠名到期时间'" json:"cdk_expire_time"`
|
||||
}
|
||||
|
||||
func (s *ServerList) GetID() string {
|
||||
return gconv.String(100000*s.OnlineID + s.Port)
|
||||
}
|
||||
|
||||
// OSS相关配置
|
||||
type oss struct {
|
||||
Endpoint string `json:"endpoint"`
|
||||
AccessKeyID string `json:"accessKeyID"`
|
||||
SecretAccessKey string `json:"secretAccessKey"`
|
||||
UseSSL bool `json:"useSSL"`
|
||||
BucketName string `json:"bucketName"`
|
||||
Location string `json:"location"`
|
||||
}
|
||||
|
||||
// 文件上传配置
|
||||
type file struct {
|
||||
Mode string `json:"mode"` // 模式 local oss
|
||||
Domain string `json:"domain"` // 域名 http://
|
||||
Oss *oss `json:"oss,omitempty"`
|
||||
}
|
||||
|
||||
// NewConfig new config
|
||||
func newConfig() *sConfig {
|
||||
var ctx g.Ctx
|
||||
config := &sConfig{
|
||||
AutoMigrate: GetCfgWithDefault(ctx, "blazing.autoMigrate", g.NewVar(false)).Bool(),
|
||||
Name: GetCfgWithDefault(ctx, "server.name", g.NewVar("")).String(),
|
||||
|
||||
Eps: GetCfgWithDefault(ctx, "blazing.eps", g.NewVar(false)).Bool(),
|
||||
// LoginPort: string(GetCfgWithDefault(ctx, "server.port", g.NewVar("8080")).String()),
|
||||
Address: GetCfgWithDefault(ctx, "server.address", g.NewVar("8080")).String(),
|
||||
//GamePort: GetCfgWithDefault(ctx, "server.game", g.NewVar("8080")).Uint64s(),
|
||||
|
||||
File: &file{
|
||||
Mode: GetCfgWithDefault(ctx, "blazing.file.mode", g.NewVar("none")).String(),
|
||||
Domain: GetCfgWithDefault(ctx, "blazing.file.domain", g.NewVar("http://127.0.0.1:8300")).String(),
|
||||
Oss: &oss{
|
||||
Endpoint: GetCfgWithDefault(ctx, "blazing.file.oss.endpoint", g.NewVar("127.0.0.1:9000")).String(),
|
||||
AccessKeyID: GetCfgWithDefault(ctx, "blazing.file.oss.accessKeyID", g.NewVar("")).String(),
|
||||
SecretAccessKey: GetCfgWithDefault(ctx, "blazing.file.oss.secretAccessKey", g.NewVar("")).String(),
|
||||
UseSSL: GetCfgWithDefault(ctx, "blazing.file.oss.useSSL", g.NewVar(false)).Bool(),
|
||||
BucketName: GetCfgWithDefault(ctx, "blazing.file.oss.bucketName", g.NewVar("blazing")).String(),
|
||||
Location: GetCfgWithDefault(ctx, "blazing.file.oss.location", g.NewVar("us-east-1")).String(),
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
return config
|
||||
}
|
||||
|
||||
// qiniu 七牛云配置
|
||||
type qiniu struct {
|
||||
AccessKey string `json:"ak"`
|
||||
SecretKey string `json:"sk"`
|
||||
Bucket string `json:"bucket"`
|
||||
CDN string `json:"cdn"`
|
||||
}
|
||||
|
||||
// Config config
|
||||
var Config = newConfig()
|
||||
|
||||
// GetCfgWithDefault get config with default value
|
||||
func GetCfgWithDefault(ctx g.Ctx, key string, defaultValue *g.Var) *g.Var {
|
||||
value, err := g.Cfg().GetWithEnv(ctx, key)
|
||||
if err != nil {
|
||||
return defaultValue
|
||||
}
|
||||
if value.IsEmpty() || value.IsNil() {
|
||||
return defaultValue
|
||||
}
|
||||
return value
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user