func (p *Page) checkHtmlDoWrite(tplFi, htmlFi os.FileInfo, htmlErr error) bool { var doWrite bool log.Debug("<Page.checkHtmlDoWrite> ", "p.Config.AutoGenerateHtmlCycleTime:", p.Config.AutoGenerateHtmlCycleTime) if p.Config.AutoGenerateHtmlCycleTime <= 0 { doWrite = true } else { if htmlErr != nil { doWrite = true } else { log.Debug("<Page.checkHtmlDoWrite> ", "tplFi.ModTime().Unix():", tplFi.ModTime().Unix()) log.Debug("<Page.checkHtmlDoWrite> ", "htmlFi.ModTime().Unix():", htmlFi.ModTime().Unix()) switch { case tplFi.ModTime().Unix() >= htmlFi.ModTime().Unix(): doWrite = true case time.Now().Unix()-htmlFi.ModTime().Unix() >= p.Config.AutoGenerateHtmlCycleTime: doWrite = true default: globalTplCache := p.site.GetTemplateCache("globalTpl") log.Debug("<Page.checkHtmlDoWrite> ", `globalTplCache.ModTime:`, globalTplCache.ModTime) if globalTplCache.ModTime > 0 && globalTplCache.ModTime >= htmlFi.ModTime().Unix() { doWrite = true } } } } return doWrite }
func (p *Page) filterDoMethod(tpc reflect.Type, vpc reflect.Value, action string, rvr reflect.Value, rvw reflect.Value) { log.Debug("<Page.filterDoMethod> ", "action:", action) if aarv := p.callMethod(tpc, vpc, action, rvr, rvw); len(aarv) == 1 { if ma, tok := aarv[0].Interface().([]map[string]string); tok { log.Debug("<Page.filterDoMethod> ", "FilterMap:", ma) for _, filter := range ma { doFilter := false doMethod := false doParam := false method, mok := filter["_FILTER"] if mok { filterType, found := filter[p.CurrentAction] if !found && filter["_ALL"] == "allow" { doFilter = true } else if found && filterType == "allow" { doFilter = true } } if doFilter { if rm, ok := filter["_METHOD"]; !ok { doMethod = true } else { reqMethods := strings.Split(rm, ",") for _, v := range reqMethods { r := rvr.Interface().(*http.Request) if r.Method == strings.ToUpper(v) { doMethod = true break } } } if rmp, ok := filter["_PARAM"]; !ok { doParam = true } else { reqParams := strings.Split(rmp, ",") for _, v := range reqParams { _, vgok := p.GET[v] _, vpok := p.POST[v] if vgok || vpok { doParam = true break } } } if doMethod && doParam { p.callMethod(tpc, vpc, "Filter_"+method, rvr, rvw) } } } } } }
func (p *Page) initModule(w http.ResponseWriter, r *http.Request) { if p.site.supportSession { switch p.Config.SessionType { case "file": p.SESSION = p.FileSession.Get(w, r) case "memory": p.SESSION = p.MemorySession.Get(w, r) default: p.SESSION = p.MemorySession.Get(w, r) } var ok bool p.ONCE_SESSION, ok = p.SESSION["__ONCE"] if ok { log.Debug("<Page.Init> ", `p.SESSION["__ONCE"] to set:`, p.ONCE_SESSION) delete(p.SESSION, "__ONCE") log.Debug("<Page.Init> ", `p.SESSION["__ONCE"] to delete:`, p.SESSION["__ONCE"]) } } if p.site.supportCookieSession { p.COOKIE_SESSION = p.CookieSession.Get(r) } if p.site.supportI18n { p.LANG = func() map[string]string { log.Debug("<Page.Init> ", `p.TARGET_LANG: `, p.TARGET_LANG) if p.TARGET_LANG == "" { p.TARGET_LANG = strings.TrimSpace(r.Header.Get("Accept-Language")) if i := strings.Index(p.TARGET_LANG, ","); i != -1 { p.TARGET_LANG = p.TARGET_LANG[:i] } } return p.I18n.Lang(p.TARGET_LANG) }() } if p.site.base.header != nil || len(p.site.base.header) > 0 { log.Debug("<Page.Init> ", "p.site.base.header:", p.site.base.header) for t, s := range p.site.base.header { for _, v := range s { w.Header().Add(t, v) } } } }
func (p *Page) handleRoute(i interface{}) { http.HandleFunc(p.site.Root, func(w http.ResponseWriter, r *http.Request) { p.site.base.mutex.Lock() if p.Config.Reload() { p.reset(true) } if p.UrlManage.Manage() { newUrl := p.UrlManage.ReWrite(w, r) if newUrl == "redirect" { p.site.base.mutex.Unlock() return } else { r.URL, _ = url.Parse(newUrl) } } if p.site.supportI18n { if err := p.I18n.Setup(p.Config.DefaultLocalePath, p.Config.DefaultLanguage); err != nil { log.Panic("<Page.handleRoute> ", "I18n(Setup):", err) } } p.setCurrentInfo(r.URL.Path) p.Template = p.CurrentController + p.currentFileName p.site.base.mutex.Unlock() log.Debug("<Page.handleRoute> ", "p.Template:", p.Template) if !p.loadGeneratedHtml(w, r) { p.routeController(i, w, r) } }) }
func readFiles(files ...string) []byte { lfs := len(files) chContent := make(chan []byte, lfs) for _, file := range files { go func(chContent chan []byte, configPath string) { chContent <- format(configPath) }(chContent, file) } bytess := make([][]byte, 0, lfs) for i := 1; i <= lfs; i++ { content := <-chContent if len(content) != 0 { bytess = append(bytess, content) } } buf := bytes.NewBufferString(`{`) buf.Write(bytes.Join(bytess, []byte(`,`))) buf.WriteString(`}`) var contentBuf bytes.Buffer err := json.Compact(&contentBuf, buf.Bytes()) if err != nil { log.Debug("<readFiles> jsonData: ", buf.String()) log.Fatal("<readFiles> error: ", err) } return contentBuf.Bytes() }
func (p *Page) loadGeneratedHtml(w http.ResponseWriter, r *http.Request) (b bool) { log.Debug("<Page.loadGeneratedHtml> ", "p.Config.SupportTemplate:", p.Config.SupportTemplate) log.Debug("<Page.loadGeneratedHtml> ", "p.Config.LoadStaticHtmlWithLogic:", p.Config.LoadStaticHtmlWithLogic) log.Debug("<Page.loadGeneratedHtml> ", "p.Config.AutoGenerateHtml:", p.Config.AutoGenerateHtml) if p.Config.SupportTemplate && !p.Config.LoadStaticHtmlWithLogic && p.Config.AutoGenerateHtml { tplFi, tplErr := os.Stat(p.Config.TemplateDirectory + p.Config.ThemeDirectory + p.Template) if tplErr != nil { log.Error("<Page.loadGeneratedHtml> ", tplErr) } else { p.site.base.rmutex.RLock() tmplCache := p.site.GetTemplateCache(p.Template) p.site.base.rmutex.RUnlock() if tplFi.ModTime().Unix() > tmplCache.ModTime { return false } } 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 } if htmlFi, htmlErr := os.Stat(htmlFile); htmlErr == nil { if p.checkHtmlDoWrite(tplFi, htmlFi, htmlErr) { } htmlContent, err := ioutil.ReadFile(htmlFile) if err == nil { w.Header().Add("Content-Length", strconv.FormatInt(htmlFi.Size(), 10)) w.Header().Add("Last-Modified", htmlFi.ModTime().UTC().Format("Mon, 02 Jan 2006 15:04:05 GMT")) w.Write(htmlContent) return true } } } return }
func (p *Page) handleRootStatic(files string) { aFile := strings.Split(files, ",") for _, file := range aFile { http.HandleFunc(p.site.Root+file, func(w http.ResponseWriter, r *http.Request) { staticPath := p.Config.AssetsDirectory + file log.Debug("<Page.handleRootStatic> ", "staticPath:", staticPath) http.ServeFile(w, r, staticPath) }) } }
func (p *Page) setCurrentInfo(path string) { urlPath, fileName := filepath.Split(path) if urlPath == p.site.Root { urlPath = p.site.Root + p.Config.IndexDirectory } if fileName == "" { fileName = p.Config.IndexPage } p.currentPath = urlPath log.Debug("<Page.setCurrentInfo> ", "p.currentPath:", p.currentPath) p.currentFileName = fileName log.Debug("<Page.setCurrentInfo> ", "p.currentFileName:", p.currentFileName) p.CurrentController = urlPath[len(p.site.Root):] log.Debug("<Page.setCurrentInfo> ", "p.CurrentController:", p.CurrentController) p.CurrentAction = strings.Replace(strings.Title(strings.Replace(p.currentFileName[:len(p.currentFileName)-len(filepath.Ext(p.currentFileName))], "_", " ", -1)), " ", "", -1) log.Debug("<Page.setCurrentInfo> ", "p.CurrentAction:", p.CurrentAction) }
func (p *Page) setStaticDocument() { fileNameNoExt := p.currentFileName[:len(p.currentFileName)-len(filepath.Ext(p.currentFileName))] cssFi, cssErr := os.Stat(p.Config.StaticCssDirectory + p.currentPath) jsFi, jsErr := os.Stat(p.Config.StaticJsDirectory + p.currentPath) imgFi, imgErr := os.Stat(p.Config.StaticImgDirectory + p.currentPath) if cssErr == nil && cssFi.IsDir() { cssPath := strings.Trim(p.currentPath, "/") DcssPath := p.Config.StaticCssDirectory + cssPath + "/" p.Document.Css[cssPath] = p.site.Root + DcssPath[len(p.Config.AssetsDirectory):] log.Debug("<Page.setStaticDocument> ", "p.Document.Css["+cssPath+"]:", p.Document.Css[cssPath]) _, errgcss := os.Stat(DcssPath + "global.css") _, errcss := os.Stat(DcssPath + fileNameNoExt + ".css") p.Document.CssPath = p.Document.Css[cssPath] if errgcss == nil { p.Document.GlobalIndexCssFile = p.Document.Css[cssPath] + "global.css" log.Debug("<Page.setStaticDocument> ", "p.Document.GlobalIndexCssFile:", p.Document.GlobalIndexCssFile) } if errcss == nil { p.Document.IndexCssFile = p.Document.Css[cssPath] + fileNameNoExt + ".css" log.Debug("<Page.setStaticDocument> ", "p.Document.IndexCssFile:", p.Document.IndexCssFile) } } if jsErr == nil && jsFi.IsDir() { jsPath := strings.Trim(p.currentPath, "/") DjsPath := p.Config.StaticJsDirectory + jsPath + "/" p.Document.Js[jsPath] = p.site.Root + DjsPath[len(p.Config.AssetsDirectory):] log.Debug("<Page.setStaticDocument> ", "p.Document.Js["+jsPath+"]:", p.Document.Js[jsPath]) _, errgjs := os.Stat(DjsPath + "global.js") _, errjs := os.Stat(DjsPath + fileNameNoExt + ".js") p.Document.JsPath = p.Document.Js[jsPath] if errgjs == nil { p.Document.GlobalIndexJsFile = p.Document.Js[jsPath] + "global.js" log.Debug("<Page.setStaticDocument> ", "p.Document.GlobalIndexJsFile:", p.Document.GlobalIndexJsFile) } if errjs == nil { p.Document.IndexJsFile = p.Document.Js[jsPath] + fileNameNoExt + ".js" log.Debug("<Page.setStaticDocument> ", "p.Document.IndexJsFile:", p.Document.IndexJsFile) } } if imgErr == nil && imgFi.IsDir() { imgPath := strings.Trim(p.currentPath, "/") DimgPath := p.Config.StaticImgDirectory + imgPath + "/" p.Document.Img[imgPath] = p.site.Root + DimgPath[len(p.Config.AssetsDirectory):] p.Document.ImgPath = p.Document.Img[imgPath] log.Debug("<Page.setStaticDocument> ", "p.Document.Img["+imgPath+"]:", p.Document.Img[imgPath]) } }
func (c *Config) Load(i interface{}) *Config { mu.RLock() data := c.data mu.RUnlock() err := json.Unmarshal(data, i) if err != nil { log.Debug("<Config.Load> jsonData: ", c.String()) log.Fatal("<Config.Load> error: ", err) } return c }
func (i *I18nManager) Lang(lang string) map[string]string { lang = strings.ToLower(lang) err := i.Load(lang) if err != nil { lang = i.defaultLanguage log.Debug("<I18nManager.Lang> ", `Load error: `, err) } i.rmutex.RLock() msgs := i.languages[lang] i.rmutex.RUnlock() return msgs }
func Data(data string) *Config { data = `{` + data + `}` var buf bytes.Buffer err := json.Compact(&buf, []byte(data)) if err != nil { log.Debug("<Data> jsonData: ", data) log.Fatal("<Data> error: ", err) } return &Config{ dataType: "data", data: buf.Bytes(), } }
func (p *Page) handleStatic() { http.HandleFunc(p.Document.Static, func(w http.ResponseWriter, r *http.Request) { if p.UrlManage.Manage() { newUrl := p.UrlManage.ReWrite(w, r) if newUrl == "redirect" { p.site.base.mutex.Unlock() return } else { r.URL, _ = url.Parse(newUrl) } } staticPath := p.Config.AssetsDirectory + p.Config.StaticDirectory + r.URL.Path[len(p.Document.Static):] log.Debug("<Page.handleStatic> ", "staticPath:", staticPath) http.ServeFile(w, r, staticPath) }) }
func (p *Page) callMethod(tpc reflect.Type, vpc reflect.Value, action string, rvr reflect.Value, rvw reflect.Value) []reflect.Value { arv := []reflect.Value{} if rm, ok := tpc.MethodByName(action); ok { mt := rm.Type log.Debug("<Page.callMethod> ", mt.String()+".NumIn():", mt.NumIn()) switch mt.NumIn() { case 2: if mt.In(1) == rvr.Type() { arv = vpc.MethodByName(action).Call([]reflect.Value{rvr}) } else { arv = vpc.MethodByName(action).Call([]reflect.Value{rvw}) } case 3: arv = vpc.MethodByName(action).Call([]reflect.Value{rvw, rvr}) default: arv = vpc.MethodByName(action).Call([]reflect.Value{}) } } return arv }
func (p *Page) handleStaticHtml() { StaticHtmlDir := p.Config.SiteRoot + p.Config.HtmlDirectory http.HandleFunc(StaticHtmlDir, func(w http.ResponseWriter, r *http.Request) { if p.UrlManage.Manage() { newUrl := p.UrlManage.ReWrite(w, r) if newUrl == "redirect" { p.site.base.mutex.Unlock() return } else { r.URL, _ = url.Parse(newUrl) } } staticPath := p.Config.AssetsDirectory + p.Config.HtmlDirectory + r.URL.Path[len(StaticHtmlDir):] if r.URL.RawQuery != "" { staticPath += "?" + r.URL.RawQuery } log.Debug("<Page.handleStatic> ", "staticPath:", staticPath) http.ServeFile(w, r, staticPath) }) }
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) } }