func (v *Validation) apply(chk Validator, obj interface{}) *ValidationResult { if chk.IsSatisfied(obj) { return &ValidationResult{Ok: true} } //e.g key: validate.(*Validation).apply#84 var key string if pc, _, line, ok := runtime.Caller(2); ok { f := runtime.FuncForPC(pc) key = f.Name() + "#" + strconv.Itoa(line) } else { log.Info("Failed to get Caller information to look up Validation key") } err := &ValidationError{ Message: chk.DefaultMessage(), Key: key, } v.Errors = append(v.Errors, err) return &ValidationResult{ Ok: false, Error: err, } }
func (p *Page) routeTemplate(w http.ResponseWriter, r *http.Request) { if globalTplFi, err := os.Stat(p.Config.TemplateDirectory + p.Config.ThemeDirectory + p.Config.TemplateGlobalDirectory); err != nil { log.Error("<Page.routeTemplate> ", err) } else { if globalTplCache := p.site.GetTemplateCache("globalTpl"); globalTplCache.ModTime > 0 { if globalTplFi.ModTime().Unix() > globalTplCache.ModTime { p.setGlobalTpl(globalTplFi.ModTime().Unix(), true) } } } if tplFi, err := os.Stat(p.Config.TemplateDirectory + p.Config.ThemeDirectory + p.Template); err != nil { p.site.base.rmutex.RLock() tmplCache := p.site.GetTemplateCache(p.Template) if tmplCache.ModTime > 0 { p.site.base.mutex.Lock() p.DelTemplateCache(p.Template) p.site.base.mutex.Unlock() log.Info("<Page.routeTemplate> ", "Delete Template Cache:", p.Template) } p.site.base.rmutex.RUnlock() } else { p.site.base.rmutex.RLock() tmplCache := p.site.GetTemplateCache(p.Template) if tplFi.ModTime().Unix() > tmplCache.ModTime { p.site.base.mutex.Lock() p.SetTemplateCache(p.Template, p.Config.TemplateDirectory+p.Config.ThemeDirectory+p.Template) p.site.base.mutex.Unlock() tmplCache = p.site.GetTemplateCache(p.Template) } globalTemplate, _ := p.site.globalTemplate.Clone() p.site.base.rmutex.RUnlock() pageTemplate, err := globalTemplate.New(filepath.Base(p.Template)).Parse(tmplCache.Content) if err != nil { log.Error("<Page.routeTemplate> ", err) w.Write([]byte(fmt.Sprint(err))) } else { /*向模板传递值*/ templateVar := map[string]interface{}{ "G": p.GET, "P": p.POST, "S": p.SESSION, "O_S": p.ONCE_SESSION, "C": p.COOKIE, "CS": p.COOKIE_SESSION, "D": p.Document, "L": p.LANG, "T": p.Template, "Custom": p.Config.Custom, "Config": p.Config, } p.site.base.rmutex.RLock() templateVar["Siteroot"] = p.site.Root templateVar["Version"] = p.site.Version p.site.base.rmutex.RUnlock() if !p.Document.GenerateHtml { err := pageTemplate.Execute(w, templateVar) if err != nil { log.Error("<Page.routeTemplate> ", err) w.Write([]byte(fmt.Sprint(err))) } } else { htmlFile := "" assetsHtmlDir := p.Config.AssetsDirectory + p.Config.HtmlDirectory if strings.HasPrefix(p.Template, p.Config.IndexDirectory) { htmlFile = assetsHtmlDir + strings.Replace(p.Template, p.Config.IndexDirectory, "", 1) } else { htmlFile = assetsHtmlDir + p.Template } if r.URL.RawQuery != "" { htmlFile += "?" + r.URL.RawQuery } htmlDir := filepath.Dir(htmlFile) if htmlDirFi, err := os.Stat(htmlDir); err != nil || !htmlDirFi.IsDir() { os.MkdirAll(htmlDir, 0777) log.Info("<Page.routeTemplate> ", "MkdirAll:", htmlDir) } htmlFi, htmlErr := os.Stat(htmlFile) if p.checkHtmlDoWrite(tplFi, htmlFi, htmlErr) { if file, err := os.OpenFile(htmlFile, os.O_CREATE|os.O_WRONLY, 0777); err != nil { log.Error("<Page.routeTemplate> ", err) } else { if p.Config.AutoJumpToHtml || p.Config.ChangeSiteRoot { p.site.base.rmutex.RLock() templateVar["Siteroot"] = p.site.Root + p.Config.HtmlDirectory p.site.base.rmutex.RUnlock() } pageTemplate.Execute(file, templateVar) } } if p.Config.AutoJumpToHtml { p.site.base.rmutex.RLock() siteRoot := p.site.Root p.site.base.rmutex.RUnlock() http.Redirect(w, r, siteRoot+htmlFile[len(p.Config.AssetsDirectory):], http.StatusFound) } else if p.Config.AutoLoadStaticHtml && htmlFi != nil && htmlErr == nil { htmlContent, err := ioutil.ReadFile(htmlFile) if err == nil { w.Write(htmlContent) } else { log.Error("<Page.routeTemplate> ", err) } } else { err := pageTemplate.Execute(w, templateVar) if err != nil { log.Error("<Page.routeTemplate> ", err) } } } } } }
func (p *Page) reset(update bool) { p.setLog(p.Config.SupportLog, p.Config.LogWriteTo, p.Config.LogLevel) p.setUrlManage(p.Config.SupportUrlManage, p.Config.SupportUrlManageWithCache, p.Config.UrlManageRule) if update { if p.site.supportSession != p.Config.SupportSession { p.site.supportSession = p.Config.SupportSession } if p.site.supportCookieSession != p.Config.SupportCookieSession { p.site.supportCookieSession = p.Config.SupportCookieSession } if p.site.supportI18n != p.Config.SupportI18n { p.site.supportI18n = p.Config.SupportI18n } if p.Document.Theme != p.Config.Theme { p.Document.Theme = p.Config.Theme } if p.Document.Static != p.Config.SiteRoot+p.Config.StaticDirectory { p.Document.Static = p.Config.SiteRoot + p.Config.StaticDirectory } if p.site.Root == p.Config.SiteRoot { return } else { log.Info("<Page.reset> ", "p.Config.SiteRoot be changed:", p.Config.SiteRoot) p.SetDefaultController(p.GetController(p.Config.IndexDirectory)) p.UpdateController(p.site.Root, p.Config.SiteRoot, p.DefaultController) p.site.Root = p.Config.SiteRoot } } else { p.site.supportSession = p.Config.SupportSession p.site.supportCookieSession = p.Config.SupportCookieSession p.site.supportI18n = p.Config.SupportI18n p.Document.Theme = p.Config.Theme p.site.Root = p.Config.SiteRoot p.Document.Static = p.site.Root + p.Config.StaticDirectory p.SetDefaultController(p.GetController(p.Config.IndexDirectory)) p.RegisterController(p.site.Root, p.DefaultController) p.site.globalTemplate = template.New("globalTpl").Funcs(p.site.templateFunc) } if globalCssFi, err := os.Stat(p.Config.StaticCssDirectory + "/global/"); err == nil && globalCssFi.IsDir() { DcssPath := p.Config.StaticCssDirectory + "global/" p.Document.Css["global"] = p.site.Root + DcssPath[len(p.Config.AssetsDirectory):] log.Debug("<Page.reset> ", `p.Document.Css["global"]:`, p.Document.Css["global"]) if _, err := os.Stat(DcssPath + "global.css"); err == nil { p.Document.GlobalCssFile = p.Document.Css["global"] + "global.css" } } if globalJsFi, err := os.Stat(p.Config.StaticJsDirectory + "/global/"); err == nil && globalJsFi.IsDir() { DjsPath := p.Config.StaticJsDirectory + "global/" p.Document.Js["global"] = p.site.Root + DjsPath[len(p.Config.AssetsDirectory):] log.Debug("<Page.reset> ", `p.Document.Js["global"]:`, p.Document.GlobalCssFile) if _, err := os.Stat(DjsPath + "global.js"); err == nil { p.Document.GlobalJsFile = p.Document.Js["global"] + "global.js" } } if globalImgFi, err := os.Stat(p.Config.StaticImgDirectory + "/global/"); err == nil && globalImgFi.IsDir() { DimgPath := p.Config.StaticImgDirectory + "global/" p.Document.Img["global"] = p.site.Root + DimgPath[len(p.Config.AssetsDirectory):] log.Debug("<Page.reset> ", `p.Document.Img["global"]:`, p.Document.Img["global"]) } if globalTplFi, err := os.Stat(p.Config.TemplateDirectory + p.Config.ThemeDirectory + p.Config.TemplateGlobalDirectory); err != nil { log.Error("<Page.reset> ", err) } else { p.setGlobalTpl(globalTplFi.ModTime().Unix(), false) } }