aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorChristine Dodrill <me@christine.website>2016-12-14 11:53:26 -0800
committerChristine Dodrill <me@christine.website>2016-12-14 11:53:26 -0800
commit766c6cd3d0c0583f66bdc22a8ea12265dd4505ba (patch)
treeaf7d23d3a743c9fd12d25e0606c3ecfdcadc5f3c
parentb65845c8ac8d2a62f10bc5c577b1f70b4b4bfcf3 (diff)
downloadxesite-766c6cd3d0c0583f66bdc22a8ea12265dd4505ba.tar.xz
xesite-766c6cd3d0c0583f66bdc22a8ea12265dd4505ba.zip
backend: return individual posts as json
-rw-r--r--backend/christine.website/main.go42
1 files changed, 26 insertions, 16 deletions
diff --git a/backend/christine.website/main.go b/backend/christine.website/main.go
index 83dc47a..3100f0c 100644
--- a/backend/christine.website/main.go
+++ b/backend/christine.website/main.go
@@ -1,12 +1,12 @@
package main
import (
+ "bytes"
"encoding/json"
- "fmt"
+ "io/ioutil"
"log"
"net/http"
"os"
- "path"
"path/filepath"
"sort"
"strings"
@@ -19,7 +19,8 @@ import (
type Post struct {
Title string `json:"title"`
Link string `json:"link"`
- Summary string `json:"summary"`
+ Summary string `json:"summary,omitifempty"`
+ Body string `json:"body, omitifempty"`
Date string `json:"date"`
}
@@ -53,17 +54,27 @@ func init() {
}
defer fin.Close()
+ content, err := ioutil.ReadAll(fin)
+ if err != nil {
+ // handle error
+ }
+
m := front.NewMatter()
m.Handle("---", front.YAMLHandler)
- front, _, err := m.Parse(fin)
+ front, _, err := m.Parse(bytes.NewReader(content))
if err != nil {
return err
}
+ sp := strings.Split(string(content), "\n")
+ sp = sp[4:]
+ data := strings.Join(sp, "\n")
+
p := &Post{
Title: front["title"].(string),
Date: front["date"].(string),
Link: strings.Split(path, ".")[0],
+ Body: data,
}
posts = append(posts, p)
@@ -80,24 +91,23 @@ func init() {
func main() {
http.HandleFunc("/api/blog/posts", writeBlogPosts)
- http.HandleFunc("/api/blog/name", func(w http.ResponseWriter, r *http.Request) {
+ http.HandleFunc("/api/blog/post", func(w http.ResponseWriter, r *http.Request) {
q := r.URL.Query()
name := q.Get("name")
- fin, err := os.Open(path.Join("./blog", name))
- if err != nil {
- http.Error(w, err.Error(), http.StatusInternalServerError)
- return
+ if name == "" {
+ goto fail
}
- defer fin.Close()
- m := front.NewMatter()
- m.Handle("---", front.YAMLHandler)
- _, body, err := m.Parse(fin)
- if err != nil {
- http.Error(w, err.Error(), http.StatusInternalServerError)
+ for _, p := range posts {
+ if strings.HasSuffix(p.Link, name) {
+ json.NewEncoder(w).Encode(p)
+ return
+ }
}
- fmt.Fprintln(w, body)
+
+ fail:
+ http.Error(w, "Not Found", http.StatusNotFound)
})
http.Handle("/dist/", http.FileServer(http.Dir("./frontend/static/")))
http.HandleFunc("/", writeIndexHTML)