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-31 13:29:44 +00:00
|
|
|
"net/http"
|
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) {
|
2019-10-31 13:29:44 +00:00
|
|
|
w := c.Writer
|
|
|
|
t := c.DefaultQuery("type", "user")
|
|
|
|
r := c.DefaultQuery("request", "")
|
|
|
|
data, _ := tile38.FromScan(client, t)
|
|
|
|
if r == "sse" {
|
|
|
|
w.Header().Set("Content-Type", "text/event-stream")
|
|
|
|
w.Header().Set("Cache-Control", "no-cache")
|
|
|
|
w.Header().Set("Connection", "keep-alive")
|
|
|
|
_ = sse.Encode(w, sse.Event{
|
|
|
|
Event: "message",
|
|
|
|
Data: data,
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
c.JSON(http.StatusOK, 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
|
|
|
}
|