-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathviews.go
138 lines (119 loc) · 3.75 KB
/
views.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
package main
import (
_ "embed"
"encoding/json"
"log"
"net/http"
"os"
"path"
"strings"
"text/template"
"github.com/go-errors/errors"
)
//go:embed "index.html"
var indexTemplateString string
var indexTemplate = template.Must(template.New("index").Parse(indexTemplateString))
//go:embed "error.html"
var errorTemplateString string
var errorTemplate = template.Must(template.New("error").Parse(errorTemplateString))
func respondTemplate(w http.ResponseWriter, t *template.Template, data interface{}) error {
return t.Execute(w, data)
}
func respondErrorTemplate(w http.ResponseWriter, err error) {
// TODO: rethink this interface a bit
respondTemplate(w, errorTemplate, err.(*errors.Error).ErrorStack())
}
func gatherTableData(workspaces []Workspace) (td TableData) {
for _, workspace := range workspaces {
numOpen, workingDir := getWorkspaceInfo(workspace.Data.Data)
td = append(td, tableRow{
ID: workspace.Data.Meta.ID,
WorkingDir: workingDir,
NumOpenNb: numOpen,
})
}
return
}
type tableRow struct {
ID *string
WorkingDir string
NumOpenNb int
}
// TableData stores data for the table displayed on the main page
type TableData []tableRow
func mainHandlerWrapper(workspaceDir string, notebookIP string, w http.ResponseWriter, r *http.Request) {
workspaces, err := getWorkspaces(workspaceDir)
if err != nil {
respondErrorTemplate(w, errors.Wrap(err, 0))
return
}
instances, err := getRunningJupyterURLsForCurrentUser(notebookIP)
if err != nil {
respondErrorTemplate(w, errors.Wrap(err, 0))
return
}
respondTemplate(w, indexTemplate, struct {
TableData TableData
Instances map[int32]string
}{TableData: gatherTableData(workspaces), Instances: instances})
}
func getWorkspacesToRemove(formData string) (workspacesToRemove map[string]struct{}) {
// make map w/ empty values, set-like object
workspacesToRemove = make(map[string]struct{})
if formData == "" {
return // TODO: log this?
}
for _, workspace := range strings.Split(formData, ",") {
workspacesToRemove[workspace] = struct{}{}
}
return
}
func removeWorkspaceHandlerWrapper(workspaceDir string, workspaceTrashDir string, w http.ResponseWriter, r *http.Request) {
workspacesToRemove := getWorkspacesToRemove(r.FormValue("workspaces"))
workspaces, err := getWorkspaces(workspaceDir)
if err != nil {
// respondErrorTemplate(w, errors.Wrap(err, 0)) respond json
return
}
err = os.MkdirAll(workspaceTrashDir, os.ModePerm)
if err != nil {
writeErrorJSON(w, err)
return
}
removed := []string{}
for _, workspace := range workspaces {
if _, ok := workspacesToRemove[*workspace.Data.Meta.ID]; ok {
workspaceTrashPath := path.Join(workspaceTrashDir, workspace.FileInfo.Name())
log.Printf("moving %s => %s\n", workspace.Path, workspaceTrashPath)
err = os.Rename(workspace.Path, workspaceTrashPath)
if err != nil {
writeErrorJSON(w, err)
}
removed = append(removed, *workspace.Data.Meta.ID)
}
}
respondJSON(w, apiResponse{Result: removed})
}
type apiResponse struct {
Result interface{} `json:"result"`
Error *string `json:"error"`
}
// Write json payload to response writer, setting the proper header
func writeJSON(w http.ResponseWriter, payload interface{}) error {
w.Header().Set("Content-Type", "application/json")
return json.NewEncoder(w).Encode(payload)
}
// Write json error payload to response writer
func writeErrorJSON(w http.ResponseWriter, err error) {
response := apiResponse{}
errMsg := err.Error()
response.Error = &errMsg
writeJSON(w, response)
}
// Create json response, where an error is written if there is trouble
// serializing the json response. Otherwise, write the json response
func respondJSON(w http.ResponseWriter, payload interface{}) {
if err := writeJSON(w, payload); err != nil {
writeErrorJSON(w, err)
}
}