-
Notifications
You must be signed in to change notification settings - Fork 1
/
router.go
72 lines (58 loc) · 1.35 KB
/
router.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
package main
import (
"net/http"
"regexp"
"strings"
)
// router handler GET: /
func router(w http.ResponseWriter, r *http.Request) {
path := r.URL.Path
if regexp.MustCompile(`(?i)([a-zA-Z]+)`).MatchString(path) {
path := strings.ToLower(path)
// get all currencies
if path == "/v1" || path == "/v1/" {
curs, err := getAll()
if err != nil {
resp := newResponseError(err.Error(), nil)
rJSON(w, http.StatusServiceUnavailable, resp)
return
}
rJSON(w, http.StatusOK, newResponseOK("", curs))
return
}
if path == "/v1/euro" || path == "/v1/euro/" {
getOne(w, 0)
return
}
if path == "/v1/yuan" || path == "/v1/yuan/" {
getOne(w, 1)
return
}
if path == "/v1/lira" || path == "/v1/lira/" {
getOne(w, 2)
return
}
if path == "/v1/ruble" || path == "/v1/ruble/" {
getOne(w, 3)
return
}
if path == "/v1/dollar" || path == "/v1/dollar/" {
getOne(w, 4)
return
}
resp := newResponseError("path error", nil)
rJSON(w, http.StatusNotFound, resp)
return
}
resp := newResponseError("path error", nil)
rJSON(w, http.StatusNotFound, resp)
}
func getOne(w http.ResponseWriter, key int) {
cur, err := getUnique(key)
if err != nil {
resp := newResponseError(err.Error(), nil)
rJSON(w, http.StatusServiceUnavailable, resp)
return
}
rJSON(w, http.StatusOK, newResponseOK("", cur))
}