-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.go
74 lines (61 loc) · 1.44 KB
/
main.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
package main
import (
"encoding/json"
"log"
"net/http"
_ "net/http/pprof"
"strconv"
"github.com/rafaelhl/profiling-samples/samples"
)
func main() {
http.HandleFunc("/json/unmarshall", handleJson)
http.HandleFunc("/timezone", handleTimezone)
http.HandleFunc("/aloc", handleAloc)
log.Fatal(http.ListenAndServe(":7777", nil))
}
func timesParam(r *http.Request, w http.ResponseWriter) (int, bool) {
timesParam := r.URL.Query().Get("times")
times, err := strconv.Atoi(timesParam)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte("unexpected times value"))
return 0, false
}
return times, true
}
func handleJson(w http.ResponseWriter, r *http.Request) {
times, ok := timesParam(r, w)
if !ok {
return
}
result := samples.Unmarshall[map[string]any](times)
body, err := json.Marshal(result)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte("unexpected error"))
return
}
w.Write(body)
}
func handleTimezone(w http.ResponseWriter, r *http.Request) {
times, ok := timesParam(r, w)
if !ok {
return
}
result := samples.Timezone(times)
w.Write([]byte(result.String()))
}
func handleAloc(w http.ResponseWriter, r *http.Request) {
times, ok := timesParam(r, w)
if !ok {
return
}
result := samples.Aloc("test", times)
body, err := json.Marshal(result)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte("unexpected times value"))
return
}
w.Write(body)
}