1
0
mirror of https://github.com/schollz/cowyo.git synced 2023-08-10 21:13:00 +03:00

Sitemap includes the request host/port, fixes #125

This commit is contained in:
Daniel Heath 2018-03-31 14:33:10 +11:00
parent 446ba00fe9
commit d9e622fa9d

View File

@ -30,6 +30,7 @@ var debounceTime int
var diaryMode bool var diaryMode bool
var allowFileUploads bool var allowFileUploads bool
var maxUploadMB uint var maxUploadMB uint
var needSitemapUpdate = true
func serve( func serve(
host, host,
@ -234,10 +235,13 @@ func getSetSessionID(c *gin.Context) (sid string) {
func thread_SiteMap() { func thread_SiteMap() {
for { for {
log.Info("Generating sitemap...") if needSitemapUpdate {
ioutil.WriteFile(path.Join(pathToData, "sitemap.xml"), []byte(generateSiteMap()), 0644) log.Info("Generating sitemap...")
log.Info("..finished generating sitemap") needSitemapUpdate = false
time.Sleep(24 * time.Hour) 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] names = names[:i]
lastEdited = lastEdited[:i] lastEdited = lastEdited[:i]
sitemap = `<?xml version="1.0" encoding="UTF-8"?> sitemap = ""
<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/read</loc> <loc>{{ .Request.Host }}/%s/read</loc>
<lastmod>%s</lastmod> <lastmod>%s</lastmod>
<changefreq>monthly</changefreq> <changefreq>monthly</changefreq>
<priority>0.8</priority> <priority>0.8</priority>
@ -281,7 +284,8 @@ func handlePageRequest(c *gin.Context) {
if err != nil { if err != nil {
c.Data(http.StatusInternalServerError, contentType("sitemap.xml"), []byte("")) c.Data(http.StatusInternalServerError, contentType("sitemap.xml"), []byte(""))
} else { } else {
c.Data(http.StatusOK, contentType("sitemap.xml"), siteMap) fmt.Fprintln(c.Writer, `<?xml version="1.0" encoding="UTF-8"?><urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">`)
template.Must(template.New("sitemap").Parse(string(siteMap))).Execute(c.Writer, c)
} }
return return
} else if page == "favicon.ico" { } else if page == "favicon.ico" {
@ -560,6 +564,9 @@ func handlePageUpdate(c *gin.Context) {
} }
p.Save() p.Save()
message = "Saved" message = "Saved"
if p.IsPublished {
needSitemapUpdate = true
}
success = true success = true
} }
c.JSON(http.StatusOK, gin.H{"success": success, "message": message, "unix_time": time.Now().Unix()}) c.JSON(http.StatusOK, gin.H{"success": success, "message": message, "unix_time": time.Now().Unix()})