Delete Hook when Hook is Called once

This commit is contained in:
Supan Adit Pratama 2019-12-27 18:01:13 +07:00
parent 8a8364da2c
commit e407af393e

19
main.go
View File

@ -85,12 +85,15 @@ func main() {
var detection model.Detection
err := c.BindJSON(&detection)
hookID := "HOOK-" + xid.New().String()
fmt.Printf("Set HOOK with ID : %s \n", hookID)
hookURL := "http://192.168.99.1:" + port + "/detection/call"
hookURL := "http://192.168.99.1:" + port + "/detection/call?hook=" + hookID
trigger := strings.Join(detection.TriggerType, ",")
client.Do("SETHOOK", hookID, hookURL, "NEARBY", detection.Type, "FENCE", "DETECT", trigger, "COMMANDS", "set", "POINT", detection.Lat, detection.Lng)
var status map[string]interface{}
var httpStatus int
if trigger == "" {
status = gin.H{"status": "Please include trigger type such as 'enter','cross','exit','inside' or 'outside'"}
httpStatus = http.StatusBadRequest
} else {
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
@ -98,13 +101,19 @@ func main() {
status = gin.H{"status": "Ok"}
httpStatus = http.StatusOK
}
}
c.Writer.Header().Set("Content-Type", "application/json")
c.JSON(httpStatus, status)
})
r.GET("/detection/call", func(c *gin.Context) {
fmt.Println("Called")
c.JSON(200, gin.H{"status": "test"})
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) {