diff --git a/handlers.go b/handlers.go index acca91f..2027e67 100755 --- a/handlers.go +++ b/handlers.go @@ -30,6 +30,7 @@ var debounceTime int var diaryMode bool var allowFileUploads bool var maxUploadMB uint +var needSitemapUpdate = true func serve( host, @@ -234,10 +235,13 @@ func getSetSessionID(c *gin.Context) (sid string) { func thread_SiteMap() { for { - log.Info("Generating sitemap...") - ioutil.WriteFile(path.Join(pathToData, "sitemap.xml"), []byte(generateSiteMap()), 0644) - log.Info("..finished generating sitemap") - time.Sleep(24 * time.Hour) + if needSitemapUpdate { + log.Info("Generating sitemap...") + needSitemapUpdate = false + ioutil.WriteFile(path.Join(pathToData, "sitemap.xml"), []byte(generateSiteMap()), 0644) + log.Info("..finished generating sitemap") + } + time.Sleep(time.Second) } } @@ -256,12 +260,11 @@ func generateSiteMap() (sitemap string) { } names = names[:i] lastEdited = lastEdited[:i] - sitemap = ` - ` + sitemap = "" for i := range names { sitemap += fmt.Sprintf(` - https://cowyo.com/%s/read + {{ .Request.Host }}/%s/read %s monthly 0.8 @@ -281,7 +284,8 @@ func handlePageRequest(c *gin.Context) { if err != nil { c.Data(http.StatusInternalServerError, contentType("sitemap.xml"), []byte("")) } else { - c.Data(http.StatusOK, contentType("sitemap.xml"), siteMap) + fmt.Fprintln(c.Writer, ``) + template.Must(template.New("sitemap").Parse(string(siteMap))).Execute(c.Writer, c) } return } else if page == "favicon.ico" { @@ -560,6 +564,9 @@ func handlePageUpdate(c *gin.Context) { } p.Save() message = "Saved" + if p.IsPublished { + needSitemapUpdate = true + } success = true } c.JSON(http.StatusOK, gin.H{"success": success, "message": message, "unix_time": time.Now().Unix()})