forked from drkelf/rell
/
examples.go
233 lines (214 loc) · 5.46 KB
/
examples.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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
// Package examples provides stock and stored examples.
package examples
import (
"bytes"
"crypto/md5"
"fmt"
"github.com/daaku/go.errcode"
"github.com/daaku/go.flag.pkgpath"
"io/ioutil"
"log"
"net/http"
"path"
"path/filepath"
"strings"
)
// Some categories are hidden from the listing.
var hidden = map[string]bool{
"bugs": true,
"fbml": true,
"hidden": true,
"secret": true,
"tests": true,
}
type ByteStore interface {
Store(key string, value []byte) error
Get(key string) ([]byte, error)
}
type Store struct {
ByteStore ByteStore
}
type Example struct {
Name string `json:"-"`
Content []byte `json:"-"`
AutoRun bool `json:"autoRun"`
Title string `json:"-"`
URL string `json:"-"`
}
type Category struct {
Name string
Example []*Example
Hidden bool
}
type DB struct {
Category []*Category
Reverse map[string]*Example
}
var (
// Directory for disk backed DBs.
oldExamplesDir = pkgpath.Dir(
"rell.examples.old",
"github.com/daaku/rell/examples/db/old",
"The directory containing examples for the old SDK.")
newExamplesDir = pkgpath.Dir(
"rell.examples.new",
"github.com/daaku/rell/examples/db/mu",
"The directory containing examples for the new SDK.")
// We have two disk backed DBs.
old *DB
mu *DB
// Stock response for the index page.
emptyExample = &Example{Title: "Welcome", URL: "/", AutoRun: true}
)
// Loads a specific examples directory.
func loadDir(name string) (*DB, error) {
categories, err := ioutil.ReadDir(name)
if err != nil {
return nil, fmt.Errorf("Failed to read directory %s: %s", name, err)
}
db := &DB{
Category: make([]*Category, 0, len(categories)),
Reverse: make(map[string]*Example),
}
db.Reverse[ContentID(emptyExample.Content)] = emptyExample
for _, categoryFileInfo := range categories {
categoryName := categoryFileInfo.Name()
if !categoryFileInfo.IsDir() {
log.Printf(
"Got unexpected file instead of directory for category: %s",
categoryName)
continue
}
category := &Category{
Name: categoryName,
Hidden: hidden[categoryName],
}
categoryDir := filepath.Join(name, categoryName)
examples, err := ioutil.ReadDir(categoryDir)
if err != nil {
return nil, fmt.Errorf("Failed to read category %s: %s", categoryDir, err)
}
category.Example = make([]*Example, 0, len(examples))
for _, exampleFileInfo := range examples {
exampleName := exampleFileInfo.Name()
exampleFile := filepath.Join(categoryDir, exampleName)
content, err := ioutil.ReadFile(exampleFile)
if err != nil {
return nil, fmt.Errorf(
"Failed to read example %s: %s", exampleFile, err)
}
cleanName := exampleName[:len(exampleName)-5]
example := &Example{
Name: cleanName,
Content: content,
AutoRun: true,
Title: categoryName + " · " + cleanName,
URL: path.Join("/", categoryName, cleanName),
}
category.Example = append(category.Example, example)
db.Reverse[ContentID(bytes.TrimSpace(content))] = example
}
db.Category = append(db.Category, category)
}
return db, nil
}
// Load an Example for a given version and path.
func (s *Store) Load(version, path string) (*Example, error) {
parts := strings.Split(path, "/")
if len(parts) == 2 && parts[1] == "" {
return emptyExample, nil
} else if len(parts) == 4 {
if parts[1] != "raw" && parts[1] != "simple" {
return nil, errcode.New(http.StatusNotFound, "Invalid URL: %s", path)
}
parts = []string{"", parts[2], parts[3]}
} else if len(parts) != 3 {
return nil, errcode.New(http.StatusNotFound, "Invalid URL: %s", path)
}
if parts[1] == "saved" {
content, err := s.ByteStore.Get(makeKey(parts[2]))
if err != nil {
return nil, err
}
if content == nil {
return nil, errcode.New(
http.StatusNotFound, "Example not found: %s", path)
}
return &Example{
Content: content,
Title: "Stored Example",
URL: path,
}, nil
}
category := GetDB(version).FindCategory(parts[1])
if category == nil {
return nil, errcode.New(http.StatusNotFound, "Could not find category: %s", parts[1])
}
example := category.FindExample(parts[2])
if example == nil {
return nil, errcode.New(http.StatusNotFound, "Could not find example: %s", parts[2])
}
return example, nil
}
// Get the DB for a given SDK Version.
func GetDB(version string) *DB {
var err error
if version == "mu" {
if mu == nil {
mu, err = loadDir(*newExamplesDir)
if err != nil {
log.Fatal(err)
}
}
return mu
}
if old == nil {
old, err = loadDir(*oldExamplesDir)
if err != nil {
log.Fatal(err)
}
}
return old
}
// Find a category by it's name.
func (d *DB) FindCategory(name string) *Category {
for _, category := range d.Category {
if category.Name == name {
return category
}
}
return nil
}
// Find an example by it's name.
func (c *Category) FindExample(name string) *Example {
for _, example := range c.Example {
if example.Name == name {
return example
}
}
return nil
}
// Save an Example.
func (s *Store) Save(id string, content []byte) error {
if len(content) > 10240 {
return errcode.New(
http.StatusRequestEntityTooLarge,
"Maximum allowed size is 10 kilobytes.")
}
err := s.ByteStore.Store(makeKey(id), content)
if err != nil {
log.Printf("Error in ByteStore.Store: %s", err)
}
return err
}
func makeKey(id string) string {
return "fbrell_examples:" + id
}
func ContentID(content []byte) string {
h := md5.New()
_, err := h.Write(content)
if err != nil {
log.Fatalf("Error comupting md5 sum: %s", err)
}
return fmt.Sprintf("%x", h.Sum(nil))
}