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

Add publishing, for sitemap.xml

This commit is contained in:
Zack Scholl 2017-10-15 07:49:40 -06:00
parent 683bcdea1c
commit 107a0dc32b
4 changed files with 128 additions and 0 deletions

View File

@ -1,7 +1,9 @@
package main package main
import ( import (
"fmt"
"html/template" "html/template"
"io/ioutil"
"net/http" "net/http"
"strconv" "strconv"
"strings" "strings"
@ -34,6 +36,7 @@ func serve(host, port, crt_path, key_path string, TLS bool) {
router.POST("/exists", handlePageExists) router.POST("/exists", handlePageExists)
router.POST("/prime", handlePrime) router.POST("/prime", handlePrime)
router.POST("/lock", handleLock) router.POST("/lock", handleLock)
router.POST("/publish", handlePublish)
router.POST("/encrypt", handleEncrypt) router.POST("/encrypt", handleEncrypt)
router.DELETE("/oldlist", handleClearOldListItems) router.DELETE("/oldlist", handleClearOldListItems)
router.DELETE("/listitem", deleteListItem) router.DELETE("/listitem", deleteListItem)
@ -103,8 +106,44 @@ func handlePageRelinquish(c *gin.Context) {
"destroyed": destroyed}) "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 = `<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">`
for i := range names {
sitemap += fmt.Sprintf(`
<url>
<loc>https://cowyo.com/%s/view</loc>
<lastmod>%s</lastmod>
<changefreq>monthly</changefreq>
<priority>0.8</priority>
</url>
`, names[i], lastEdited[i])
}
sitemap += "</urlset>"
return
}
func handlePageRequest(c *gin.Context) { func handlePageRequest(c *gin.Context) {
page := c.Param("page") page := c.Param("page")
if page == "sitemap.xml" {
c.Data(http.StatusOK, contentType("sitemap.xml"), []byte(generateSiteMap()))
return
}
command := c.Param("command") command := c.Param("command")
if len(command) < 2 { if len(command) < 2 {
c.Redirect(302, "/"+page+"/edit") c.Redirect(302, "/"+page+"/edit")
@ -223,6 +262,7 @@ func handlePageRequest(c *gin.Context) {
"Route": "/" + page + command, "Route": "/" + page + command,
"HasDotInName": strings.Contains(page, "."), "HasDotInName": strings.Contains(page, "."),
"RecentlyEdited": getRecentlyEdited(page, c), "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}) 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) { func handleEncrypt(c *gin.Context) {
type QueryJSON struct { type QueryJSON struct {
Page string `json:"page"` Page string `json:"page"`

View File

@ -22,6 +22,7 @@ type Page struct {
PassphraseToUnlock string PassphraseToUnlock string
IsEncrypted bool IsEncrypted bool
IsPrimedForSelfDestruct bool IsPrimedForSelfDestruct bool
IsPublished bool
} }
func Open(name string) (p *Page) { func Open(name string) (p *Page) {
@ -105,3 +106,7 @@ func (p *Page) Erase() error {
log.Trace("Erasing " + p.Name) log.Trace("Erasing " + p.Name)
return os.Remove(path.Join(pathToData, encodeToBase32(strings.ToLower(p.Name))+".json")) return os.Remove(path.Join(pathToData, encodeToBase32(strings.ToLower(p.Name))+".json"))
} }
func (p *Page) Published() bool {
return p.IsPublished
}

View File

@ -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) { function encryptPage(passphrase) {
$.ajax({ $.ajax({
type: 'POST', 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) { $("#clearOld").click(function(e) {
e.preventDefault(); 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)."); 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 {
<ul class="pure-menu-children"> <ul class="pure-menu-children">
<li class="pure-menu-item"><a href="/" class="pure-menu-link">New</a></li> <li class="pure-menu-item"><a href="/" class="pure-menu-link">New</a></li>
<li class="pure-menu-item"><a href="https://github.com/schollz/cowyo" class="pure-menu-link">Source</a></li> <li class="pure-menu-item"><a href="https://github.com/schollz/cowyo" class="pure-menu-link">Source</a></li>
{{ if .EditPage }}
<li class="pure-menu-item"><a href="#" class="pure-menu-link" id="publishPage">
{{- if .IsPublished -}}
Unpublish
{{- else -}}
Publish
{{- end -}}
</a></li>
{{ end }}
<hr> <hr>
{{ if (or (.IsLocked) (.IsEncrypted) )}} {{ if (or (.IsLocked) (.IsEncrypted) )}}
{{ else }} {{ else }}

View File

@ -85,6 +85,8 @@ func contentType(filename string) string {
return "image/png" return "image/png"
case strings.Contains(filename, ".js"): case strings.Contains(filename, ".js"):
return "application/javascript" return "application/javascript"
case strings.Contains(filename, ".xml"):
return "application/xml"
} }
return "text/html" return "text/html"
} }