geo-smart-system/main.go

54 lines
1.5 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"
"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())
2019-10-31 13:56:26 +00:00
r.GET("/get/unique/id", func(c *gin.Context) {
var location model.Location
_ = 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.POST("/set/point", 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) {
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")
2019-10-31 13:56:26 +00:00
r.Static("/assets", "./assets")
2019-10-27 16:34:34 +00:00
_ = r.Run()
2019-10-19 16:38:38 +00:00
}