|
1 | 1 | package dotweb |
2 | 2 |
|
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "github.com/devfeel/dotweb/config" |
| 6 | + "os" |
| 7 | + "path/filepath" |
| 8 | + "time" |
| 9 | +) |
| 10 | + |
3 | 11 | // Plugin a interface for app's global plugin |
4 | 12 | type Plugin interface { |
5 | 13 | Name() string |
6 | 14 | Run() error |
7 | 15 | IsValidate() bool |
8 | 16 | } |
| 17 | + |
| 18 | +// NewDefaultNotifyPlugin return new NotifyPlugin with default config |
| 19 | +func NewDefaultNotifyPlugin(app *DotWeb) *NotifyPlugin { |
| 20 | + p := new(NotifyPlugin) |
| 21 | + p.app = app |
| 22 | + p.LoopTime = notifyPlugin_LoopTime |
| 23 | + p.Root = app.Config.ConfigFilePath |
| 24 | + p.suffix = make(map[string]bool) |
| 25 | + p.ModTimes = make(map[string]time.Time) |
| 26 | + return p |
| 27 | +} |
| 28 | + |
| 29 | +// NewNotifyPlugin return new NotifyPlugin with fileRoot & loopTime & suffix |
| 30 | +// if suffix is nil or suffix[0] == "*", will visit all files in fileRoot |
| 31 | +/*func NewNotifyPlugin(app *DotWeb, fileRoot string, loopTime int, suffix []string) *NotifyPlugin{ |
| 32 | + p := new(NotifyPlugin) |
| 33 | + p.app = app |
| 34 | + p.LoopTime = loopTime |
| 35 | + p.Root = fileRoot |
| 36 | + Suffix := make(map[string]bool) |
| 37 | + if len(suffix) > 0 && suffix[0] != "*" { |
| 38 | + for _, v := range suffix { |
| 39 | + Suffix[v] = true |
| 40 | + } |
| 41 | + } |
| 42 | + p.suffix = Suffix |
| 43 | + p.ModTimes = make(map[string]time.Time) |
| 44 | + return p |
| 45 | +}*/ |
| 46 | + |
| 47 | +const notifyPlugin_LoopTime = 500 //ms |
| 48 | + |
| 49 | +type NotifyPlugin struct { |
| 50 | + app *DotWeb |
| 51 | + Root string |
| 52 | + suffix map[string]bool |
| 53 | + LoopTime int |
| 54 | + ModTimes map[string]time.Time |
| 55 | +} |
| 56 | + |
| 57 | +func (p *NotifyPlugin) Name() string { |
| 58 | + return "NotifyPlugin" |
| 59 | +} |
| 60 | + |
| 61 | +func (p *NotifyPlugin) IsValidate() bool { |
| 62 | + return true |
| 63 | +} |
| 64 | + |
| 65 | +func (p *NotifyPlugin) Run() error { |
| 66 | + return p.start() |
| 67 | +} |
| 68 | + |
| 69 | +func (p *NotifyPlugin) visit(path string, fileinfo os.FileInfo, err error) error { |
| 70 | + if err != nil { |
| 71 | + return fmt.Errorf("访问文件失败%s", err) |
| 72 | + } |
| 73 | + ext := filepath.Ext(path) |
| 74 | + if !fileinfo.IsDir() && (p.suffix[ext] || len(p.suffix) == 0) { |
| 75 | + modTime := fileinfo.ModTime() |
| 76 | + if oldModTime, ok := p.ModTimes[path]; !ok { |
| 77 | + p.ModTimes[path] = modTime |
| 78 | + } else { |
| 79 | + if oldModTime.Before(modTime) { |
| 80 | + p.app.Logger().Info("NotifyPlugin Reload "+path, LogTarget_HttpServer) |
| 81 | + appConfig, err := config.InitConfig(p.app.Config.ConfigFilePath, p.app.Config.ConfigType) |
| 82 | + if err != nil { |
| 83 | + p.app.Logger().Error("NotifyPlugin Reload "+path+" error => "+fmt.Sprint(err), LogTarget_HttpServer) |
| 84 | + } |
| 85 | + p.app.ReSetConfig(appConfig) |
| 86 | + p.ModTimes[path] = modTime |
| 87 | + } |
| 88 | + } |
| 89 | + } |
| 90 | + return nil |
| 91 | +} |
| 92 | + |
| 93 | +func (p *NotifyPlugin) start() error { |
| 94 | + for { |
| 95 | + filepath.Walk(p.Root, p.visit) |
| 96 | + time.Sleep(time.Duration(p.LoopTime) * time.Millisecond) |
| 97 | + } |
| 98 | +} |
0 commit comments