Initial commit with cru (no delete) of notes
This commit is contained in:
commit
1e1174f9ca
11 changed files with 244 additions and 0 deletions
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
|
|
@ -0,0 +1 @@
|
||||||
|
main
|
||||||
19
cmd/server/main.go
Normal file
19
cmd/server/main.go
Normal file
|
|
@ -0,0 +1,19 @@
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"log"
|
||||||
|
"net/http"
|
||||||
|
|
||||||
|
"gitea.gwairfelin.com/max/gonotes/internal/conf"
|
||||||
|
"gitea.gwairfelin.com/max/gonotes/internal/notes/views"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
conf.LoadConfig("./conf.toml")
|
||||||
|
|
||||||
|
router := http.NewServeMux()
|
||||||
|
notesRouter := views.GetRoutes("/notes")
|
||||||
|
|
||||||
|
router.Handle("/notes/", http.StripPrefix("/notes", notesRouter))
|
||||||
|
log.Fatal(http.ListenAndServe(":8080", router))
|
||||||
|
}
|
||||||
3
conf.toml
Normal file
3
conf.toml
Normal file
|
|
@ -0,0 +1,3 @@
|
||||||
|
extension = "md"
|
||||||
|
notesdir = "saved_notes"
|
||||||
|
templatesdir = "templates"
|
||||||
7
go.mod
Normal file
7
go.mod
Normal file
|
|
@ -0,0 +1,7 @@
|
||||||
|
module gitea.gwairfelin.com/max/gonotes
|
||||||
|
|
||||||
|
go 1.23.5
|
||||||
|
|
||||||
|
require github.com/yuin/goldmark v1.7.8
|
||||||
|
|
||||||
|
require github.com/pelletier/go-toml v1.9.5
|
||||||
4
go.sum
Normal file
4
go.sum
Normal file
|
|
@ -0,0 +1,4 @@
|
||||||
|
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=
|
||||||
|
github.com/yuin/goldmark v1.7.8/go.mod h1:uzxRWxtg69N339t3louHJ7+O03ezfj6PlliRlaOzY1E=
|
||||||
30
internal/conf/conf.go
Normal file
30
internal/conf/conf.go
Normal file
|
|
@ -0,0 +1,30 @@
|
||||||
|
package conf
|
||||||
|
|
||||||
|
import (
|
||||||
|
"log"
|
||||||
|
"os"
|
||||||
|
|
||||||
|
"github.com/pelletier/go-toml"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Config struct {
|
||||||
|
Extension string
|
||||||
|
NotesDir string
|
||||||
|
TemplatesDir string
|
||||||
|
}
|
||||||
|
|
||||||
|
var Conf Config
|
||||||
|
|
||||||
|
func LoadConfig(path string) {
|
||||||
|
var err error
|
||||||
|
|
||||||
|
b, err := os.ReadFile(path)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
err = toml.Unmarshal([]byte(b), &Conf)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
52
internal/notes/notes.go
Normal file
52
internal/notes/notes.go
Normal file
|
|
@ -0,0 +1,52 @@
|
||||||
|
// Notes implements a data structure for reasoning about and rendering
|
||||||
|
// markdown notes
|
||||||
|
package notes
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"fmt"
|
||||||
|
"log"
|
||||||
|
"os"
|
||||||
|
"path/filepath"
|
||||||
|
|
||||||
|
"gitea.gwairfelin.com/max/gonotes/internal/conf"
|
||||||
|
"github.com/yuin/goldmark"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Note is the central data structure. It can be Saved, Rendered and Loaded
|
||||||
|
// using the Save, Render and LoadNote functions.
|
||||||
|
type Note struct {
|
||||||
|
Title string
|
||||||
|
BodyRendered string
|
||||||
|
Body []byte
|
||||||
|
}
|
||||||
|
|
||||||
|
func fmtPath(path string) string {
|
||||||
|
return fmt.Sprintf("%s.%s", path, conf.Conf.Extension)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Save a note to a path derived from the title
|
||||||
|
func (n *Note) Save() error {
|
||||||
|
filename := filepath.Join(conf.Conf.NotesDir, fmtPath(n.Title))
|
||||||
|
return os.WriteFile(filename, n.Body, 0600)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Render the markdown content of the note to HTML
|
||||||
|
func (n *Note) Render() {
|
||||||
|
var buf bytes.Buffer
|
||||||
|
err := goldmark.Convert(n.Body, &buf)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
n.BodyRendered = buf.String()
|
||||||
|
}
|
||||||
|
|
||||||
|
// Load a note from the disk. The path is derived from the title
|
||||||
|
func LoadNote(title string) (*Note, error) {
|
||||||
|
filename := filepath.Join(conf.Conf.NotesDir, fmtPath(title))
|
||||||
|
body, err := os.ReadFile(filename)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return &Note{Title: title, Body: body}, nil
|
||||||
|
}
|
||||||
72
internal/notes/views/views.go
Normal file
72
internal/notes/views/views.go
Normal file
|
|
@ -0,0 +1,72 @@
|
||||||
|
package views
|
||||||
|
|
||||||
|
import (
|
||||||
|
"html/template"
|
||||||
|
"log"
|
||||||
|
"net/http"
|
||||||
|
"path/filepath"
|
||||||
|
|
||||||
|
"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
|
||||||
|
|
||||||
|
func GetRoutes(prefix string) *http.ServeMux {
|
||||||
|
myurls = urls.URLs{
|
||||||
|
Prefix: prefix,
|
||||||
|
URLs: map[string]urls.URL{
|
||||||
|
"view": {Path: "/{note}/", Protocol: "GET", Handler: viewHandler},
|
||||||
|
"edit": {Path: "/{note}/edit/", Protocol: "GET", Handler: editHandler},
|
||||||
|
"save": {Path: "/{note}/edit/save/", Protocol: "POST", Handler: saveHandler},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
return myurls.GetRouter()
|
||||||
|
}
|
||||||
|
|
||||||
|
func viewHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
|
title := r.PathValue("note")
|
||||||
|
note, err := notes.LoadNote(title)
|
||||||
|
if err != nil {
|
||||||
|
http.Redirect(w, r, myurls.Reverse("edit", map[string]string{"note": title}), http.StatusFound)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
note.Render()
|
||||||
|
err = renderTemplate(w, "view.html", note)
|
||||||
|
if err != nil {
|
||||||
|
log.Println(err)
|
||||||
|
http.Error(w, "Couldn't load template", http.StatusInternalServerError)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func editHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
|
title := r.PathValue("note")
|
||||||
|
note, err := notes.LoadNote(title)
|
||||||
|
if err != nil {
|
||||||
|
note = ¬es.Note{Title: title}
|
||||||
|
}
|
||||||
|
|
||||||
|
err = renderTemplate(w, "edit.html", note)
|
||||||
|
if err != nil {
|
||||||
|
log.Println(err)
|
||||||
|
http.Error(w, "Couldn't load template", http.StatusInternalServerError)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func saveHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
|
title := r.PathValue("note")
|
||||||
|
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)
|
||||||
|
}
|
||||||
|
|
||||||
|
func renderTemplate(w http.ResponseWriter, tmpl string, note *notes.Note) error {
|
||||||
|
t, err := template.ParseFiles(filepath.Join(conf.Conf.TemplatesDir, tmpl))
|
||||||
|
t.Execute(w, note)
|
||||||
|
return err
|
||||||
|
}
|
||||||
47
internal/urls/urls.go
Normal file
47
internal/urls/urls.go
Normal file
|
|
@ -0,0 +1,47 @@
|
||||||
|
// 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
|
||||||
|
}
|
||||||
6
templates/edit.html
Normal file
6
templates/edit.html
Normal file
|
|
@ -0,0 +1,6 @@
|
||||||
|
<h1>Editing {{.Title}}</h1>
|
||||||
|
|
||||||
|
<form action="save/" method="POST">
|
||||||
|
<textarea name="body">{{printf "%s" .Body}}</textarea>
|
||||||
|
<input type="submit" value="Save">
|
||||||
|
</form>
|
||||||
3
templates/view.html
Normal file
3
templates/view.html
Normal file
|
|
@ -0,0 +1,3 @@
|
||||||
|
<h1>{{.Title}}</h1>
|
||||||
|
|
||||||
|
<div>{{printf "%s" .BodyRendered}}</div>
|
||||||
Loading…
Add table
Reference in a new issue