forked from rohanthewiz/go_notes
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwebserver.go
More file actions
256 lines (221 loc) · 6.97 KB
/
Copy pathwebserver.go
File metadata and controls
256 lines (221 loc) · 6.97 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
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
package main
import (
"encoding/json"
"errors"
"fmt"
"go_notes/note"
"go_notes/note/web"
"io/ioutil"
"log"
"net/http"
"net/url"
"path"
"strconv"
"github.com/julienschmidt/httprouter"
)
func webserver(port string) {
router := httprouter.New()
doRoutes(router)
pf("Web server listening on %s... Ctrl-C to quit\n", port)
log.Fatal(http.ListenAndServe(":"+port, router))
}
// Handlers for httprouter
func Index(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
http.Redirect(w, r, "/q/all/l/100", http.StatusFound)
}
func webListNotes(w http.ResponseWriter, r *http.Request) {
notes := queryNotes()
err := web.NotesList(w, r, notes, optsStr)
if err != nil {
log.Println("Error in notes list html gen:", err)
}
}
func Query(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
resetOptions()
optsStr["q"] = p.ByName("query") // Overwrite the query param
limit, err := strconv.Atoi(p.ByName("limit"))
if err == nil {
optsIntf["l"] = limit
}
webListNotes(w, r)
}
func QueryLast(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
resetOptions()
optsIntf["ql"] = true
webListNotes(w, r)
}
func QueryId(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
resetOptions()
id, err := strconv.ParseInt(p.ByName("id"), 10, 64)
if err != nil {
id = 0
}
optsIntf["qi"] = id // qi is the highest priority
webListNotes(w, r)
}
func QueryIdAsJson(w http.ResponseWriter, _ *http.Request, p httprouter.Params) {
resetOptions()
id, err := strconv.ParseInt(p.ByName("id"), 10, 64)
if err != nil {
id = 0
}
optsIntf["qi"] = id // qi is the highest priority
jNotes, err := json.Marshal(queryNotes())
if err != nil {
log.Println("Error marshalling Note id:", strconv.FormatInt(id, 10))
}
w.Header().Set("Content-Type", "application/json")
_, err = w.Write(jNotes)
if err != nil {
log.Println("Error in notes JSON gen:", err)
}
}
func QueryTag(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
resetOptions()
optsStr["qg"] = p.ByName("tag") // Overwrite the query param
optsIntf["qi"] = nil // turn off unused option
optsStr["qt"] = "" // turn off unused option
optsStr["q"] = "" // turn off unused option
webListNotes(w, r)
}
func QueryTitle(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
resetOptions()
optsStr["qt"] = p.ByName("title") // Overwrite the query param
optsIntf["qi"] = nil // turn off unused option
optsStr["qg"] = "" // turn off unused option
webListNotes(w, r)
}
func QueryTagAndWildCard(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
resetOptions()
optsStr["qg"] = p.ByName("tag") // Overwrite the query param
optsStr["q"] = p.ByName("query") // Overwrite the query param
webListNotes(w, r)
}
func QueryTitleAndWildCard(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
resetOptions()
optsStr["qt"] = p.ByName("title") // Overwrite the query param
optsStr["q"] = p.ByName("query") // Overwrite the query param
webListNotes(w, r)
}
func WebNoteForm(w http.ResponseWriter, _ *http.Request, p httprouter.Params) {
if id, err := strconv.ParseInt(p.ByName("id"), 10, 64); err == nil {
var nte note.Note
db.Where("id = ?", id).First(&nte) // get the original for comparision
fmt.Printf("note at WebNoteForm %#v\n", nte.Guid)
if nte.Id > 0 {
err = web.NoteForm(w, nte)
if err != nil {
log.Println("Error in Render NoteForm:", err)
}
} else {
err := web.NoteForm(w, note.Note{})
if err != nil {
log.Println("Error in Render NoteForm:", err)
}
}
} else {
err := web.NoteForm(w, note.Note{})
if err != nil {
log.Println("Error in Render NoteForm:", err)
}
}
}
func WebCreateNote(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
postData, err := ioutil.ReadAll(r.Body)
if err != nil {
HandleRequestErr(err, w)
return
}
v, err := url.ParseQuery(string(postData))
if err != nil {
HandleRequestErr(err, w)
return
}
nb := trimWhitespace(v.Get("note_body"))
nb = note.UpsertKeyNotes(nb) // prepend KeyNotes - hardwired ON for now
tl := trimWhitespace(v.Get("title"))
if tl == "" {
HandleRequestErr(errors.New("title should not be empty"), w)
return
}
id := CreateNote(tl, trimWhitespace(v.Get("descr")),
nb, trimWhitespace(v.Get("tag")))
http.Redirect(w, r, "/qi/"+strconv.FormatUint(id, 10), http.StatusFound)
}
func WebNoteDup(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
origId, err := strconv.ParseInt(p.ByName("id"), 10, 64)
if err != nil {
HandleRequestErr(err, w)
return
}
nte := findNoteById(origId)
if nte.Id < 1 {
http.Redirect(w, r, "/q/all/l/100", http.StatusFound)
}
// TODO - Check that note with title below does not already exist
// and gracefully handle error
id := CreateNote("Copy of - "+nte.Title, "",
"", nte.Tag)
if id > 0 {
http.Redirect(w, r, "/edit/"+strconv.FormatUint(id, 10), http.StatusFound)
} else {
http.Redirect(w, r, "/q/all/l/100", http.StatusFound)
}
}
func WebDeleteNote(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
returnPath := "/q/all/l/100"
id, err := strconv.ParseInt(p.ByName("id"), 10, 64)
if err != nil {
fmt.Println("Error parsing id when deleting note.")
http.Redirect(w, r, returnPath, http.StatusFound)
return
}
DoDelete(findNoteById(id))
qs := r.URL.Query()
if qs != nil {
returnPath = qs.Get("return")
}
http.Redirect(w, r, returnPath, http.StatusFound)
}
func WebUpdateNote(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
var nte note.Note
if id, err := strconv.ParseUint(p.ByName("id"), 10, 64); err == nil {
post_data, err := ioutil.ReadAll(r.Body)
if err != nil {
HandleRequestErr(err, w)
return
}
v, err := url.ParseQuery(string(post_data))
if err != nil {
HandleRequestErr(err, w)
return
}
nb := trimWhitespace(v.Get("note_body"))
nb = note.UpsertKeyNotes(nb) // prepend KeyNotes - hardwired ON for now
nte = note.Note{Id: id, Title: trimWhitespace(v.Get("title")),
Description: trimWhitespace(v.Get("descr")),
Body: nb, Tag: trimWhitespace(v.Get("tag")),
}
pf("Updating note: %s %s...\n", nte.Guid, nte.Title)
AllFieldsUpdate(nte)
http.Redirect(w, r, "/qi/"+strconv.FormatUint(nte.Id, 10), http.StatusFound)
}
}
func ServeJS(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
http.ServeFile(w, r, path.Join("js", p.ByName("file")))
}
func resetOptions() {
optsIntf["qi"] = nil // turn off unused option
optsIntf["ql"] = false // turn off unused option
optsIntf["l"] = -1 // turn off unused option
optsStr["qg"] = "" // turn off higher priority option
optsStr["qt"] = "" // turn off unused option
optsStr["qd"] = "" // turn off unused option
optsStr["qb"] = "" // turn off unused option
optsStr["q"] = "" // turn off higher priority option
}
func HandleRequestErr(err error, w http.ResponseWriter) {
w.WriteHeader(http.StatusBadRequest)
_, _ = fmt.Fprint(w, err)
}
// Only applies to GET request? //pl("p.Title", p.ByName("title"))