From 107a0dc32b2903fd13763cd38eff0152979afbe1 Mon Sep 17 00:00:00 2001 From: Zack Scholl Date: Sun, 15 Oct 2017 07:49:40 -0600 Subject: [PATCH] Add publishing, for sitemap.xml --- handlers.go | 61 ++++++++++++++++++++++++++++++++++++++++++++ page.go | 5 ++++ templates/index.tmpl | 60 +++++++++++++++++++++++++++++++++++++++++++ utils.go | 2 ++ 4 files changed, 128 insertions(+) diff --git a/handlers.go b/handlers.go index 46933a0..116a264 100755 --- a/handlers.go +++ b/handlers.go @@ -1,7 +1,9 @@ package main import ( + "fmt" "html/template" + "io/ioutil" "net/http" "strconv" "strings" @@ -34,6 +36,7 @@ func serve(host, port, crt_path, key_path string, TLS bool) { router.POST("/exists", handlePageExists) router.POST("/prime", handlePrime) router.POST("/lock", handleLock) + router.POST("/publish", handlePublish) router.POST("/encrypt", handleEncrypt) router.DELETE("/oldlist", handleClearOldListItems) router.DELETE("/listitem", deleteListItem) @@ -103,8 +106,44 @@ func handlePageRelinquish(c *gin.Context) { "destroyed": destroyed}) } +func generateSiteMap() (sitemap string) { + files, _ := ioutil.ReadDir(pathToData) + lastEdited := make([]string, len(files)) + names := make([]string, len(files)) + i := 0 + for _, f := range files { + names[i] = DecodeFileName(f.Name()) + p := Open(names[i]) + if p.IsPublished { + lastEdited[i] = time.Unix(p.Text.LastEditTime()/1000000000, 0).Format("2006-01-02") + i++ + } + } + names = names[:i] + lastEdited = lastEdited[:i] + fmt.Println(names) + + sitemap = ` + ` + for i := range names { + sitemap += fmt.Sprintf(` + +https://cowyo.com/%s/view +%s +monthly +0.8 + +`, names[i], lastEdited[i]) + } + sitemap += "" + return +} func handlePageRequest(c *gin.Context) { page := c.Param("page") + if page == "sitemap.xml" { + c.Data(http.StatusOK, contentType("sitemap.xml"), []byte(generateSiteMap())) + return + } command := c.Param("command") if len(command) < 2 { c.Redirect(302, "/"+page+"/edit") @@ -223,6 +262,7 @@ func handlePageRequest(c *gin.Context) { "Route": "/" + page + command, "HasDotInName": strings.Contains(page, "."), "RecentlyEdited": getRecentlyEdited(page, c), + "IsPublished": p.IsPublished, }) } @@ -382,6 +422,27 @@ func handleLock(c *gin.Context) { c.JSON(http.StatusOK, gin.H{"success": true, "message": message}) } +func handlePublish(c *gin.Context) { + type QueryJSON struct { + Page string `json:"page"` + Publish bool `json:"publish"` + } + + var json QueryJSON + if c.BindJSON(&json) != nil { + c.String(http.StatusBadRequest, "Problem binding keys") + return + } + p := Open(json.Page) + p.IsPublished = json.Publish + p.Save() + message := "Published" + if !p.IsPublished { + message = "Unpublished" + } + c.JSON(http.StatusOK, gin.H{"success": true, "message": message}) +} + func handleEncrypt(c *gin.Context) { type QueryJSON struct { Page string `json:"page"` diff --git a/page.go b/page.go index 8ff9881..89fa832 100755 --- a/page.go +++ b/page.go @@ -22,6 +22,7 @@ type Page struct { PassphraseToUnlock string IsEncrypted bool IsPrimedForSelfDestruct bool + IsPublished bool } func Open(name string) (p *Page) { @@ -105,3 +106,7 @@ func (p *Page) Erase() error { log.Trace("Erasing " + p.Name) return os.Remove(path.Join(pathToData, encodeToBase32(strings.ToLower(p.Name))+".json")) } + +func (p *Page) Published() bool { + return p.IsPublished +} diff --git a/templates/index.tmpl b/templates/index.tmpl index a3f5421..22b7657 100755 --- a/templates/index.tmpl +++ b/templates/index.tmpl @@ -262,6 +262,38 @@ body#pad textarea { }); } + function publishPage() { + $.ajax({ + type: 'POST', + url: '/publish', + data: JSON.stringify({ + page: "{{ .Page }}", + publish: $('#publishPage').text() == "Publish" + }), + success: function(data) { + $('#saveEditButton').removeClass() + if (data.success == true) { + $('#saveEditButton').addClass("success"); + } else { + $('#saveEditButton').addClass("failure"); + } + $('#saveEditButton').text(data.message); + if (data.message == "Unpublished") { + $('#publishPage').text("Publish"); + } else { + $('#publishPage').text("Unpublish"); + } + }, + error: function(xhr, error) { + $('#saveEditButton').removeClass() + $('#saveEditButton').addClass("failure"); + $('#saveEditButton').text(error); + }, + contentType: "application/json", + dataType: 'json' + }); + } + function encryptPage(passphrase) { $.ajax({ type: 'POST', @@ -373,6 +405,25 @@ body#pad textarea { } }); + $("#publishPage").click(function(e) { + e.preventDefault(); + var message = " This will add your page to the sitemap.xml so it will be indexed."; + if ($('#publishPage').text() == "Unpublish") { + message = ""; + } + var confirmed = confirm("Are you sure?" + message); + if (confirmed == true) { + if ($('#publishPage').text() == "Unpublish") { + $('#saveEditButton').removeClass(); + $("#saveEditButton").text("Unpublishing"); + } else { + $('#saveEditButton').removeClass(); + $("#saveEditButton").text("Publishing"); + } + publishPage(); + } + }); + $("#clearOld").click(function(e) { e.preventDefault(); var r = confirm("This will erase all cleared list items, are you sure you want to do that? (Versions will stay in history)."); @@ -439,6 +490,15 @@ body#pad textarea {