-
Notifications
You must be signed in to change notification settings - Fork 0
/
web_admin_endpoint.go
55 lines (46 loc) · 1.22 KB
/
web_admin_endpoint.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package main
import (
"encoding/json"
"github.com/gin-gonic/gin"
"github.com/messaget/messaget/intent"
"io/ioutil"
)
var intentHandler = intent.NewIntentMap()
func registerAdminIntents() {
intentHandler.AddIntent("LIST_CLIENTS", intentListClients)
intentHandler.AddIntent("SEND_TO_IDS", intentSendToId)
intentHandler.AddIntent("FIND_BY_IDS", intendFindByIds)
intentHandler.AddIntent("FIND_BY_NAMESPACE", intentFindByNamespaces)
intentHandler.AddIntent("FIND_BY_NAMESPACE_EXACT", intentFindByNamespaceExact)
intentHandler.AddIntent("KICK_CLIENTS_BY_ID", intentKickClients)
}
func handleIntentEndpoint(c *gin.Context) {
// attempt to parse the intent
jsonData, err := ioutil.ReadAll(c.Request.Body)
if err != nil {
errorLogger.Println(err)
c.JSON(400, FailedIntent)
return
}
// parse
var i intent.Intent
if err := json.Unmarshal(jsonData, &i); err != nil {
errorLogger.Println(err)
c.JSON(400, FailedIntent)
return
}
// handle
handler, err := intentHandler.GetHandler(i.Intent)
if err != nil {
errorLogger.Println(err)
c.JSON(400, FailedIntent)
return
}
response, statusCode, err := handler(i)
if err != nil {
errorLogger.Println(err)
c.JSON(500, err)
} else {
c.JSON(statusCode, response)
}
}