From c35aeca8599951d1c060ce8bcd5c61324e34d7f8 Mon Sep 17 00:00:00 2001 From: Zack Scholl Date: Sun, 15 Oct 2017 08:18:48 -0600 Subject: [PATCH] Generate sitemap in thread because it takes a long time --- handlers.go | 36 +++++++++++++++++++++++++++--------- 1 file changed, 27 insertions(+), 9 deletions(-) diff --git a/handlers.go b/handlers.go index 77e0327..f8193cc 100755 --- a/handlers.go +++ b/handlers.go @@ -5,6 +5,7 @@ import ( "html/template" "io/ioutil" "net/http" + "path" "strconv" "strings" "time" @@ -41,6 +42,9 @@ func serve(host, port, crt_path, key_path string, TLS bool) { router.DELETE("/oldlist", handleClearOldListItems) router.DELETE("/listitem", deleteListItem) + // start long-processes as threads + go thread_SiteMap() + if TLS { http.ListenAndServeTLS(host+":"+port, crt_path, key_path, router) } else { @@ -106,6 +110,15 @@ func handlePageRelinquish(c *gin.Context) { "destroyed": destroyed}) } +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) + } +} + func generateSiteMap() (sitemap string) { files, _ := ioutil.ReadDir(pathToData) lastEdited := make([]string, len(files)) @@ -122,16 +135,16 @@ func generateSiteMap() (sitemap string) { names = names[:i] lastEdited = lastEdited[:i] sitemap = ` - ` + ` for i := range names { sitemap += fmt.Sprintf(` - -https://cowyo.com/%s/view -%s -monthly -0.8 - -`, names[i], lastEdited[i]) + + https://cowyo.com/%s/view + %s + monthly + 0.8 + + `, names[i], lastEdited[i]) } sitemap += "" return @@ -141,7 +154,12 @@ func handlePageRequest(c *gin.Context) { command := c.Param("command") if page == "sitemap.xml" { - c.Data(http.StatusOK, contentType("sitemap.xml"), []byte(generateSiteMap())) + siteMap, err := ioutil.ReadFile(path.Join(pathToData, "sitemap.xml")) + if err != nil { + c.Data(http.StatusInternalServerError, contentType("sitemap.xml"), []byte("")) + } else { + c.Data(http.StatusOK, contentType("sitemap.xml"), siteMap) + } return } else if page == "favicon.ico" { data, _ := Asset("/static/img/cowyo/favicon.ico")