geo-smart-system/main.go

39 lines
1.0 KiB
Go
Raw Normal View History

2019-10-19 16:38:38 +00:00
package main
import (
"github.com/gin-contrib/cors"
2019-10-27 16:34:34 +00:00
"github.com/gin-contrib/sse"
2019-10-19 16:38:38 +00:00
"github.com/gin-gonic/gin"
"github.com/go-redis/redis"
"github.com/supanadit/geosmartsystem/model"
2019-10-20 03:34:17 +00:00
"github.com/supanadit/geosmartsystem/model/tile38"
2019-10-19 16:38:38 +00:00
)
func main() {
client := redis.NewClient(&redis.Options{
Addr: "localhost:9851",
})
2019-10-27 16:34:34 +00:00
r := gin.Default()
r.Use(cors.Default())
r.POST("/set-points", func(c *gin.Context) {
2019-10-20 03:34:17 +00:00
var location model.Location
2019-10-27 16:34:34 +00:00
_ = c.BindJSON(&location)
c.Writer.Header().Set("Content-Type", "application/json")
client.Do("SET", location.Type, location.Id, "POINT", location.Lat, location.Lng)
c.JSON(200, gin.H{"status": "ok"})
})
r.GET("/stream", func(c *gin.Context) {
writer := c.Writer
writer.Header().Set("Content-Type", "text/event-stream")
writer.Header().Set("Cache-Control", "no-cache")
writer.Header().Set("Connection", "keep-alive")
data, _ := tile38.FromScan(client, "sales")
_ = sse.Encode(writer, sse.Event{
Event: "message",
Data: data,
})
2019-10-19 16:38:38 +00:00
})
2019-10-27 16:34:34 +00:00
r.Static("/public", "./public")
_ = r.Run()
2019-10-19 16:38:38 +00:00
}