diff --git a/broker/http.go b/broker/http.go index 4dda055..e8a8090 100644 --- a/broker/http.go +++ b/broker/http.go @@ -4,10 +4,19 @@ import ( "github.com/gin-gonic/gin" ) +const ( + CONNECTIONS = "api/v1/connections" +) + +type resp struct { + Code int `json:"code,omitempty"` + Clients []string `json:"clients,omitempty"` +} + func InitHTTPMoniter(b *Broker) { gin.SetMode(gin.ReleaseMode) router := gin.Default() - router.DELETE("api/v1/connections/:clientid", func(c *gin.Context) { + router.DELETE(CONNECTIONS + "/:clientid", func(c *gin.Context) { clientid := c.Param("clientid") cli, ok := b.clients.Load(clientid) if ok { @@ -16,10 +25,17 @@ func InitHTTPMoniter(b *Broker) { conn.Close() } } - resp := map[string]int{ - "code": 0, - } - c.JSON(200, &resp) + r := resp{Code: 0} + c.JSON(200, &r) + }) + router.GET(CONNECTIONS, func(c *gin.Context) { + conns := make([]string, 0) + b.clients.Range(func (k, v interface{}) bool { + conns = append(conns, v.(*client).info.clientID) + return true + }) + r := resp{Clients: conns} + c.JSON(200, &r) }) router.Run(":" + b.config.HTTPPort)