Add half arsed user separation
This commit is contained in:
parent
3c792decd6
commit
25bcf4d706
6 changed files with 125 additions and 41 deletions
|
|
@ -4,18 +4,24 @@ import (
|
|||
"log"
|
||||
"net/http"
|
||||
"os"
|
||||
"path"
|
||||
"path/filepath"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
urls "forgejo.gwairfelin.com/max/gispatcho"
|
||||
"forgejo.gwairfelin.com/max/gonotes/internal/conf"
|
||||
"forgejo.gwairfelin.com/max/gonotes/internal/middleware"
|
||||
"forgejo.gwairfelin.com/max/gonotes/internal/notes"
|
||||
"forgejo.gwairfelin.com/max/gonotes/internal/templ"
|
||||
)
|
||||
|
||||
var myurls urls.URLs
|
||||
|
||||
func addRequestContext(r *http.Request, ctx templ.Ctx) templ.Ctx {
|
||||
return ctx
|
||||
}
|
||||
|
||||
func GetRoutes(prefix string) *http.ServeMux {
|
||||
myurls = urls.URLs{
|
||||
Prefix: prefix,
|
||||
|
|
@ -34,8 +40,10 @@ func GetRoutes(prefix string) *http.ServeMux {
|
|||
}
|
||||
|
||||
func view(w http.ResponseWriter, r *http.Request) {
|
||||
user := r.Context().Value(middleware.ContextKey("user")).(string)
|
||||
|
||||
title := r.PathValue("note")
|
||||
note, err := notes.LoadNote(title)
|
||||
note, err := notes.LoadNote(user, title)
|
||||
urlEdit := myurls.Reverse("edit", urls.Repl{"note": title})
|
||||
urlDelete := myurls.Reverse("delete", urls.Repl{"note": title})
|
||||
if err != nil {
|
||||
|
|
@ -45,7 +53,7 @@ func view(w http.ResponseWriter, r *http.Request) {
|
|||
|
||||
context := templ.Ctx{"note": note, "urlEdit": urlEdit, "urlDelete": urlDelete}
|
||||
note.Render()
|
||||
err = templ.RenderTemplate(w, "view.tmpl.html", context)
|
||||
err = templ.RenderTemplate(w, r, "view.tmpl.html", context)
|
||||
if err != nil {
|
||||
log.Print(err.Error())
|
||||
http.Error(w, "Couldn't load template", http.StatusInternalServerError)
|
||||
|
|
@ -54,8 +62,10 @@ func view(w http.ResponseWriter, r *http.Request) {
|
|||
}
|
||||
|
||||
func edit(w http.ResponseWriter, r *http.Request) {
|
||||
user := r.Context().Value(middleware.ContextKey("user")).(string)
|
||||
|
||||
encodedTitle := r.PathValue("note")
|
||||
note, err := notes.LoadNote(encodedTitle)
|
||||
note, err := notes.LoadNote(user, encodedTitle)
|
||||
if err != nil {
|
||||
title := notes.DecodeTitle(encodedTitle)
|
||||
note = ¬es.Note{Title: title}
|
||||
|
|
@ -63,7 +73,7 @@ func edit(w http.ResponseWriter, r *http.Request) {
|
|||
|
||||
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)
|
||||
err = templ.RenderTemplate(w, r, "edit.tmpl.html", context)
|
||||
if err != nil {
|
||||
log.Print(err.Error())
|
||||
http.Error(w, "Couldn't load template", http.StatusInternalServerError)
|
||||
|
|
@ -84,8 +94,10 @@ func new(w http.ResponseWriter, r *http.Request) {
|
|||
}
|
||||
|
||||
func delete(w http.ResponseWriter, r *http.Request) {
|
||||
user := r.Context().Value(middleware.ContextKey("user")).(string)
|
||||
|
||||
encodedTitle := r.PathValue("note")
|
||||
err := notes.DeleteNote(encodedTitle)
|
||||
err := notes.DeleteNote(user, encodedTitle)
|
||||
if err != nil {
|
||||
log.Print(err.Error())
|
||||
http.Error(w, "Couldn't delete note", http.StatusInternalServerError)
|
||||
|
|
@ -97,21 +109,25 @@ func delete(w http.ResponseWriter, r *http.Request) {
|
|||
}
|
||||
|
||||
func save(w http.ResponseWriter, r *http.Request) {
|
||||
user := r.Context().Value(middleware.ContextKey("user")).(string)
|
||||
|
||||
oldTitle := r.PathValue("note")
|
||||
title := r.FormValue("title")
|
||||
body := r.FormValue("body")
|
||||
|
||||
note := ¬es.Note{Title: title, Body: []byte(body)}
|
||||
note := ¬es.Note{Title: title, Body: []byte(body), Owner: user}
|
||||
note.Save()
|
||||
|
||||
if oldTitle != note.EncodedTitle() {
|
||||
notes.DeleteNote(oldTitle)
|
||||
notes.DeleteNote(user, oldTitle)
|
||||
}
|
||||
|
||||
http.Redirect(w, r, myurls.Reverse("view", urls.Repl{"note": note.EncodedTitle()}), http.StatusFound)
|
||||
}
|
||||
|
||||
func togglebox(w http.ResponseWriter, r *http.Request) {
|
||||
user := r.Context().Value(middleware.ContextKey("user")).(string)
|
||||
|
||||
title := r.PathValue("note")
|
||||
nthBox, err := strconv.Atoi(r.FormValue("box"))
|
||||
if err != nil {
|
||||
|
|
@ -119,7 +135,7 @@ func togglebox(w http.ResponseWriter, r *http.Request) {
|
|||
return
|
||||
}
|
||||
|
||||
note, err := notes.LoadNote(title)
|
||||
note, err := notes.LoadNote(user, title)
|
||||
if err != nil {
|
||||
http.Redirect(w, r, myurls.Reverse("view", urls.Repl{"note": title}), http.StatusFound)
|
||||
return
|
||||
|
|
@ -136,11 +152,19 @@ type titleAndURL struct {
|
|||
}
|
||||
|
||||
func list(w http.ResponseWriter, r *http.Request) {
|
||||
files, err := os.ReadDir(conf.Conf.NotesDir)
|
||||
user := r.Context().Value(middleware.ContextKey("user")).(string)
|
||||
|
||||
notesDir := path.Join(conf.Conf.NotesDir, user)
|
||||
|
||||
files, err := os.ReadDir(notesDir)
|
||||
if err != nil {
|
||||
log.Print(err.Error())
|
||||
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
|
||||
return
|
||||
if os.IsNotExist(err) {
|
||||
os.MkdirAll(notesDir, os.FileMode(0750))
|
||||
} else {
|
||||
log.Print(err.Error())
|
||||
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
titlesAndUrls := make([]titleAndURL, 0)
|
||||
|
|
@ -162,7 +186,7 @@ func list(w http.ResponseWriter, r *http.Request) {
|
|||
|
||||
urlNew := myurls.Reverse("new", urls.Repl{})
|
||||
|
||||
err = templ.RenderTemplate(w, "list.tmpl.html", templ.Ctx{"notes": titlesAndUrls, "urlNew": urlNew})
|
||||
err = templ.RenderTemplate(w, r, "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