-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcalc_server_add.go
59 lines (46 loc) · 1.2 KB
/
calc_server_add.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
package main
import (
"calc_util"
"flag"
"fmt"
"log"
"net/http"
"strconv"
)
func handler(w http.ResponseWriter, r *http.Request, url string) {
output := calc_util.ResultMsg{}
a, err := parseFloatQueryParamAndTransform(r, "a", url)
if err != nil {
output.Error = err.Error()
log.Println(output.Error)
}
b, err := parseFloatQueryParamAndTransform(r, "b", url)
if err != nil {
output.Error = err.Error()
log.Println(output.Error)
}
if output.Error == "" {
output.Result = a + b
}
calc_util.SendOutput(w, output)
}
func parseFloatQueryParamAndTransform(r *http.Request, paramName, url string) (num float64, err error) {
num, err = calc_util.ParseFloatQueryParam(r, paramName)
if err != nil {
return 0, err
}
num, err = calc_util.CallUrlAndReturnFloat(fmt.Sprintf("%s?a=%f", url, num))
if err != nil {
return 0, err
}
return num, nil
}
func main() {
port := flag.Int("port", 0, "TCP port for the HTTP server to listen on")
url := flag.String("squareServerUrl", "", "URL to the 'calc_server_square' instance")
flag.Parse()
http.HandleFunc("/compute/add", func(w http.ResponseWriter, r *http.Request) {
handler(w, r, *url)
})
http.ListenAndServe(":" + strconv.Itoa(*port), nil)
}