-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathroutes.go
114 lines (95 loc) · 3.02 KB
/
routes.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 main
import (
"net/http"
"strconv"
"time"
)
func getHome(response http.ResponseWriter, request *http.Request) {
response.Header().Set("Content-Type", "application/json")
response.Write([]byte(`{ "message": "Welcome! To use this system please query /ip/$ip" }`))
}
func getIp(response http.ResponseWriter, request *http.Request) {
if !validApiKey(request, false) {
response.Header().Set("Content-Type", "application/json")
response.Write([]byte(`{ "error": "Sorry, this API requires a key" }`))
return
}
ipString := request.PathValue("ip")
jsonBytes, err := fetchIPJson(ipString)
if err != nil {
response.Header().Set("Content-Type", "application/json")
response.Write([]byte(`{ "error": "` + err.Error() + `" }`))
return
}
response.Header().Set("Content-Type", "application/json")
response.Write(jsonBytes)
}
func getRandomIp(response http.ResponseWriter, request *http.Request) {
if !validApiKey(request, true) {
response.Header().Set("Content-Type", "application/json")
response.Write([]byte(`{ "error": "Sorry, this API requires a key" }`))
return
}
var ipString string
ipVersion := request.PathValue("ipVersion")
if ipVersion == "4" {
ipString = randomIpv4()
} else {
ipString = randomIpv6();
}
jsonBytes, err := fetchIPJson(ipString)
if err != nil {
response.Header().Set("Content-Type", "application/json")
response.Write([]byte(`{ "error": "` + err.Error() + `" }`))
return
}
response.Header().Set("Content-Type", "application/json")
response.Write(jsonBytes)
}
func getBenchmark(response http.ResponseWriter, request *http.Request) {
if !validApiKey(request, true) {
response.Header().Set("Content-Type", "application/json")
response.Write([]byte(`{ "error": "Sorry, this API requires a key" }`))
return
}
var ipString string
ipVersion := request.PathValue("ipVersion")
times := request.PathValue("times")
timesInt, err := strconv.Atoi(times)
if err != nil {
response.Header().Set("Content-Type", "application/json")
response.Write([]byte(`{ "error": "URL must contain a numeric number of times to run" }`))
return
}
// Don't want this to be part of the benchmark
var testIps []string
for i := 0; i < timesInt; i++ {
if ipVersion == "4" {
ipString = randomIpv4()
} else {
ipString = randomIpv6();
}
testIps = append(testIps, ipString)
}
start := time.Now()
for _, ipString := range testIps {
_, err := fetchIP(ipString)
if err != nil {
response.Header().Set("Content-Type", "application/json")
response.Write([]byte(`{ "error": "Error encountered during run (` + ipString + `)" }`))
return
}
}
ms := int(time.Now().Sub(start).Milliseconds())
us := int(time.Now().Sub(start).Microseconds())
msPerOp := ms / timesInt;
usPerOp := us / timesInt;
response.Header().Set("Content-Type", "application/json")
response.Write([]byte(`{
"times": ` + strconv.Itoa(timesInt) + `,
"ms": ` + strconv.Itoa(ms) + `,
"μs": ` + strconv.Itoa(us) + `,
"ms_per_op": ` + strconv.Itoa(msPerOp) + `,
"μs_per_op": ` + strconv.Itoa(usPerOp) + `
}`))
}