move leaf to own file
parent
ced507ca68
commit
2bec0dd1b6
|
|
@ -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
|
||||
}
|
||||
|
|
@ -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
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue