/
notebook.go
127 lines (104 loc) · 3 KB
/
notebook.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
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
package nbserver
import (
"bytes"
"html/template"
"io/ioutil"
"log"
"net/http"
"path"
"strings"
"github.com/spf13/afero"
)
var defaultNotebookEndpoint = "/nb/"
type notebookHandler struct {
iframeHost string
root http.FileSystem
writeFS *afero.BasePathFs
serveFolder string
}
func (h *notebookHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
// loadTemplates(fs.templates) // TODO: Only do this in dev mode
if r.Method == http.MethodGet {
upath := strings.TrimPrefix(r.URL.Path, defaultNotebookEndpoint)
r.URL.Path = upath
// if !strings.HasPrefix(upath, "/") {
// upath = "/" + upath
// r.URL.Path = upath
// }
upath = path.Clean(upath)
h.serveNotebook(w, r, h.root, upath)
} else if r.Method == http.MethodPut {
upath := strings.TrimPrefix(r.URL.Path, defaultNotebookEndpoint)
upath = path.Clean(upath)
content, err := ioutil.ReadAll(r.Body)
if err != nil {
http.Error(w, "Could not read body, bad request.", http.StatusBadRequest)
return
}
log.Printf("Writing to %s (%v bytes)", upath, len(content))
err = afero.WriteFile(h.writeFS, upath, content, 0644)
if err != nil {
log.Printf("Failed to save notebook to disk: %v", err)
http.Error(w, "Could not save notebook", http.StatusInternalServerError)
return
}
}
}
// name is '/'-separated, not filepath.Separator.
func (h *notebookHandler) serveNotebook(w http.ResponseWriter, r *http.Request, fs http.FileSystem, name string) {
f, err := fs.Open(name)
if err != nil {
msg, code := toHTTPError(err)
http.Error(w, msg, code)
return
}
defer f.Close()
d, err := f.Stat()
if err != nil {
msg, code := toHTTPError(err)
http.Error(w, msg, code)
return
}
if d.IsDir() {
url := r.URL.Path
// redirect if the directory name doesn't end in a slash
if url == "" || url[len(url)-1] != '/' {
localRedirect(w, r, path.Base(url)+"/")
return
}
localRedirect(w, r, path.Join(defaultBrowseEndpoint, url))
return
}
fileContent, err := ioutil.ReadAll(f)
if err != nil {
log.Print("Failed to read file")
msg, code := toHTTPError(err)
http.Error(w, msg, code)
return
}
crumbs := makeBreadCrumbs(r.URL.Path, false)
var b bytes.Buffer
err = editorTemplate.Execute(&b, map[string]interface{}{
"browseEndpoint": defaultBrowseEndpoint,
"notebookEndpoint": defaultNotebookEndpoint,
"path": r.URL.Path,
"breadCrumbs": crumbs,
"iframeHost": h.iframeHost,
"notebookContent": string(fileContent),
})
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "text/html; charset=utf-8")
err = indexTemplate.Execute(w, map[string]interface{}{
"body": template.HTML(b.String()),
})
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
}