1 Star 0 Fork 0

player1/restful-api-demo

加入 Gitee
与超过 1400万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
config.go 2.28 KB
一键复制 编辑 原始数据 按行查看 历史
package conf
import "fmt"
// 为了不被程序运行时被修改,设置成私有变量
var config *Config
// 获取配置
func C() *Config {
if config == nil {
panic("Load config first")
}
//fmt.Println(config)
return config
}
// 将所有应用配置封装成一个对象,来与外部配置
type Config struct {
App *App `toml:"app"`
Log *Log `toml:"log"`
MySQL *MySQL `toml:"mysql"`
}
type App struct {
Name string `toml:"name" env:"APP_NAME"`
Host string `toml:"host" env:"APP_HOST"`
Port string `toml:"port" env:"APP_PORT"`
Key string `toml:"key" env:"APP_KEY"`
/* EnableSSL bool `toml:"enable_ssl" env:"APP_ENABLE_SSL"`
CertFile string `toml:"cert_file" env:"APP_CERT_FILE"`
KeyFile string `toml:"key_file" env:"APP_KEY_FILE"`*/
}
type Log struct {
Level string `toml:"level" env:"LOG_LEVEL"`
PathDir string `toml:"path_dir" env:"LOG_PATH_DIR"`
Format LogFormat `toml:"format" env:"LOG_FORMAT"`
To LogTo `toml:"to" env:"LOG_TO"`
}
type MySQL struct {
Host string `toml:"host" env:"MYSQL_HOST"`
Port string `toml:"port" env:"MYSQL_PORT"`
UserName string `toml:"username" env:"MYSQL_USERNAME"`
Password string `toml:"password" env:"MYSQL_PASSWORD"`
Database string `toml:"database" env:"MYSQL_DATABASE"`
MaxOpenConn int `toml:"max_open_conn" env:"MYSQL_MAX_OPEN_CONN"`
MaxIdleConn int `toml:"max_idle_conn" env:"MYSQL_MAX_IDLE_CONN"`
MaxLifeTime int `toml:"max_life_time" env:"MYSQL_MAX_LIFE_TIME"`
MaxIdleTime int `toml:"max_idle_time" env:"MYSQL_MAX_idle_TIME"`
//lock sync.Mutex
}
func NewDefaultApp() *App {
return &App{
Name: "demo",
Host: "127.0.0.1",
Port: "8080",
Key: "",
}
}
func NewDefaultMySQL() *MySQL {
return &MySQL{
Host: "127.0.0.1",
Port: "33060",
UserName: "root",
Password: "my-secret-pw",
Database: "demo",
MaxIdleConn: 100,
MaxOpenConn: 100,
MaxLifeTime: 60 * 60,
MaxIdleTime: 60 * 60,
}
}
func NewDefaultLog() *Log {
return &Log{
Level: "info",
PathDir: ".",
Format: TextFormat,
To: ToStdout,
}
}
func NewDefaultConfig() *Config {
return &Config{
App: NewDefaultApp(),
MySQL: NewDefaultMySQL(),
Log: NewDefaultLog(),
}
}
func (a *App) HttpAddr() string {
return fmt.Sprintf("%s:%s", a.Host, a.Port)
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/player1111/restful-api-demo.git
git@gitee.com:player1111/restful-api-demo.git
player1111
restful-api-demo
restful-api-demo
19fd67857982

搜索帮助