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