geo-smart-system/main.go

153 lines
3.8 KiB
Go
Raw Normal View History

2019-10-19 16:38:38 +00:00
package main
import (
"fmt"
2019-10-19 16:38:38 +00:00
"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/gorilla/websocket"
"github.com/rs/xid"
2019-12-15 07:50:05 +00:00
"github.com/supanadit/geo-smart-system/model"
"github.com/supanadit/geo-smart-system/model/tile38"
"log"
"net/http"
2019-12-27 10:34:01 +00:00
"strings"
2019-10-19 16:38:38 +00:00
)
func main() {
port := "8080"
2019-10-19 16:38:38 +00:00
client := redis.NewClient(&redis.Options{
Addr: "192.168.99.100:9851",
2019-10-19 16:38:38 +00:00
})
2019-10-27 16:34:34 +00:00
r := gin.Default()
r.Use(cors.Default())
2019-12-15 07:50:05 +00:00
r.GET("/id/get/unique", func(c *gin.Context) {
id := xid.New()
c.JSON(200, gin.H{"id": id.String()})
2019-10-31 13:56:26 +00:00
})
2019-12-15 07:50:05 +00:00
r.POST("/point/set", func(c *gin.Context) {
2019-10-20 03:34:17 +00:00
var location model.Location
2019-12-15 07:50:05 +00:00
err := c.BindJSON(&location)
2019-10-27 16:34:34 +00:00
client.Do("SET", location.Type, location.Id, "POINT", location.Lat, location.Lng)
2019-12-15 07:50:05 +00:00
var status map[string]interface{}
var httpStatus int
2019-12-15 07:50:05 +00:00
if err != nil {
status = gin.H{"status": "Unknown Error"}
httpStatus = http.StatusInternalServerError
2019-12-15 07:50:05 +00:00
} else {
status = gin.H{"status": "Ok"}
httpStatus = http.StatusOK
2019-12-15 07:50:05 +00:00
}
c.Writer.Header().Set("Content-Type", "application/json")
c.JSON(httpStatus, status)
2019-12-15 07:50:05 +00:00
})
2019-12-15 07:50:05 +00:00
r.POST("/point/unset", func(c *gin.Context) {
var location model.Location
err := c.BindJSON(&location)
client.Do("DEL", location.Type, location.Id)
var status map[string]interface{}
var httpStatus int
2019-12-15 07:50:05 +00:00
if err != nil {
status = gin.H{"status": "Unknown Error"}
httpStatus = http.StatusInternalServerError
2019-12-15 07:50:05 +00:00
} else {
status = gin.H{"status": "Ok"}
httpStatus = http.StatusOK
2019-12-15 07:50:05 +00:00
}
c.Writer.Header().Set("Content-Type", "application/json")
c.JSON(httpStatus, status)
2019-10-27 16:34:34 +00:00
})
2019-12-15 07:50:05 +00:00
r.GET("/point/get", func(c *gin.Context) {
t := c.DefaultQuery("type", "user")
data, _ := tile38.FromScan(client, t)
c.JSON(http.StatusOK, data)
})
r.GET("/point/get/stream", func(c *gin.Context) {
w := c.Writer
t := c.DefaultQuery("type", "user")
data, _ := tile38.FromScan(client, t)
_ = sse.Encode(w, sse.Event{
Event: "message",
Data: data,
})
})
r.POST("/detection/set", func(c *gin.Context) {
var detection model.Detection
err := c.BindJSON(&detection)
hookID := "HOOK-" + xid.New().String()
2019-12-27 11:01:13 +00:00
hookURL := "http://192.168.99.1:" + port + "/detection/call?hook=" + hookID
2019-12-27 10:34:01 +00:00
trigger := strings.Join(detection.TriggerType, ",")
var status map[string]interface{}
var httpStatus int
2019-12-27 11:01:13 +00:00
if trigger == "" {
status = gin.H{"status": "Please include trigger type such as 'enter','cross','exit','inside' or 'outside'"}
httpStatus = http.StatusBadRequest
} else {
2019-12-27 11:01:13 +00:00
client.Do("SETHOOK", hookID, hookURL, "NEARBY", detection.Type, "FENCE", "DETECT", trigger, "COMMANDS", "set", "POINT", detection.Lat, detection.Lng)
if err != nil {
status = gin.H{"status": "Unknown Error"}
httpStatus = http.StatusInternalServerError
} else {
status = gin.H{"status": "Ok"}
httpStatus = http.StatusOK
}
}
c.Writer.Header().Set("Content-Type", "application/json")
c.JSON(httpStatus, status)
2019-10-19 16:38:38 +00:00
})
r.GET("/detection/call", func(c *gin.Context) {
2019-12-27 11:01:13 +00:00
hookID := c.Query("hook")
if hookID == "" {
c.JSON(http.StatusBadRequest, gin.H{"status": "Wrong Request"})
} else {
client.Do("DELHOOK", hookID)
c.JSON(http.StatusOK, gin.H{"status": "OK"})
}
})
r.GET("/ws", func(c *gin.Context) {
websocketHandler(c.Writer, c.Request)
})
2019-10-27 16:34:34 +00:00
r.Static("/public", "./public")
2019-10-31 13:56:26 +00:00
r.Static("/assets", "./assets")
_ = r.Run(":" + port)
}
var websocketUpgrade = websocket.Upgrader{
ReadBufferSize: 1024,
WriteBufferSize: 1024,
CheckOrigin: func(r *http.Request) bool {
return true
},
}
func websocketHandler(w http.ResponseWriter, r *http.Request) {
conn, err := websocketUpgrade.Upgrade(w, r, nil)
if err != nil {
log.Println(err)
return
}
for {
t, msg, err := conn.ReadMessage()
if err != nil {
break
}
fmt.Printf("Received : %s \n", msg)
_ = conn.WriteMessage(t, []byte("OK"))
}
2019-10-19 16:38:38 +00:00
}