2021-09-02 17:22:39 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2021-09-04 20:17:56 +00:00
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
2021-09-02 17:22:39 +00:00
|
|
|
"net/http"
|
2021-09-02 21:05:57 +00:00
|
|
|
"strings"
|
2021-09-02 17:22:39 +00:00
|
|
|
|
2021-09-06 22:46:48 +00:00
|
|
|
"github.com/by-cx/lobby/server"
|
2021-09-02 17:22:39 +00:00
|
|
|
"github.com/labstack/echo"
|
|
|
|
)
|
|
|
|
|
|
|
|
func listHandler(c echo.Context) error {
|
2021-09-02 21:05:57 +00:00
|
|
|
labels := c.QueryParam("labels")
|
2021-09-05 15:30:21 +00:00
|
|
|
prefixes := c.QueryParam("prefixes")
|
2021-09-02 17:22:39 +00:00
|
|
|
|
|
|
|
var discoveries []server.Discovery
|
|
|
|
|
2021-09-02 21:05:57 +00:00
|
|
|
if len(labels) > 0 {
|
|
|
|
labelsFilterSlice := strings.Split(labels, ",")
|
|
|
|
discoveries = discoveryStorage.Filter(labelsFilterSlice)
|
2021-09-05 15:30:21 +00:00
|
|
|
} else if len(prefixes) > 0 {
|
|
|
|
discoveries = discoveryStorage.FilterPrefix(strings.Split(prefixes, ","))
|
2021-09-02 17:22:39 +00:00
|
|
|
} else {
|
|
|
|
discoveries = discoveryStorage.GetAll()
|
|
|
|
}
|
|
|
|
|
|
|
|
return c.JSONPretty(200, discoveries, " ")
|
|
|
|
}
|
|
|
|
|
|
|
|
func prometheusHandler(c echo.Context) error {
|
2021-09-02 21:05:57 +00:00
|
|
|
name := c.Param("name")
|
|
|
|
|
|
|
|
services := preparePrometheusOutput(name, discoveryStorage.GetAll())
|
2021-09-02 17:22:39 +00:00
|
|
|
|
|
|
|
return c.JSONPretty(http.StatusOK, services, " ")
|
|
|
|
}
|
2021-09-04 20:17:56 +00:00
|
|
|
|
|
|
|
func getIdentificationHandler(c echo.Context) error {
|
|
|
|
discovery, err := localHost.GetIdentification()
|
|
|
|
if err != nil {
|
|
|
|
return c.String(http.StatusInternalServerError, fmt.Sprintf("gathering identification info error: %v\n", err))
|
|
|
|
}
|
|
|
|
|
|
|
|
return c.JSONPretty(http.StatusOK, discovery, " ")
|
|
|
|
}
|
|
|
|
|
|
|
|
func addLabelsHandler(c echo.Context) error {
|
|
|
|
body, err := ioutil.ReadAll(c.Request().Body)
|
|
|
|
if err != nil {
|
|
|
|
return c.String(http.StatusBadRequest, fmt.Sprintf("reading request body error: %v\n", err))
|
|
|
|
}
|
|
|
|
|
|
|
|
labels := server.Labels{}
|
|
|
|
|
|
|
|
for _, label := range strings.Split(string(body), "\n") {
|
|
|
|
labels = append(labels, server.Label(label))
|
|
|
|
}
|
|
|
|
|
|
|
|
err = localHost.AddLabels(labels)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return c.String(http.StatusBadRequest, err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
return c.String(http.StatusOK, "OK")
|
|
|
|
}
|
|
|
|
|
|
|
|
func deleteLabelsHandler(c echo.Context) error {
|
|
|
|
body, err := ioutil.ReadAll(c.Request().Body)
|
|
|
|
if err != nil {
|
|
|
|
return c.String(http.StatusBadRequest, fmt.Sprintf("reading request body error: %v\n", err))
|
|
|
|
}
|
|
|
|
|
|
|
|
labels := server.Labels{}
|
|
|
|
|
|
|
|
for _, label := range strings.Split(string(body), "\n") {
|
|
|
|
labels = append(labels, server.Label(label))
|
|
|
|
}
|
|
|
|
|
|
|
|
err = localHost.DeleteLabels(labels)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return c.String(http.StatusBadRequest, err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
return c.String(http.StatusOK, "OK")
|
|
|
|
}
|