suyiiyii 1f3066be16
feat(app): 添加 Dockerfile 和 .dockerignore 文件
- 在 app/facade 和 app/user 目录下新增 Dockerfile 文件,用于构建 Docker 镜像
- 在 app/facade 和 app/user目录下新增 .dockerignore 文件,用于指定不需要复制到容器中的文件和目录
- 更新 app/facade/conf/conf.go 和 app/user/conf/conf.go,使用 embed 包嵌入配置文件
- 移除不必要的库引用,简化代码结构
2025-01-20 23:41:18 +08:00

112 lines
2.1 KiB
Go

package conf
import (
_ "embed"
"os"
"sync"
"github.com/cloudwego/hertz/pkg/common/hlog"
"github.com/kr/pretty"
)
var (
conf *Config
once sync.Once
)
type Config struct {
Env string
Hertz Hertz `yaml:"hertz"`
MySQL MySQL `yaml:"mysql"`
Redis Redis `yaml:"redis"`
}
type MySQL struct {
DSN string `yaml:"dsn"`
}
type Redis struct {
Address string `yaml:"address"`
Password string `yaml:"password"`
Username string `yaml:"username"`
DB int `yaml:"db"`
}
type Hertz struct {
Service string `yaml:"service"`
Address string `yaml:"address"`
EnablePprof bool `yaml:"enable_pprof"`
EnableGzip bool `yaml:"enable_gzip"`
EnableAccessLog bool `yaml:"enable_access_log"`
LogLevel string `yaml:"log_level"`
LogFileName string `yaml:"log_file_name"`
LogMaxSize int `yaml:"log_max_size"`
LogMaxBackups int `yaml:"log_max_backups"`
LogMaxAge int `yaml:"log_max_age"`
}
// GetConf gets configuration instance
func GetConf() *Config {
once.Do(initConf)
return conf
}
//go:embed dev/conf.yaml
var configFile []byte
func initConf() {
//prefix := "conf"
//confFileRelPath := filepath.Join(prefix, filepath.Join(GetEnv(), "conf.yaml"))
//content, err := ioutil.ReadFile(confFileRelPath)
//if err != nil {
// panic(err)
//}
conf = new(Config)
err := yaml.Unmarshal(configFile, conf)
if err != nil {
hlog.Error("parse yaml error - %v", err)
panic(err)
}
if err := validator.Validate(conf); err != nil {
hlog.Error("validate config error - %v", err)
panic(err)
}
conf.Env = GetEnv()
pretty.Printf("%+v\n", conf)
}
func GetEnv() string {
e := os.Getenv("GO_ENV")
if len(e) == 0 {
return "test"
}
return e
}
func LogLevel() hlog.Level {
level := GetConf().Hertz.LogLevel
switch level {
case "trace":
return hlog.LevelTrace
case "debug":
return hlog.LevelDebug
case "info":
return hlog.LevelInfo
case "notice":
return hlog.LevelNotice
case "warn":
return hlog.LevelWarn
case "error":
return hlog.LevelError
case "fatal":
return hlog.LevelFatal
default:
return hlog.LevelInfo
}
}