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:
parent
683bcdea1c
commit
107a0dc32b
61
handlers.go
61
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 = `<?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) {
|
||||
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"`
|
||||
|
5
page.go
5
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
|
||||
}
|
||||
|
@ -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 {
|
||||
<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="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>
|
||||
{{ if (or (.IsLocked) (.IsEncrypted) )}}
|
||||
{{ else }}
|
||||
|
Loading…
Reference in New Issue
Block a user