-
Notifications
You must be signed in to change notification settings - Fork 44
/
prefs.go
168 lines (132 loc) · 3.31 KB
/
prefs.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
// Copyright 2013 Chris McGee <sirnewton_01@yahoo.ca>. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package main
import (
"encoding/json"
"go/build"
"net/http"
"os"
"path/filepath"
"strings"
)
func prefsHandler(writer http.ResponseWriter, req *http.Request, path string, pathSegs []string) bool {
switch {
case req.Method == "PUT":
gopaths := filepath.SplitList(build.Default.GOPATH)
prefFile := gopaths[len(gopaths)-1] + "/prefs.txt"
var prefs map[string]map[string]string
_, err := os.Stat(prefFile)
if err == nil {
file, err := os.Open(prefFile)
if err != nil {
ShowError(writer, 500, "Could not open preferences file", err)
return true
}
dec := json.NewDecoder(file)
err = dec.Decode(&prefs)
file.Close()
if err != nil {
ShowError(writer, 500, "Corrupt preferences file", err)
return true
}
} else {
prefs = make(map[string]map[string]string)
}
var prefNode map[string]string
prefNode, _ = prefs[path]
if strings.HasPrefix(req.Header.Get("Content-Type"), "application/json") {
prefNode = make(map[string]string)
dec := json.NewDecoder(req.Body)
err = dec.Decode(&prefNode)
if err != nil {
ShowError(writer, 400, "Could not parse JSON input", err)
return true
}
} else {
if prefNode == nil {
prefNode = make(map[string]string)
}
err := req.ParseForm()
if err != nil {
ShowError(writer, 400, "Could not parse form input", err)
return true
}
form := req.Form
keyValue, keyValueOk := form["key"]
if !keyValueOk {
writer.WriteHeader(400)
return true
}
key := keyValue[0]
valueValue, valueValueOk := form["value"]
if !valueValueOk {
writer.WriteHeader(400)
return true
}
value := valueValue[0]
prefNode[key] = value
}
prefs[path] = prefNode
file, err := os.Create(prefFile)
if err != nil {
ShowError(writer, 500, "Could not open preferences file", err)
return true
}
enc := json.NewEncoder(file)
enc.Encode(&prefs)
file.Close()
writer.WriteHeader(204)
return true
case req.Method == "DELETE":
gopaths := filepath.SplitList(build.Default.GOPATH)
prefFile := gopaths[len(gopaths)-1] + "/prefs.txt"
var prefs map[string]map[string]string
_, err := os.Stat(prefFile)
if err == nil {
file, err := os.Open(prefFile)
if err != nil {
ShowError(writer, 500, "Could not open preferences file", err)
return true
}
dec := json.NewDecoder(file)
err = dec.Decode(&prefs)
file.Close()
if err != nil {
ShowError(writer, 500, "Corrupt preference file", err)
return true
}
} else {
writer.WriteHeader(204)
return true
}
prefNode, _ := prefs[path]
if prefNode == nil {
writer.WriteHeader(204)
return true
}
if req.URL.RawQuery == "" {
delete(prefs, path)
} else {
keyValue, queryOk := req.URL.Query()["key"]
if !queryOk {
writer.WriteHeader(204)
return true
}
key := keyValue[0]
delete(prefNode, key)
prefs[path] = prefNode
}
file, err := os.Create(prefFile)
if err != nil {
ShowError(writer, 500, "Could not open preferences file", err)
return true
}
enc := json.NewEncoder(file)
enc.Encode(&prefs)
file.Close()
writer.WriteHeader(204)
return true
}
return false
}