-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathhandlers_api.go
47 lines (41 loc) · 938 Bytes
/
handlers_api.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
package main
import (
"encoding/json"
"log"
"net/http"
)
type Lister interface {
ListPages() ([]string, error)
ListFiles() ([]File, error)
}
func ApiListHandler(l Lister) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
var res []string
if r.FormValue("type") == "file" {
files, err := l.ListFiles()
if err != nil {
log.Printf("Failed to list files: %v\n", err)
w.WriteHeader(http.StatusInternalServerError)
return
}
for i := range files {
res = append(res, files[i].Name)
}
} else {
pages, err := l.ListPages()
if err != nil {
log.Printf("Failed to list pages: %v\n", err)
w.WriteHeader(http.StatusInternalServerError)
return
}
res = pages
}
b, err := json.Marshal(res)
if err != nil {
log.Printf("Failed to marshal list contents: %v\n", err)
w.WriteHeader(http.StatusInternalServerError)
return
}
_, _ = w.Write(b)
}
}