-
Notifications
You must be signed in to change notification settings - Fork 4
/
tiddlyweb.go
129 lines (108 loc) · 2.79 KB
/
tiddlyweb.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
package main
import (
"bytes"
"crypto/md5"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"log"
"net/http"
"net/url"
"strings"
)
func index(basepath string) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
switch r.Method {
case "GET":
http.ServeFile(w, r, "index.html")
case "OPTIONS":
w.WriteHeader(http.StatusNoContent)
}
}
}
func status(basepath string) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
jsonResponse(w).Write([]byte(`{"username":"me","space":{"recipe":"all"}}`))
}
}
func list(basepath string) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
list := repo.List()
var buff bytes.Buffer
buff.WriteString("[")
for i, tiddler := range list {
if i != 0 {
buff.WriteString(",")
}
buff.Write(tiddler.Meta)
}
buff.WriteString("]")
jsonResponse(w).Write(buff.Bytes())
}
}
func detail(basepath string) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
title := strings.TrimPrefix(r.URL.Path, basepath)
switch r.Method {
case "GET":
tiddler, ok := repo.Get(title)
if !ok {
log.Printf("Tiddler not found: '%v'", title)
http.Error(w, "Not found", http.StatusNotFound)
return
}
var js map[string]interface{}
err := json.Unmarshal(tiddler.Meta, &js)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
if tiddler.Text != "" {
js["text"] = tiddler.Text
}
json.NewEncoder(w).Encode(js)
case "PUT":
var tiddler Tiddler
var js map[string]interface{}
err := json.NewDecoder(r.Body).Decode(&js)
if err != nil {
log.Printf("Error decoding tiddler: '%v'", title)
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
io.Copy(ioutil.Discard, r.Body)
js["bag"] = "bag"
text, _ := js["text"].(string)
delete(js, "text")
meta, err := json.Marshal(js)
if err != nil {
log.Printf("Error marshalling tiddler meta: '%v'", js)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
tiddler.Text = text
tiddler.Title = title
tiddler.Meta = meta
//create the tiddler
rev, err := repo.Put(tiddler)
if err != nil {
log.Printf("Error saving tiddler '%v'", title)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
etag := fmt.Sprintf(`"bag/%s/%d:%032x"`, url.QueryEscape(title), rev, md5.Sum(meta))
w.Header().Set("ETag", etag)
w.WriteHeader(http.StatusNoContent)
}
}
}
func remove(basepath string) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
title := strings.TrimPrefix(r.URL.Path, basepath)
if err := repo.Remove(title); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
w.WriteHeader(http.StatusNoContent)
}
}