forked from stelligent/mu-ref-spa
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
69 lines (61 loc) · 1.38 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
package main
import (
"github.com/gin-gonic/gin"
"math/rand"
"strconv"
"time"
)
func healthCheck(c *gin.Context) {
c.JSON(200, gin.H{"Status": "OK"})
}
type RandomPoint struct {
X float32
Y float32
InsideCircle bool
}
func randomPoint() RandomPoint {
x := rand.Float32()*2.0 - 1.0
y := rand.Float32()*2.0 - 1.0
return RandomPoint{
X: x,
Y: y,
InsideCircle: (x*x + y*y) < 1.0,
}
}
func randomPoints(count int) []RandomPoint {
points := make([]RandomPoint, count)
for i := 0; i < count; i++ {
points[i] = randomPoint()
}
return points
}
func estimatePi(points []RandomPoint) float32 {
numberInsideCircle := 0
for _, e := range points {
if e.InsideCircle == true {
numberInsideCircle += 1
}
}
return float32(numberInsideCircle) / float32(len(points)) * 4.0
}
func pi(c *gin.Context) {
count := 1000
countString := c.Query("count")
if countString != "" {
customCount, err := strconv.Atoi(countString)
if err == nil {
count = customCount
}
}
points := randomPoints(count)
c.JSON(200, gin.H{"Points": points, "Pi": estimatePi(points)})
}
func main() {
rand.Seed(time.Now().UTC().UnixNano())
r := gin.Default()
r.StaticFile("/index", "./public/index.html") // for local testing, served by CloudFront when deployed
r.GET("/api", healthCheck)
r.GET("/api/health", healthCheck)
r.GET("/api/pi", pi)
r.Run()
}