Make use of new gispatcho
This commit is contained in:
parent
c6975d3814
commit
e6b5ea3921
6 changed files with 22 additions and 59 deletions
|
|
@ -8,9 +8,9 @@ import (
|
|||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
urls "gitea.gwairfelin.com/max/gispatcho"
|
||||
"gitea.gwairfelin.com/max/gonotes/internal/conf"
|
||||
"gitea.gwairfelin.com/max/gonotes/internal/notes"
|
||||
"gitea.gwairfelin.com/max/gonotes/internal/urls"
|
||||
)
|
||||
|
||||
var myurls urls.URLs
|
||||
|
|
@ -31,13 +31,15 @@ func GetRoutes(prefix string) *http.ServeMux {
|
|||
func view(w http.ResponseWriter, r *http.Request) {
|
||||
title := r.PathValue("note")
|
||||
note, err := notes.LoadNote(title)
|
||||
urlEdit := myurls.Reverse("edit", urls.Repl{"note": title})
|
||||
if err != nil {
|
||||
http.Redirect(w, r, myurls.Reverse("edit", map[string]string{"note": title}), http.StatusFound)
|
||||
http.Redirect(w, r, urlEdit, http.StatusFound)
|
||||
return
|
||||
}
|
||||
|
||||
context := map[string]any{"note": note, "urlEdit": urlEdit}
|
||||
note.Render()
|
||||
err = renderTemplate(w, "view.html", note)
|
||||
err = renderTemplate(w, "view.html", context)
|
||||
if err != nil {
|
||||
log.Print(err.Error())
|
||||
http.Error(w, "Couldn't load template", http.StatusInternalServerError)
|
||||
|
|
@ -52,7 +54,9 @@ func edit(w http.ResponseWriter, r *http.Request) {
|
|||
note = ¬es.Note{Title: title}
|
||||
}
|
||||
|
||||
err = renderTemplate(w, "edit.html", note)
|
||||
urlSave := myurls.Reverse("save", urls.Repl{"note": title})
|
||||
context := map[string]any{"note": note, "urlSave": urlSave}
|
||||
err = renderTemplate(w, "edit.html", context)
|
||||
if err != nil {
|
||||
log.Print(err.Error())
|
||||
http.Error(w, "Couldn't load template", http.StatusInternalServerError)
|
||||
|
|
@ -65,7 +69,7 @@ func save(w http.ResponseWriter, r *http.Request) {
|
|||
body := r.FormValue("body")
|
||||
note := ¬es.Note{Title: title, Body: []byte(body)}
|
||||
note.Save()
|
||||
http.Redirect(w, r, myurls.Reverse("view", map[string]string{"note": title}), http.StatusFound)
|
||||
http.Redirect(w, r, myurls.Reverse("view", urls.Repl{"note": title}), http.StatusFound)
|
||||
}
|
||||
|
||||
func list(w http.ResponseWriter, r *http.Request) {
|
||||
|
|
|
|||
|
|
@ -1,47 +0,0 @@
|
|||
// Package to manage routing of urls in net/http
|
||||
// URLs type is the main thing of interest
|
||||
package urls
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
"regexp"
|
||||
)
|
||||
|
||||
// Represent a single URL with a name, a pattern and supported protocol
|
||||
type URL struct {
|
||||
Handler func(http.ResponseWriter, *http.Request)
|
||||
Path string
|
||||
Protocol string
|
||||
}
|
||||
|
||||
// Return the corresponding net/http pattern for a URL
|
||||
func (url *URL) GetPattern() string {
|
||||
return fmt.Sprintf("%s %s", url.Protocol, url.Path)
|
||||
}
|
||||
|
||||
// Represent a set of URLs at a prefix
|
||||
type URLs struct {
|
||||
URLs map[string]URL
|
||||
Prefix string
|
||||
}
|
||||
|
||||
// Given a name and replacements, return a rendered path component of a URL
|
||||
func (urls URLs) Reverse(name string, replacements map[string]string) string {
|
||||
pattern := urls.URLs[name].Path
|
||||
|
||||
for key, val := range replacements {
|
||||
re := regexp.MustCompile("{" + key + "}")
|
||||
pattern = re.ReplaceAllString(pattern, val)
|
||||
}
|
||||
return urls.Prefix + pattern
|
||||
}
|
||||
|
||||
// Return router for the urls
|
||||
func (urls URLs) GetRouter() *http.ServeMux {
|
||||
router := http.NewServeMux()
|
||||
for _, url := range urls.URLs {
|
||||
router.HandleFunc(url.GetPattern(), url.Handler)
|
||||
}
|
||||
return router
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue