-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
61 lines (51 loc) · 1.39 KB
/
config.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
package main
import (
"fmt"
"net/http"
"strconv"
"github.com/gophers-frm/go-funk-simplified/renderer"
"github.com/gophers-frm/go-funk-simplified/sampler"
)
type RequestData struct {
Conf renderer.Config
Sampler sampler.SamplerFunc
}
func parseInt(str string, def int, prevErr error) (i int, err error) {
if prevErr != nil {
return i, prevErr
}
if str == "" {
return def, nil
}
return strconv.Atoi(str)
}
func parseRequest(req *http.Request, defaults *RequestData) (*RequestData, error) {
var (
d RequestData
err error
)
query := req.URL.Query()
if colorKey := query.Get("colors"); colorKey == "" {
d.Conf.Colorer = defaults.Conf.Colorer
} else {
if colorFunc, ok := renderer.Colors[colorKey]; !ok {
return nil, fmt.Errorf("could not find color method with key %s", colorKey)
} else {
d.Conf.Colorer = colorFunc
}
}
if samplingKey := req.URL.Query().Get("sampling"); samplingKey == "" {
d.Sampler = defaults.Sampler
} else {
samplingFunc, ok := sampler.Samplers[samplingKey]
if !ok {
return nil, fmt.Errorf("could not find sampling method with key %s", samplingKey)
} else {
d.Sampler = samplingFunc
}
}
d.Conf.Width, err = parseInt(query.Get("width"), defaults.Conf.Width, nil)
d.Conf.Height, err = parseInt(query.Get("height"), defaults.Conf.Height, err)
d.Conf.Count, err = parseInt(query.Get("count"), defaults.Conf.Count, err)
return &d, err
}