2022-02-17 22:09:03 +01:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"html/template"
|
2022-03-02 22:33:43 +01:00
|
|
|
"io/ioutil"
|
2022-02-17 22:09:03 +01:00
|
|
|
"log"
|
|
|
|
"net/http"
|
|
|
|
"os"
|
2022-03-02 22:33:43 +01:00
|
|
|
"strings"
|
2022-02-17 22:09:03 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
type Page struct {
|
|
|
|
Title string
|
|
|
|
Body []byte
|
|
|
|
}
|
|
|
|
|
|
|
|
func loadPage(title string) (*Page, error) {
|
|
|
|
body, err := os.ReadFile(title + ".txt")
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &Page{Title: title, Body: body}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Page) savePage() error {
|
|
|
|
filename := p.Title + ".txt"
|
|
|
|
return os.WriteFile(filename, p.Body, 0600)
|
|
|
|
}
|
|
|
|
|
|
|
|
func viewHandler(w http.ResponseWriter, r *http.Request) {
|
|
|
|
title := r.URL.Path[len("/view/"):]
|
|
|
|
page, err := loadPage(title)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Fprintf(w, "<h1>ERROR</h1><p>%s not found!</p>", title)
|
|
|
|
} else {
|
|
|
|
fmt.Fprintf(w, "<h1>%s</h1><p>%s</p>", page.Title, string(page.Body))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func saveHandler(w http.ResponseWriter, r *http.Request) {
|
|
|
|
title := r.URL.Path[len("/save/"):]
|
|
|
|
p := &Page{Title: title, Body: []byte(r.FormValue("body"))}
|
|
|
|
p.savePage()
|
|
|
|
http.Redirect(w, r, "/view/"+title, http.StatusFound)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func editHandler(w http.ResponseWriter, r *http.Request) {
|
|
|
|
title := r.URL.Path[len("/edit/"):]
|
|
|
|
page, err := loadPage(title)
|
|
|
|
if err != nil {
|
|
|
|
page = &Page{Title: title}
|
|
|
|
}
|
|
|
|
t, _ := template.ParseFiles("edit.html")
|
|
|
|
t.Execute(w, page)
|
|
|
|
}
|
|
|
|
|
2022-03-02 22:33:43 +01:00
|
|
|
func indexHandler(w http.ResponseWriter, r *http.Request) {
|
|
|
|
title := "index"
|
|
|
|
//template, err := template.New("index").ParseFiles("templates/index.html")
|
|
|
|
template, err := template.New("index").Parse("<!DOCTYPE html><ul>{{range $p := .}}<li><a href=view/{{$p.Title}}>{{$p.Title}}</a></li>{{end}}</ul>")
|
|
|
|
if err != nil {
|
|
|
|
fmt.Fprintf(w, "<h1>ERROR</h1><p>%s not found!</p>", title)
|
|
|
|
} else {
|
|
|
|
files, err := ioutil.ReadDir(".")
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
} else {
|
|
|
|
var pages []Page
|
|
|
|
for _, file := range files {
|
|
|
|
if strings.Contains(file.Name(), ".txt") {
|
|
|
|
page, _ := loadPage(strings.Split(file.Name(), ".")[0])
|
|
|
|
pages = append(pages, *page)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
err = template.Execute(w, pages)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2022-02-17 22:09:03 +01:00
|
|
|
func main() {
|
|
|
|
http.HandleFunc("/view/", viewHandler)
|
|
|
|
http.HandleFunc("/save/", saveHandler)
|
|
|
|
http.HandleFunc("/edit/", editHandler)
|
2022-03-02 22:33:43 +01:00
|
|
|
http.HandleFunc("/", indexHandler)
|
2022-02-17 22:09:03 +01:00
|
|
|
log.Fatal(http.ListenAndServe(":8080", nil))
|
|
|
|
}
|