This repository has been archived by the owner on Sep 2, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 67
/
form.go
68 lines (53 loc) · 1.55 KB
/
form.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
package staticbackend
import (
"net/http"
"strings"
"github.com/staticbackendhq/core/backend"
"github.com/staticbackendhq/core/middleware"
)
func submitForm(w http.ResponseWriter, r *http.Request) {
conf, _, err := middleware.Extract(r, false)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
form := getURLPart(r.URL.Path, 2)
doc := make(map[string]interface{})
//TODO: Why forms would need multiplart/form-data
// There's no file upload available via form
/*if err := r.ParseMultipartForm(32 << 20); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}*/
if err := r.ParseForm(); err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
// if there's something in the _hp_ field, it's a bot
if len(r.Form.Get("_hp_")) > 0 {
http.Error(w, "invalid form field present", http.StatusBadRequest)
return
}
for k, v := range r.Form {
doc[k] = strings.Join(v, ", ")
}
if err := backend.DB.AddFormSubmission(conf.Name, form, doc); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
respond(w, http.StatusOK, true)
}
func listForm(w http.ResponseWriter, r *http.Request) {
conf, _, err := middleware.Extract(r, true)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
formName := r.URL.Query().Get("name")
results, err := backend.DB.ListFormSubmissions(conf.Name, formName)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
respond(w, http.StatusOK, results)
}