generate facade service

This commit is contained in:
2025-01-20 21:17:47 +08:00
parent 57b1f544d4
commit 98273c7645
38 changed files with 3307 additions and 1 deletions
+110
View File
@@ -0,0 +1,110 @@
package conf
import (
"io/ioutil"
"os"
"path/filepath"
"sync"
"github.com/cloudwego/hertz/pkg/common/hlog"
"github.com/kr/pretty"
"gopkg.in/validator.v2"
"gopkg.in/yaml.v2"
)
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
}
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(content, 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
}
}
+20
View File
@@ -0,0 +1,20 @@
hertz:
service: "facade"
address: ":8080"
enable_pprof: true
enable_gzip: true
enable_access_log: true
log_level: info
log_file_name: "log/hertz.log"
log_max_size: 10
log_max_age: 3
log_max_backups: 50
mysql:
dsn: "gorm:gorm@tcp(127.0.0.1:3306)/gorm?charset=utf8mb4&parseTime=True&loc=Local"
redis:
address: "127.0.0.1:6379"
username: ""
password: ""
db: 0
+20
View File
@@ -0,0 +1,20 @@
hertz:
service: "facade"
address: ":8080"
enable_pprof: false
enable_gzip: true
enable_access_log: true
log_level: info
log_file_name: "log/hertz.log"
log_max_size: 10
log_max_age: 3
log_max_backups: 50
mysql:
dsn: "gorm:gorm@tcp(127.0.0.1:3306)/gorm?charset=utf8mb4&parseTime=True&loc=Local"
redis:
address: "127.0.0.1:6379"
username: ""
password: ""
db: 0
+20
View File
@@ -0,0 +1,20 @@
hertz:
service: "facade"
address: ":8080"
enable_pprof: true
enable_gzip: true
enable_access_log: true
log_level: info
log_file_name: "log/hertz.log"
log_max_size: 10
log_max_age: 3
log_max_backups: 50
mysql:
dsn: "gorm:gorm@tcp(127.0.0.1:3306)/gorm?charset=utf8mb4&parseTime=True&loc=Local"
redis:
address: "127.0.0.1:6379"
username: ""
password: ""
db: 0