Base64 encode note titles
This commit is contained in:
parent
eb0c264ad7
commit
6f8796c83f
6 changed files with 78 additions and 22 deletions
|
|
@ -19,12 +19,13 @@ func GetRoutes(prefix string) *http.ServeMux {
|
|||
myurls = urls.URLs{
|
||||
Prefix: prefix,
|
||||
URLs: map[string]urls.URL{
|
||||
"list": {Path: "/", Protocol: "GET", Handler: list},
|
||||
"new": {Path: "/new", Protocol: "GET", Handler: new},
|
||||
"view_": {Path: "/{note}", Protocol: "GET", Handler: view},
|
||||
"view": {Path: "/{note}/", Protocol: "GET", Handler: view},
|
||||
"delete": {Path: "/{note}/delete/", Protocol: "GET", Handler: delete},
|
||||
"edit": {Path: "/{note}/edit/", Protocol: "GET", Handler: edit},
|
||||
"save": {Path: "/{note}/edit/save/", Protocol: "POST", Handler: save},
|
||||
"list": {Path: "/", Protocol: "GET", Handler: list},
|
||||
},
|
||||
}
|
||||
return myurls.GetRouter()
|
||||
|
|
@ -51,13 +52,14 @@ func view(w http.ResponseWriter, r *http.Request) {
|
|||
}
|
||||
|
||||
func edit(w http.ResponseWriter, r *http.Request) {
|
||||
title := r.PathValue("note")
|
||||
note, err := notes.LoadNote(title)
|
||||
encodedTitle := r.PathValue("note")
|
||||
note, err := notes.LoadNote(encodedTitle)
|
||||
if err != nil {
|
||||
title := notes.DecodeTitle(encodedTitle)
|
||||
note = ¬es.Note{Title: title}
|
||||
}
|
||||
|
||||
urlSave := myurls.Reverse("save", urls.Repl{"note": title})
|
||||
urlSave := myurls.Reverse("save", urls.Repl{"note": encodedTitle})
|
||||
context := templ.Ctx{"note": note, "urlSave": urlSave, "text": string(note.Body)}
|
||||
err = templ.RenderTemplate(w, "edit.tmpl.html", context)
|
||||
if err != nil {
|
||||
|
|
@ -67,17 +69,25 @@ func edit(w http.ResponseWriter, r *http.Request) {
|
|||
}
|
||||
}
|
||||
|
||||
func new(w http.ResponseWriter, r *http.Request) {
|
||||
title := r.FormValue("title")
|
||||
note := ¬es.Note{Title: title}
|
||||
|
||||
urlEdit := myurls.Reverse("edit", urls.Repl{"note": note.EncodedTitle()})
|
||||
http.Redirect(w, r, urlEdit, http.StatusFound)
|
||||
}
|
||||
|
||||
func delete(w http.ResponseWriter, r *http.Request) {
|
||||
title := r.PathValue("note")
|
||||
err := notes.DeleteNote(title)
|
||||
encodedTitle := r.PathValue("note")
|
||||
err := notes.DeleteNote(encodedTitle)
|
||||
if err != nil {
|
||||
log.Print(err.Error())
|
||||
http.Error(w, "Couldn't delete note", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
urlDelete := myurls.Reverse("list", urls.Repl{})
|
||||
http.Redirect(w, r, urlDelete, http.StatusFound)
|
||||
urlList := myurls.Reverse("list", urls.Repl{})
|
||||
http.Redirect(w, r, urlList, http.StatusFound)
|
||||
}
|
||||
|
||||
func save(w http.ResponseWriter, r *http.Request) {
|
||||
|
|
@ -88,11 +98,16 @@ func save(w http.ResponseWriter, r *http.Request) {
|
|||
note := ¬es.Note{Title: title, Body: []byte(body)}
|
||||
note.Save()
|
||||
|
||||
if oldTitle != title {
|
||||
if oldTitle != note.EncodedTitle() {
|
||||
notes.DeleteNote(oldTitle)
|
||||
}
|
||||
|
||||
http.Redirect(w, r, myurls.Reverse("view", urls.Repl{"note": title}), http.StatusFound)
|
||||
http.Redirect(w, r, myurls.Reverse("view", urls.Repl{"note": note.EncodedTitle()}), http.StatusFound)
|
||||
}
|
||||
|
||||
type titleAndUrl struct {
|
||||
Title string
|
||||
Url string
|
||||
}
|
||||
|
||||
func list(w http.ResponseWriter, r *http.Request) {
|
||||
|
|
@ -103,16 +118,26 @@ func list(w http.ResponseWriter, r *http.Request) {
|
|||
return
|
||||
}
|
||||
|
||||
titles := make([]string, 0)
|
||||
titlesAndUrls := make([]titleAndUrl, 0)
|
||||
|
||||
for _, f := range files {
|
||||
if !f.IsDir() {
|
||||
title := strings.TrimSuffix(f.Name(), filepath.Ext(f.Name()))
|
||||
titles = append(titles, title)
|
||||
encodedTitle := strings.TrimSuffix(f.Name(), filepath.Ext(f.Name()))
|
||||
title := notes.DecodeTitle(encodedTitle)
|
||||
|
||||
log.Printf("Found note %s (title '%s')", encodedTitle, title)
|
||||
|
||||
titlesAndUrls = append(
|
||||
titlesAndUrls,
|
||||
titleAndUrl{Title: title, Url: myurls.Reverse("view", urls.Repl{"note": encodedTitle})},
|
||||
)
|
||||
log.Print(titlesAndUrls)
|
||||
}
|
||||
}
|
||||
|
||||
err = templ.RenderTemplate(w, "list.tmpl.html", templ.Ctx{"titles": titles})
|
||||
urlNew := myurls.Reverse("new", urls.Repl{})
|
||||
|
||||
err = templ.RenderTemplate(w, "list.tmpl.html", templ.Ctx{"notes": titlesAndUrls, "urlNew": urlNew})
|
||||
if err != nil {
|
||||
log.Print(err.Error())
|
||||
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue