-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhttp_server.go
60 lines (45 loc) · 1.04 KB
/
http_server.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 (
"fmt"
"io/ioutil"
"net/http"
"sort"
"github.com/gin-gonic/gin"
)
func _feedPoints(c *gin.Context) {
lastPoint, ok := c.GetQuery("last_point")
if !ok {
lastPoint = "0"
}
points := pointsCache.Items()
keys := make([]string, len(points))
i := 0
for key := range points {
if key > lastPoint {
keys[i] = key
i++
}
}
keys = keys[:i]
sort.Strings(keys)
resPoints := make([]*yalsaPoint, len(keys))
for i := range keys {
resPoints[i] = points[keys[i]].Object.(*yalsaPoint)
}
c.JSON(http.StatusOK, resPoints)
}
func startHTTPServer(port int) {
gin.SetMode(gin.ReleaseMode)
gin.DefaultWriter = ioutil.Discard
r := gin.Default()
r.GET("/ping", func(c *gin.Context) {
c.String(200, "pong")
})
r.GET("/v1/points", _feedPoints)
r.StaticFile("/home", "./html/index.html") // for dev purpose
r.GET("/", func(c *gin.Context) {
c.Data(http.StatusOK, "text/html", MustAsset("html/index.html"))
})
fmt.Println("Running http server on", fmt.Sprintf(":%d", port))
r.Run(fmt.Sprintf(":%d", port))
}