mirror of
https://github.com/schollz/cowyo.git
synced 2023-08-10 21:13:00 +03:00
Generate sitemap in thread because it takes a long time
This commit is contained in:
parent
e52d0097a9
commit
c35aeca859
36
handlers.go
36
handlers.go
@ -5,6 +5,7 @@ import (
|
|||||||
"html/template"
|
"html/template"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"path"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
@ -41,6 +42,9 @@ func serve(host, port, crt_path, key_path string, TLS bool) {
|
|||||||
router.DELETE("/oldlist", handleClearOldListItems)
|
router.DELETE("/oldlist", handleClearOldListItems)
|
||||||
router.DELETE("/listitem", deleteListItem)
|
router.DELETE("/listitem", deleteListItem)
|
||||||
|
|
||||||
|
// start long-processes as threads
|
||||||
|
go thread_SiteMap()
|
||||||
|
|
||||||
if TLS {
|
if TLS {
|
||||||
http.ListenAndServeTLS(host+":"+port, crt_path, key_path, router)
|
http.ListenAndServeTLS(host+":"+port, crt_path, key_path, router)
|
||||||
} else {
|
} else {
|
||||||
@ -106,6 +110,15 @@ func handlePageRelinquish(c *gin.Context) {
|
|||||||
"destroyed": destroyed})
|
"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) {
|
func generateSiteMap() (sitemap string) {
|
||||||
files, _ := ioutil.ReadDir(pathToData)
|
files, _ := ioutil.ReadDir(pathToData)
|
||||||
lastEdited := make([]string, len(files))
|
lastEdited := make([]string, len(files))
|
||||||
@ -122,16 +135,16 @@ func generateSiteMap() (sitemap string) {
|
|||||||
names = names[:i]
|
names = names[:i]
|
||||||
lastEdited = lastEdited[:i]
|
lastEdited = lastEdited[:i]
|
||||||
sitemap = `<?xml version="1.0" encoding="UTF-8"?>
|
sitemap = `<?xml version="1.0" encoding="UTF-8"?>
|
||||||
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">`
|
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">`
|
||||||
for i := range names {
|
for i := range names {
|
||||||
sitemap += fmt.Sprintf(`
|
sitemap += fmt.Sprintf(`
|
||||||
<url>
|
<url>
|
||||||
<loc>https://cowyo.com/%s/view</loc>
|
<loc>https://cowyo.com/%s/view</loc>
|
||||||
<lastmod>%s</lastmod>
|
<lastmod>%s</lastmod>
|
||||||
<changefreq>monthly</changefreq>
|
<changefreq>monthly</changefreq>
|
||||||
<priority>0.8</priority>
|
<priority>0.8</priority>
|
||||||
</url>
|
</url>
|
||||||
`, names[i], lastEdited[i])
|
`, names[i], lastEdited[i])
|
||||||
}
|
}
|
||||||
sitemap += "</urlset>"
|
sitemap += "</urlset>"
|
||||||
return
|
return
|
||||||
@ -141,7 +154,12 @@ func handlePageRequest(c *gin.Context) {
|
|||||||
command := c.Param("command")
|
command := c.Param("command")
|
||||||
|
|
||||||
if page == "sitemap.xml" {
|
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
|
return
|
||||||
} else if page == "favicon.ico" {
|
} else if page == "favicon.ico" {
|
||||||
data, _ := Asset("/static/img/cowyo/favicon.ico")
|
data, _ := Asset("/static/img/cowyo/favicon.ico")
|
||||||
|
Loading…
Reference in New Issue
Block a user