-
Notifications
You must be signed in to change notification settings - Fork 2
/
norms.go
114 lines (99 loc) · 2.75 KB
/
norms.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
package jitter
import (
"encoding/json"
"errors"
"fmt"
"log"
"net/http"
"strconv"
"gogs.bellstone.ca/james/jitter/lib/data"
"gogs.bellstone.ca/james/jitter/lib/mef"
"gogs.bellstone.ca/james/jitter/lib/mem"
)
type filterParameters struct {
sex mem.Sex
minAge int
maxAge int
country string
species string
nerve string
}
func NormHandler(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Access-Control-Allow-Origin", "*")
mefData, err := data.AsMef()
if err != nil {
setError(w, "Error loading MEF because "+err.Error())
return
}
jsNorm, err := getFilteredNormsFromRequest(r, mefData)
if err != nil {
setError(w, "Error getting filtered norms due to "+err.Error())
return
}
jsNormArray, err := json.Marshal(&jsNorm)
if err != nil {
setError(w, "Could not create norm JSON due to "+err.Error())
return
}
log.Println("Served norms")
fmt.Fprintln(w, string(jsNormArray))
}
func setError(w http.ResponseWriter, str string) {
w.WriteHeader(http.StatusInternalServerError)
log.Println(str)
output := struct {
String string `json:"error"`
}{str}
js, err := json.Marshal(&output)
if err != nil {
log.Println("Could not marshal error " + err.Error())
fmt.Fprintln(w, str)
} else {
fmt.Fprintln(w, string(js))
}
}
func parseSex(sex string) (mem.Sex, error) {
switch sex {
case "male", "Male", "M", "m":
return mem.MaleSex, nil
case "female", "Female", "F", "f":
return mem.MaleSex, nil
case "":
return mem.UnknownSex, nil
default:
return mem.UnknownSex, errors.New("Invalid sex '" + sex + "'")
}
}
func parseQuery(r *http.Request) (filterParameters, error) {
fp := filterParameters{
country: r.FormValue("country"),
species: "human", // for now, rat doesn't work
nerve: r.FormValue("nerve"),
}
var err error
fp.sex, err = parseSex(r.FormValue("sex"))
if err != nil {
return fp, errors.New("Error parsing sex: " + err.Error())
}
fp.minAge, err = strconv.Atoi(r.FormValue("minAge"))
if r.FormValue("minAge") != "" && err != nil {
return fp, errors.New("Could not parse minAge due to error: " + err.Error())
}
fp.maxAge, err = strconv.Atoi(r.FormValue("maxAge"))
if r.FormValue("maxAge") != "" && err != nil {
return fp, errors.New("Could not parse maxAge due to error: " + err.Error())
}
return fp, nil
}
func getFilteredNormsFromRequest(r *http.Request, mefData mef.Mef) (mef.Norm, error) {
err := r.ParseForm()
if err != nil {
return mef.Norm{}, errors.New("error parsing form because " + err.Error())
}
fp, err := parseQuery(r)
if err != nil {
return mef.Norm{}, errors.New("error parsing query because " + err.Error())
}
mefData.Filter(mef.NewFilter().BySex(fp.sex).ByAge(fp.minAge, fp.maxAge).ByCountry(fp.country).BySpecies(fp.species).ByNerve(fp.nerve))
return mefData.Norm(), nil
}