Example #1
0
// Register initialize viewengine
func (ve *GoHtml) Register(server *HttpServer) error {
	if server.Config.ViewDir == "" {
		return nil
	}
	basePath := cleanFilePath(server.Config.ViewDir)
	if !strings.HasSuffix(basePath, "/") {
		basePath = basePath + "/"
	}
	ve.BasePath = basePath

	ve.TemplatesCache = make(map[string]*template.Template)
	ve.Funcs = make(template.FuncMap)

	for name, fn := range TemplateFuncs {
		ve.Funcs[name] = fn
	}
	ve.Funcs["partial"] = ve.renderfile // render a template file

	if conf, ok := server.Config.PluginConfig.Child("gohtml"); ok {
		ve.EnableCache = conf.ChildBoolOrDefault("cache_enable", false)
		if ve.EnableCache {
			if fw, err := fsw.NewFsWatcher(ve.BasePath); err == nil {
				ve.watcher = fw
				ve.watcher.Listen(ve.notify)
			}
		}

		Logger.Println("gohtml", "cache_enable:", ve.EnableCache, "watcher", ve.watcher)
	}

	return nil
}
Example #2
0
func newFileCache(root string) (*fileCache, error) {
	if info, err := os.Stat(root); err != nil {
		return nil, err
	} else if !info.IsDir() {
		return nil, fmt.Errorf("%s isn't a directory", root)
	}

	fc := &fileCache{
		root:   root,
		cache:  mcache.NewMCache(),
		expire: 60,
	}
	if fw, err := fsw.NewFsWatcher(root); err == nil {
		fc.watcher = fw
		fc.watcher.Listen(fc.notify)
	}
	return fc, nil
}