Skip to content

Commit

Permalink
fix:1、修复匿名访问判断错误的BUG
Browse files Browse the repository at this point in the history
2、重构配置文件自动加载逻辑
  • Loading branch information
lifei6671 committed Sep 19, 2018
1 parent 4d59df6 commit 3aa703d
Show file tree
Hide file tree
Showing 17 changed files with 301 additions and 256 deletions.
36 changes: 19 additions & 17 deletions commands/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"github.com/lifei6671/mindoc/models"
"github.com/lifei6671/mindoc/utils/filetil"
"github.com/astaxie/beego/cache/redis"
"github.com/howeyc/fsnotify"
)

// RegisterDataBase 注册数据库
Expand Down Expand Up @@ -55,6 +56,7 @@ func RegisterDataBase() {
beego.Error("注册默认数据库失败->", err)
os.Exit(1)
}

} else if strings.EqualFold(adapter, "sqlite3") {

database := beego.AppConfig.String("db_database")
Expand Down Expand Up @@ -401,37 +403,37 @@ func RegisterCache() {
//自动加载配置文件.修改了监听端口号和数据库配置无法自动生效.
func RegisterAutoLoadConfig() {
if conf.AutoLoadDelay > 0 {
ticker := time.NewTicker(time.Second * time.Duration(conf.AutoLoadDelay))

watcher, err := fsnotify.NewWatcher()

if err != nil {
beego.Error("创建配置文件监控器失败 ->",err)
}
go func() {
f,err := os.Stat(conf.ConfigurationFile)
if err != nil {
beego.Error("读取配置文件时出错 ->",err)
return
}
modTime := f.ModTime()
for {
select {
case <-ticker.C:
f,err := os.Stat(conf.ConfigurationFile)
if err != nil {
beego.Error("读取配置文件时出错 ->",err)
break
}
if modTime != f.ModTime() {
case ev := <-watcher.Event:
//如果是修改了配置文件
if ev.IsModify() {
if err := beego.LoadAppConfig("ini", conf.ConfigurationFile); err != nil {
beego.Error("An error occurred ->", err)
break
}
modTime = f.ModTime()
RegisterCache()

RegisterLogger("")
beego.Info("配置文件已加载")
beego.Info("配置文件已加载 ->", conf.ConfigurationFile)
}
case err := <-watcher.Error:
beego.Error("配置文件监控器错误 ->", err)
}
}
}()

err = watcher.Watch(conf.ConfigurationFile)

if err != nil {
beego.Error("监控配置文件失败 ->",err)
}
}
}

Expand Down
5 changes: 2 additions & 3 deletions controllers/BaseController.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,9 @@ func (c *BaseController) Prepare() {
for _, item := range options {
c.Data[item.OptionName] = item.OptionValue
c.Option[item.OptionName] = item.OptionValue

c.EnableAnonymous = strings.EqualFold(item.OptionName, "ENABLE_ANONYMOUS") && item.OptionValue == "true"
c.EnableDocumentHistory = strings.EqualFold(item.OptionName, "ENABLE_DOCUMENT_HISTORY") && item.OptionValue == "true"
}
c.EnableAnonymous = strings.EqualFold(c.Option["ENABLE_ANONYMOUS"], "true")
c.EnableDocumentHistory = strings.EqualFold(c.Option["ENABLE_DOCUMENT_HISTORY"],"true")
}
c.Data["HighlightStyle"] = beego.AppConfig.DefaultString("highlight_style","github")

Expand Down
Loading

0 comments on commit 3aa703d

Please sign in to comment.