Compare commits
4 commits
6ff9b673ab
...
c1e8ee336d
| Author | SHA1 | Date | |
|---|---|---|---|
| c1e8ee336d | |||
| 043b1ed7c6 | |||
| 2bf278c312 | |||
| 56d3e153cb |
5 changed files with 112 additions and 0 deletions
2
.gitignore
vendored
Normal file
2
.gitignore
vendored
Normal file
|
|
@ -0,0 +1,2 @@
|
||||||
|
galley
|
||||||
|
data
|
||||||
87
galley.go
Normal file
87
galley.go
Normal file
|
|
@ -0,0 +1,87 @@
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"html/template"
|
||||||
|
"log"
|
||||||
|
"net/http"
|
||||||
|
"os"
|
||||||
|
"regexp"
|
||||||
|
)
|
||||||
|
|
||||||
|
// cache templates on initialization
|
||||||
|
var templates = template.Must(template.ParseFiles("tmpl/edit.html", "tmpl/view.html"))
|
||||||
|
|
||||||
|
var validPath = regexp.MustCompile("^/(edit|save|view)/([a-zA-Z0-9]+)$")
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
http.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.Dir("static"))))
|
||||||
|
http.HandleFunc("/view/", makeHandler(viewHandler))
|
||||||
|
http.HandleFunc("/edit/", makeHandler(editHandler))
|
||||||
|
http.HandleFunc("/save/", makeHandler(saveHandler))
|
||||||
|
log.Fatal(http.ListenAndServe(":8080", nil))
|
||||||
|
}
|
||||||
|
|
||||||
|
type Page struct {
|
||||||
|
Title string
|
||||||
|
Body []byte
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *Page) save() error {
|
||||||
|
filename := "data/" + p.Title + ".txt"
|
||||||
|
return os.WriteFile(filename, p.Body, 0600)
|
||||||
|
}
|
||||||
|
|
||||||
|
func loadPage(title string) (*Page, error) {
|
||||||
|
filename := "data/" + title + ".txt"
|
||||||
|
body, err := os.ReadFile(filename)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return &Page{Title: title, Body: body}, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func viewHandler(w http.ResponseWriter, r *http.Request, title string) {
|
||||||
|
p, err := loadPage(title)
|
||||||
|
if err != nil {
|
||||||
|
http.Redirect(w, r, "/edit/"+title, http.StatusFound)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
renderTemplate(w, "view", p)
|
||||||
|
}
|
||||||
|
|
||||||
|
func editHandler(w http.ResponseWriter, r *http.Request, title string) {
|
||||||
|
p, err := loadPage(title)
|
||||||
|
if err != nil {
|
||||||
|
p = &Page{Title: title}
|
||||||
|
}
|
||||||
|
renderTemplate(w, "edit", p)
|
||||||
|
}
|
||||||
|
|
||||||
|
func saveHandler(w http.ResponseWriter, r *http.Request, title string) {
|
||||||
|
body := r.FormValue("body")
|
||||||
|
p := &Page{Title: title, Body: []byte(body)}
|
||||||
|
err := p.save()
|
||||||
|
if err != nil {
|
||||||
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||||
|
}
|
||||||
|
http.Redirect(w, r, "/view/"+title, http.StatusFound)
|
||||||
|
}
|
||||||
|
|
||||||
|
func renderTemplate(w http.ResponseWriter, tmpl string, p *Page) {
|
||||||
|
err := templates.ExecuteTemplate(w, tmpl + ".html", p)
|
||||||
|
if err != nil {
|
||||||
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// function that takes in and modifies a function
|
||||||
|
func makeHandler(fn func(http.ResponseWriter, *http.Request, string)) http.HandlerFunc {
|
||||||
|
return func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
m := validPath.FindStringSubmatch(r.URL.Path)
|
||||||
|
if m == nil {
|
||||||
|
http.NotFound(w, r)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
fn(w, r, m[2])
|
||||||
|
}
|
||||||
|
}
|
||||||
3
go.mod
Normal file
3
go.mod
Normal file
|
|
@ -0,0 +1,3 @@
|
||||||
|
module galley
|
||||||
|
|
||||||
|
go 1.25.1
|
||||||
6
tmpl/edit.html
Normal file
6
tmpl/edit.html
Normal file
|
|
@ -0,0 +1,6 @@
|
||||||
|
<h1>Editing {{.Title}}</h1>
|
||||||
|
|
||||||
|
<form action="/save/{{.Title}}" method="POST">
|
||||||
|
<div><textarea name="body" rows="20" cols="80">{{printf "%s" .Body}}</textarea></div>
|
||||||
|
<div><input type="submit" value="Save"></div>
|
||||||
|
</form>
|
||||||
14
tmpl/view.html
Normal file
14
tmpl/view.html
Normal file
|
|
@ -0,0 +1,14 @@
|
||||||
|
<!doctype html>
|
||||||
|
<html lang="en">
|
||||||
|
|
||||||
|
<head>
|
||||||
|
<link href="/static/css/main.css" rel="stylesheet" />
|
||||||
|
</head>
|
||||||
|
|
||||||
|
<body>
|
||||||
|
<h1>{{.Title}}</h1>
|
||||||
|
<p>[<a href="/edit/{{.Title}}">edit</a>]</p>
|
||||||
|
<div>{{printf "%s" .Body}}<div>
|
||||||
|
</body>
|
||||||
|
|
||||||
|
</html>
|
||||||
Loading…
Reference in a new issue