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
20
handlers.go
20
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))
|
||||||
@ -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