forked from rnzsgh/eks-workshop-sample-api-service-go
-
Notifications
You must be signed in to change notification settings - Fork 5
/
main.go
99 lines (78 loc) · 2.22 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
package main
import (
"encoding/json"
"fmt"
"io"
"math"
"math/rand"
"net/http"
"os"
"strconv"
"strings"
"time"
)
const defaultIterations = 10000000
func getEnvVars() []string {
var envVars []string
for _, e := range os.Environ() {
pair := strings.Split(e, "=")
envVars = append(envVars, pair[0]+"="+pair[1])
}
return envVars
}
func inCircle(x, y float64) bool {
return math.Sqrt(x*x+y*y) <= 1.0
}
func monteCarloPi(iterations int) float64 {
source := rand.NewSource(time.Now().Unix())
r := rand.New(source)
var h int
for i := 0; i <= iterations; i++ {
if inCircle(r.Float64(), r.Float64()) {
h++
}
}
pi := 4 * float64(h) / float64(iterations)
return pi
}
func main() {
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
start := time.Now()
res := &response{Message: "Monte-carlo pi simulation"}
res.EnvVars = getEnvVars()
res.Iterations = defaultIterations
urlParams := r.URL.Query()
if len(urlParams.Get("iterations")) == 0 {
fmt.Printf("Iterations not passed as URL parameter, using default: %d\n",
res.Iterations)
} else {
i, error := strconv.Atoi(urlParams.Get("iterations"))
if error == nil {
fmt.Printf("Setting iterations to: %d\n", i)
res.Iterations = i
} else {
fmt.Printf("Iterations provided in the URL parameter is not a number, using default: %d\n",
res.Iterations)
}
}
fmt.Printf("Starting Monte-carlo approximation with %d iterations\n" , res.Iterations)
res.MonteCarlo = monteCarloPi(res.Iterations)
res.CalcDurationInSec = time.Since(start).Seconds()
// Beautify the JSON output
out, _ := json.MarshalIndent(res, "", " ")
// Normally this would be application/json, but we don't want to prompt downloads
w.Header().Set("Content-Type", "text/plain")
io.WriteString(w, string(out))
fmt.Printf("Monte-carlo pi approximation [%d iterations ]completed in: %.6f seconds\n" ,
res.Iterations,
time.Since(start).Seconds())
})
http.ListenAndServe(":8080", nil)
}
type response struct {
Message string `json:"message"`
Iterations int `json:"iterations"`
MonteCarlo float64 `json:"pi"`
CalcDurationInSec float64 `json:"duration_in_secconds"`
EnvVars []string `json:"env"`
}