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

Added admin priveleges to list all the notes

This commit is contained in:
Zack Scholl 2016-02-08 17:37:33 -05:00
parent b704768ded
commit e33e3a7d1b
2 changed files with 29 additions and 9 deletions

View File

@ -27,6 +27,7 @@ var RuntimeArgs struct {
ServerCRT string ServerCRT string
ServerKey string ServerKey string
SourcePath string SourcePath string
AdminKey string
} }
func main() { func main() {
@ -34,6 +35,7 @@ func main() {
databaseFile := path.Join(path.Dir(executableFile), "data.db") databaseFile := path.Join(path.Dir(executableFile), "data.db")
flag.StringVar(&RuntimeArgs.Port, "p", ":12312", "port to bind") flag.StringVar(&RuntimeArgs.Port, "p", ":12312", "port to bind")
flag.StringVar(&RuntimeArgs.DatabaseLocation, "db", databaseFile, "location of database file") flag.StringVar(&RuntimeArgs.DatabaseLocation, "db", databaseFile, "location of database file")
flag.StringVar(&RuntimeArgs.AdminKey, "a", "", "key to use admin priveleges")
flag.StringVar(&RuntimeArgs.ServerCRT, "crt", "", "location of ssl crt") flag.StringVar(&RuntimeArgs.ServerCRT, "crt", "", "location of ssl crt")
flag.StringVar(&RuntimeArgs.ServerKey, "key", "", "location of ssl key") flag.StringVar(&RuntimeArgs.ServerKey, "key", "", "location of ssl key")
flag.CommandLine.Usage = func() { flag.CommandLine.Usage = func() {

View File

@ -9,6 +9,7 @@ import (
"strconv" "strconv"
"strings" "strings"
"github.com/boltdb/bolt"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"github.com/microcosm-cc/bluemonday" "github.com/microcosm-cc/bluemonday"
"github.com/russross/blackfriday" "github.com/russross/blackfriday"
@ -48,7 +49,14 @@ func everythingElse(c *gin.Context) {
option := c.Param("option") option := c.Param("option")
title := c.Param("title") title := c.Param("title")
if option == "/view" { if option == "/view" {
renderMarkdown(c, title) var p CowyoData
err := p.load(strings.ToLower(title))
if err != nil {
panic(err)
}
renderMarkdown(c, p.CurrentText, title)
} else if option == "/"+RuntimeArgs.AdminKey && len(RuntimeArgs.AdminKey) > 1 {
renderMarkdown(c, listEverything(), "Everything")
} else if option == "/list" { } else if option == "/list" {
renderList(c, title) renderList(c, title)
} else if title == "static" { } else if title == "static" {
@ -67,13 +75,8 @@ func serveStaticFile(c *gin.Context, option string) {
} }
} }
func renderMarkdown(c *gin.Context, title string) { func renderMarkdown(c *gin.Context, currentText string, title string) {
var p CowyoData unsafe := blackfriday.MarkdownCommon([]byte(currentText))
err := p.load(strings.ToLower(title))
if err != nil {
panic(err)
}
unsafe := blackfriday.MarkdownCommon([]byte(p.CurrentText))
html := bluemonday.UGCPolicy().SanitizeBytes(unsafe) html := bluemonday.UGCPolicy().SanitizeBytes(unsafe)
html2 := string(html) html2 := string(html)
r, _ := regexp.Compile("\\$\\$(.*?)\\$\\$") r, _ := regexp.Compile("\\$\\$(.*?)\\$\\$")
@ -186,5 +189,20 @@ func deleteListItem(c *gin.Context) {
"message": "?", "message": "?",
}) })
} }
}
func listEverything() string {
everything := ""
db.View(func(tx *bolt.Tx) error {
// Assume bucket exists and has keys
b := tx.Bucket([]byte("datas"))
c := b.Cursor()
for k, v := c.First(); k != nil; k, v = c.Next() {
if len(v) > 1 {
everything += "- [" + string(k) + "](/" + string(k) + "/view) (" + strconv.Itoa(len(v)) + ")\n"
}
}
return nil
})
return everything
} }