Make use of new gispatcho
This commit is contained in:
parent
c6975d3814
commit
e6b5ea3921
6 changed files with 22 additions and 59 deletions
2
go.mod
2
go.mod
|
|
@ -5,3 +5,5 @@ go 1.23.5
|
|||
require github.com/yuin/goldmark v1.7.8
|
||||
|
||||
require github.com/pelletier/go-toml v1.9.5
|
||||
|
||||
require gitea.gwairfelin.com/max/gispatcho v0.1.1
|
||||
|
|
|
|||
2
go.sum
2
go.sum
|
|
@ -1,3 +1,5 @@
|
|||
gitea.gwairfelin.com/max/gispatcho v0.1.1 h1:8Zv6UH046mSSLdf0PqpNRK6DACuWJfgBSUiWhMW0sMY=
|
||||
gitea.gwairfelin.com/max/gispatcho v0.1.1/go.mod h1:TRhxU+uNv+nIcFIxy2jl30DPbcM5Yib0S/cpLXws5iM=
|
||||
github.com/pelletier/go-toml v1.9.5 h1:4yBQzkHv+7BHq2PQUZF3Mx0IYxG7LsP222s7Agd3ve8=
|
||||
github.com/pelletier/go-toml v1.9.5/go.mod h1:u1nR/EPcESfeI/szUZKdtJ0xRNbUoANCkoOuaOx1Y+c=
|
||||
github.com/yuin/goldmark v1.7.8 h1:iERMLn0/QJeHFhxSt3p6PeN9mGnvIKSpG9YYorDMnic=
|
||||
|
|
|
|||
|
|
@ -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
|
||||
}
|
||||
|
|
@ -1,11 +1,13 @@
|
|||
{{define "title"}}Edit {{.Title}}{{end}}
|
||||
{{define "title"}}Edit {{.note.Title}}{{end}}
|
||||
|
||||
{{define "main"}}
|
||||
<form action="save/" method="POST">
|
||||
<form action="{{.urlEdit}}" method="POST">
|
||||
<div class="mb-3">
|
||||
<label for="noteBodyInput" class="form-label">Note</label>
|
||||
<div id="toolbar"></div>
|
||||
<textarea class="form-control" id="noteBodyInput" name="body" aria-described-by="bodyHelp">{{printf "%s" .Body}}</textarea>
|
||||
<div class="border rounded rounded-1">
|
||||
<div id="toolbar"></div>
|
||||
<textarea class="form-control" id="noteBodyInput" name="body" aria-described-by="bodyHelp">{{.noteBody}}</textarea>
|
||||
</div>
|
||||
<div id="bodyHelp" class="form-text">Enter your note content in markdown</div>
|
||||
</div>
|
||||
<button class="btn btn-primary" type="submit">Save</button>
|
||||
|
|
|
|||
|
|
@ -1,10 +1,10 @@
|
|||
{{define "title"}}{{.Title}}{{end}}
|
||||
{{define "title"}}{{.note.Title}}{{end}}
|
||||
{{define "main"}}
|
||||
<div>
|
||||
{{.BodyRendered}}
|
||||
{{.note.BodyRendered}}
|
||||
</div>
|
||||
<div>
|
||||
<a href="edit/">Edit</a>
|
||||
<a href="{{.urlEdit}}">Edit</a>
|
||||
</div>
|
||||
{{end}}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue