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

Remove sitemapUpToDate global

This commit is contained in:
Daniel Heath 2018-04-28 12:19:43 +10:00
parent d095915f83
commit f4f5042245

View File

@ -24,7 +24,6 @@ import (
const minutesToUnlock = 10.0 const minutesToUnlock = 10.0
var needSitemapUpdate = true
var pathToData string var pathToData string
var log *lumber.ConsoleLogger var log *lumber.ConsoleLogger
@ -42,9 +41,11 @@ type Site struct {
Fileuploads bool Fileuploads bool
MaxUploadSize uint MaxUploadSize uint
Logger *lumber.ConsoleLogger Logger *lumber.ConsoleLogger
sitemapUpToDate bool // TODO this makes everything use a pointer
} }
func (s Site) defaultLock() string { func (s *Site) defaultLock() string {
if s.DefaultPassword == "" { if s.DefaultPassword == "" {
return "" return ""
} }
@ -97,6 +98,7 @@ func Serve(
fileuploads, fileuploads,
maxUploadSize, maxUploadSize,
logger, logger,
false,
}.Router() }.Router()
if TLS { if TLS {
@ -173,7 +175,7 @@ func (s Site) Router() *gin.Engine {
c.Redirect(302, "/"+page+"/") c.Redirect(302, "/"+page+"/")
}) })
router.GET("/:page/*command", s.handlePageRequest) router.GET("/:page/*command", s.handlePageRequest)
router.POST("/update", handlePageUpdate) router.POST("/update", s.handlePageUpdate)
router.POST("/relinquish", handlePageRelinquish) // relinquish returns the page no matter what (and destroys if nessecary) router.POST("/relinquish", handlePageRelinquish) // relinquish returns the page no matter what (and destroys if nessecary)
router.POST("/exists", handlePageExists) router.POST("/exists", handlePageExists)
router.POST("/prime", handlePrime) router.POST("/prime", handlePrime)
@ -184,7 +186,7 @@ func (s Site) Router() *gin.Engine {
router.DELETE("/listitem", deleteListItem) router.DELETE("/listitem", deleteListItem)
// start long-processes as threads // start long-processes as threads
go thread_SiteMap() go s.thread_SiteMap()
// Allow iframe/scripts in markup? // Allow iframe/scripts in markup?
allowInsecureHtml = s.AllowInsecure allowInsecureHtml = s.AllowInsecure
@ -274,11 +276,11 @@ func getSetSessionID(c *gin.Context) (sid string) {
return sid return sid
} }
func thread_SiteMap() { func (s *Site) thread_SiteMap() {
for { for {
if needSitemapUpdate { if !s.sitemapUpToDate {
log.Info("Generating sitemap...") log.Info("Generating sitemap...")
needSitemapUpdate = false s.sitemapUpToDate = true
ioutil.WriteFile(path.Join(pathToData, "sitemap.xml"), []byte(generateSiteMap()), 0644) ioutil.WriteFile(path.Join(pathToData, "sitemap.xml"), []byte(generateSiteMap()), 0644)
log.Info("..finished generating sitemap") log.Info("..finished generating sitemap")
} }
@ -316,7 +318,7 @@ func generateSiteMap() (sitemap string) {
return return
} }
func (s Site) handlePageRequest(c *gin.Context) { func (s *Site) handlePageRequest(c *gin.Context) {
page := c.Param("page") page := c.Param("page")
command := c.Param("command") command := c.Param("command")
@ -572,7 +574,7 @@ func handlePageExists(c *gin.Context) {
} }
func handlePageUpdate(c *gin.Context) { func (s *Site) handlePageUpdate(c *gin.Context) {
type QueryJSON struct { type QueryJSON struct {
Page string `json:"page"` Page string `json:"page"`
NewText string `json:"new_text"` NewText string `json:"new_text"`
@ -627,7 +629,7 @@ func handlePageUpdate(c *gin.Context) {
p.Save() p.Save()
message = "Saved" message = "Saved"
if p.IsPublished { if p.IsPublished {
needSitemapUpdate = true s.sitemapUpToDate = false
} }
success = true success = true
} }
@ -657,7 +659,7 @@ func handlePrime(c *gin.Context) {
c.JSON(http.StatusOK, gin.H{"success": true, "message": "Primed"}) c.JSON(http.StatusOK, gin.H{"success": true, "message": "Primed"})
} }
func (s Site) handleLock(c *gin.Context) { func (s *Site) handleLock(c *gin.Context) {
type QueryJSON struct { type QueryJSON struct {
Page string `json:"page"` Page string `json:"page"`
Passphrase string `json:"passphrase"` Passphrase string `json:"passphrase"`
@ -735,7 +737,7 @@ func handlePublish(c *gin.Context) {
c.JSON(http.StatusOK, gin.H{"success": true, "message": message}) c.JSON(http.StatusOK, gin.H{"success": true, "message": message})
} }
func (s Site) handleUpload(c *gin.Context) { func (s *Site) handleUpload(c *gin.Context) {
if !s.Fileuploads { if !s.Fileuploads {
c.AbortWithError(http.StatusInternalServerError, fmt.Errorf("Uploads are disabled on this server")) c.AbortWithError(http.StatusInternalServerError, fmt.Errorf("Uploads are disabled on this server"))
return return