Skip to content

Commit

Permalink
refactor code
Browse files Browse the repository at this point in the history
  • Loading branch information
hantmac committed Dec 13, 2019
1 parent d263b23 commit 24ea99c
Show file tree
Hide file tree
Showing 28 changed files with 19 additions and 16 deletions.
4 changes: 4 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
*.css linguist-language=go
*.less linguist-language=go
*.js linguist-language=go
*.html linguist-language=go
21 changes: 10 additions & 11 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ type Config struct {
Name string
}

// 监控配置文件变化并热加载程序
func (c *Config) WatchConfig() {
viper.WatchConfig()
viper.OnConfigChange(func(e fsnotify.Event) {
Expand All @@ -21,17 +20,17 @@ func (c *Config) WatchConfig() {

func (c *Config) Init() error {
if c.Name != "" {
viper.SetConfigFile(c.Name) // 如果指定了配置文件,则解析指定的配置文件
viper.SetConfigName(c.Name)
} else {
viper.AddConfigPath("./config") // 如果没有指定配置文件,则解析默认的配置文件
viper.AddConfigPath("./config")
viper.SetConfigName("config")
}
viper.SetConfigType("yaml") // 设置配置文件格式为YAML
viper.AutomaticEnv() // 读取匹配的环境变量
viper.SetEnvPrefix("monitor") // 读取环境变量的前缀为monitor
viper.SetConfigType("yaml")
viper.AutomaticEnv()
viper.SetEnvPrefix("monitor")
replacer := strings.NewReplacer(".", "_")
viper.SetEnvKeyReplacer(replacer)
if err := viper.ReadInConfig(); err != nil { // viper解析配置文件
if err := viper.ReadInConfig(); err != nil {
return err
}
c.initLog()
Expand All @@ -52,21 +51,21 @@ func (c *Config) initLog() {
}

log.InitWithConfig(&passLagerCfg)
log.Info("初始化日志设置成功")
log.Info("Init log setting success")
}

// main.go初始化配置文件
// main.go init config
func InitConfig(cfg string) error {
c := Config{
Name: cfg,
}

// 初始化配置文件
//init config file
if err := c.Init(); err != nil {
return err
}

// 监控配置文件变化并热加载程序
// watch hot update
c.WatchConfig()

return nil
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<div class="m-doc">
<div class="m-header">
<div class="f-tit">
<h3>表单</h3>
<h3>FDGGS</h3>
</div>
<el-form ref="form" :model="form" :rules="rules" label-width="130px" size="medium">
<el-row>
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
8 changes: 4 additions & 4 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import (
func main() {

g := gin.New()
// LoggerWithFormatter middleware 会代理所有API的日志输出到stdout
// LoggerWithFormatter middleware
// By default gin.DefaultWriter = os.Stdout
g.Use(gin.LoggerWithFormatter(func(param gin.LogFormatterParams) string {
b, _ := ioutil.ReadAll(param.Request.Body)
Expand Down Expand Up @@ -45,14 +45,14 @@ func main() {
middlewares...,
)

// 初始化配置
// init config
if err := config.InitConfig(""); err != nil {
log.Error("init config error:%s", err)
panic(err)
}
log.Info("初始化配置成功")
log.Info("config init success")

// 运行服务器
// run server
host := viper.GetString("server.host")
port := viper.GetString("server.port")
if err := g.Run(host + ":" + port); err != nil {
Expand Down

0 comments on commit 24ea99c

Please sign in to comment.