From f44e7c10ae488917d3e5f65a5158a0fc6088c121 Mon Sep 17 00:00:00 2001 From: Henry Whitaker Date: Fri, 19 Apr 2024 22:54:45 +0100 Subject: [PATCH] fix: added services api endpoints --- internal/http/get.go | 57 +++++++++++++++++++++++++++++ internal/http/http.go | 4 ++ internal/http/results.go | 15 ++++++++ internal/resources/views/index.html | 4 +- 4 files changed, 78 insertions(+), 2 deletions(-) create mode 100644 internal/http/get.go diff --git a/internal/http/get.go b/internal/http/get.go new file mode 100644 index 0000000..234aaca --- /dev/null +++ b/internal/http/get.go @@ -0,0 +1,57 @@ +package http + +import ( + "errors" + "net/http" + + "github.com/henrywhitaker3/prompage/internal/app" + "github.com/henrywhitaker3/prompage/internal/collector" + "github.com/labstack/echo/v4" +) + +type HttpResult struct { + Name string `json:"name"` + Group string `json:"group"` + Status bool `json:"status"` + Uptime float32 `json:"uptime"` +} + +type GetAllResponse struct { + Services []HttpResult `json:"services"` +} + +func NewGetAllHandler(app *app.App, cache *ResultCache) echo.HandlerFunc { + return func(c echo.Context) error { + res, _ := cache.Get() + + out := []HttpResult{} + for _, r := range res { + out = append(out, convertResult(r)) + } + + return c.JSON(http.StatusOK, &GetAllResponse{Services: out}) + } +} + +func NewGetHandler(app *app.App, cache *ResultCache) echo.HandlerFunc { + return func(c echo.Context) error { + svc, err := cache.GetService(c.Param("name")) + if err != nil { + if errors.Is(err, ErrNotFound) { + return c.JSON(http.StatusNotFound, struct{}{}) + } + return err + } + + return c.JSON(http.StatusOK, convertResult(svc)) + } +} + +func convertResult(r collector.Result) HttpResult { + return HttpResult{ + Name: r.Service.Name, + Group: r.Service.Group, + Status: r.Status, + Uptime: r.Uptime, + } +} diff --git a/internal/http/http.go b/internal/http/http.go index 966bb97..03f196b 100644 --- a/internal/http/http.go +++ b/internal/http/http.go @@ -25,6 +25,10 @@ func NewHttp(app *app.App, cache *ResultCache) *Http { e.GET("/", NewStatusPageHandler(app, cache)) e.GET("/static/*", echo.WrapHandler(http.StripPrefix("/static/", http.FileServerFS(static.FS)))) + + e.GET("/api/services", NewGetAllHandler(app, cache)) + e.GET("/api/services/:name", NewGetHandler(app, cache)) + // e.GET("/metrics", echoprometheus.NewHandler()) return &Http{ diff --git a/internal/http/results.go b/internal/http/results.go index 1064e90..7d0992c 100644 --- a/internal/http/results.go +++ b/internal/http/results.go @@ -2,6 +2,7 @@ package http import ( "context" + "errors" "log" "sync" "time" @@ -11,6 +12,10 @@ import ( "github.com/henrywhitaker3/prompage/internal/config" ) +var ( + ErrNotFound = errors.New("service not found") +) + type Result struct { Query config.Query Status bool @@ -40,6 +45,16 @@ func (c *ResultCache) Get() ([]collector.Result, time.Time) { return c.results, c.time } +func (c *ResultCache) GetService(name string) (collector.Result, error) { + results, _ := c.Get() + for _, r := range results { + if r.Service.Name == name { + return r, nil + } + } + return collector.Result{}, ErrNotFound +} + func (c *ResultCache) Work(ctx context.Context) { c.mu.Lock() c.results = c.collector.Collect(ctx) diff --git a/internal/resources/views/index.html b/internal/resources/views/index.html index 928d2ac..58c941d 100644 --- a/internal/resources/views/index.html +++ b/internal/resources/views/index.html @@ -6,7 +6,7 @@ {{ .Config.UI.PageTitle }} - + {{/* Main content */}}
@@ -49,7 +49,7 @@

{{ .Service.Name }}

{{/* Footer */}}
- Built with PromPage | + Built with PromPage 🔗 | Checked {{ .Age }} ago