-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
60 lines (54 loc) · 1.26 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
package main
import (
"log"
"net"
"net/http"
"os"
"strconv"
)
func checkErr(cause string, err error) {
if err != nil {
log.Fatalln(cause, err)
}
}
func calcHandler(op byte) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
a, b := getOperands(r)
resp := calculate(a, b, op)
w.Header().Set("Content-type", "application/json")
w.Write(resp)
})
}
func getOperands(r *http.Request) (a, b int) {
a, err := strconv.Atoi(r.FormValue("a"))
checkErr("Fail to get a", err)
b, err = strconv.Atoi(r.FormValue("b"))
checkErr("Fail to get b", err)
return a, b
}
func calculate(a, b int, op byte) []byte {
var result int
switch op {
case '+':
result = a + b
case '-':
result = a - b
case '*':
result = a * b
case '/':
result = a / b
}
hostname, err := os.Hostname()
checkErr("Fail to get hostname", err)
ip, err := net.ResolveIPAddr("ip", hostname)
checkErr("Fail to get ip", err)
return []byte(ip.String() + `:{"result": ` + strconv.Itoa(result) + `}`)
}
func main() {
http.Handle("/plus", calcHandler('+'))
http.Handle("/minus", calcHandler('-'))
http.Handle("/multiply", calcHandler('*'))
http.Handle("/divide", calcHandler('/'))
log.Println("Starting server at :8080")
http.ListenAndServe(":8080", nil)
}