move leaf to own file

master
Bel LaPointe 2022-02-17 11:00:30 -07:00
parent ced507ca68
commit 2bec0dd1b6
2 changed files with 57 additions and 52 deletions

57
server/leaf.go Normal file
View File

@ -0,0 +1,57 @@
package main
import (
"io"
"io/ioutil"
"net/http"
"strings"
)
type Leaf struct {
Meta Meta
Content string
}
type Meta struct {
Title string
Deleted bool
}
func NewHTTPRequestLeaf(r *http.Request) (Leaf, error) {
var leaf Leaf
if b, err := ioutil.ReadAll(r.Body); err != nil {
return leaf, err
} else {
leaf.Content = string(b)
}
if leaf.Meta.Title = r.Header.Get("Title"); leaf.Meta.Title == "" {
leaf.Meta.Title = "Untitled"
}
leaf.Meta.Deleted = r.Method == http.MethodDelete
return leaf, nil
}
func NewLeaf(title string, content string) (Leaf, error) {
return NewHTTPRequestLeaf(&http.Request{
Header: http.Header{"Title": []string{title}},
Body: io.NopCloser(strings.NewReader(content)),
})
}
func (leaf Leaf) WriteHTTP(w http.ResponseWriter) error {
w.Header().Set("Title", leaf.Meta.Title)
_, err := w.Write([]byte(leaf.Content))
return err
}
func (base Leaf) Merge(updated Leaf) Leaf {
if updated.Meta.Title != "" {
base.Meta.Title = updated.Meta.Title
}
if base.Meta.Title == "" {
base.Meta.Title = "Untitled"
}
base.Meta.Deleted = updated.Meta.Deleted
base.Content = updated.Content
return base
}

View File

@ -2,43 +2,13 @@ package main
import (
"encoding/json"
"io"
"io/ioutil"
"net/http"
"os"
"path"
"strings"
yaml "gopkg.in/yaml.v2"
)
func NewHTTPRequestLeaf(r *http.Request) (Leaf, error) {
var leaf Leaf
if b, err := ioutil.ReadAll(r.Body); err != nil {
return leaf, err
} else {
leaf.Content = string(b)
}
if leaf.Meta.Title = r.Header.Get("Title"); leaf.Meta.Title == "" {
leaf.Meta.Title = "Untitled"
}
leaf.Meta.Deleted = r.Method == http.MethodDelete
return leaf, nil
}
func NewLeaf(title string, content string) (Leaf, error) {
return NewHTTPRequestLeaf(&http.Request{
Header: http.Header{"Title": []string{title}},
Body: io.NopCloser(strings.NewReader(content)),
})
}
func (leaf Leaf) WriteHTTP(w http.ResponseWriter) error {
w.Header().Set("Title", leaf.Meta.Title)
_, err := w.Write([]byte(leaf.Content))
return err
}
type Branch struct {
Leaf Leaf
Branches map[ID]Branch
@ -64,28 +34,6 @@ func (branch Branch) forEach(preid ID, foo func(ID, Leaf) error) error {
return nil
}
type Meta struct {
Title string
Deleted bool
}
type Leaf struct {
Meta Meta
Content string
}
func (base Leaf) Merge(updated Leaf) Leaf {
if updated.Meta.Title != "" {
base.Meta.Title = updated.Meta.Title
}
if base.Meta.Title == "" {
base.Meta.Title = "Untitled"
}
base.Meta.Deleted = updated.Meta.Deleted
base.Content = updated.Content
return base
}
type Tree struct {
root string
}