-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
134 lines (106 loc) · 2.1 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
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
package main
import (
"bufio"
"context"
"fmt"
"log"
"net/http"
"os"
"strconv"
"strings"
"github.com/256dpi/serve"
"github.com/256dpi/xo"
)
func main() {
// install
xo.Debug(xo.Config{})
// run repl
go repl()
// prepare mux
mux := http.NewServeMux()
mux.HandleFunc("/calc", calculate)
// prepare handler
handler := serve.Compose(
xo.RootHandler(),
mux,
)
// listen and serve
_ = http.ListenAndServe(":8000", handler)
}
func calculate(w http.ResponseWriter, r *http.Request) {
// trace
ctx, span := xo.Trace(r.Context(), "calculate")
defer span.End()
// get params
a := r.URL.Query().Get("a")
b := r.URL.Query().Get("b")
// call business logic
result, err := businessLogic(ctx, a, b)
if err != nil {
xo.Capture(err)
w.WriteHeader(http.StatusInternalServerError)
return
}
// tag result
span.Tag("result", result)
// log result
log.Printf("result: %s", result)
// write result
_, _ = w.Write([]byte(result))
}
func businessLogic(ctx context.Context, a, b string) (res string, err error) {
return res, xo.Run(ctx, func(ctx *xo.Context) error {
// tag params
ctx.Tag("a", a)
ctx.Tag("b", b)
// parse param a
aa, err := strconv.ParseInt(a, 10, 64)
if err != nil {
return xo.W(err)
}
// parse param b
bb, err := strconv.ParseInt(b, 10, 64)
if err != nil {
return xo.W(err)
}
// check negative
if aa < 0 || bb < 0 {
return xo.F("negative params")
}
// compute result
res = strconv.FormatInt(aa/bb, 10)
return nil
})
}
func repl() {
// prepare buffer
buf := bufio.NewReader(os.Stdin)
// read lines
for {
// read line
str, err := buf.ReadString('\n')
if err != nil {
panic(err)
}
// trim space
str = strings.TrimSpace(str)
// prepare params
var a string
var b string
// split string
params := strings.Split(str, " ")
if len(params) == 2 {
a = params[0]
b = params[1]
}
// compute url
url := fmt.Sprintf("http://0.0.0.0:8000/calc?a=%s&b=%s", a, b)
// make request
res, err := http.Post(url, "text/plain", nil)
if err != nil {
panic(err)
}
// close body
_ = res.Body.Close()
}
}