-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdocument.go
More file actions
63 lines (51 loc) · 1.29 KB
/
document.go
File metadata and controls
63 lines (51 loc) · 1.29 KB
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
package main
import (
"log"
"path/filepath"
"net/http"
"encoding/json"
"strings"
"os"
)
type Response struct {
Code int
Description string
}
func walkpath(path string, f os.FileInfo, err error) error {
log.Printf("%s with %d bytes\n", path,f.Size())
return nil
}
func newDocument(w http.ResponseWriter, r *http.Request) {
filename := r.URL.Query().Get("name")
//create a new document
f, err := os.Create(location + "/" + filename + ".md")
check(err)
defer f.Close()
log.Println("document created: " + filename)
var response Response
response.Code = 200
response.Description = "document created: " + filename
js, err := json.Marshal(response)
check(err)
w.Header().Set("Content-Type", "application/json")
w.Write(js)
}
func listDocuments(w http.ResponseWriter, r *http.Request) {
//list all the documents and return them as json
var fileArray []string
filepath.Walk(location, func(path string, f os.FileInfo, err error) error {
if f.Mode().IsDir() && f.Name() == ".git" {
return filepath.SkipDir
} else {
filename := strings.Replace(path, location, "", -1)
if filename != "" {
fileArray = append(fileArray, filename)
}
}
return nil
});
js, err := json.Marshal(fileArray)
check(err)
w.Header().Set("Content-Type", "application/json")
w.Write(js)
}