Skip to content

Commit

Permalink
fix: added services api endpoints
Browse files Browse the repository at this point in the history
  • Loading branch information
henrywhitaker3 committed Apr 19, 2024
1 parent 8d456fb commit f44e7c1
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 2 deletions.
57 changes: 57 additions & 0 deletions internal/http/get.go
Original file line number Diff line number Diff line change
@@ -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,
}
}
4 changes: 4 additions & 0 deletions internal/http/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down
15 changes: 15 additions & 0 deletions internal/http/results.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package http

import (
"context"
"errors"
"log"
"sync"
"time"
Expand All @@ -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
Expand Down Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions internal/resources/views/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<link rel="stylesheet" href="/static/tailwind.css?v={{ .Version }}" />
<title>{{ .Config.UI.PageTitle }}</title>
</head>
<body class="mx-auto min-h-dvh w-full md:w-3/4 p-12 md:p-8 lg:p-6 lg:w-1/2 flex flex-col justify-between bg-neutral-50">
<body class="mx-auto min-h-dvh w-full md:w-3/4 p-6 md:p-8 lg:p-6 lg:w-1/2 flex flex-col justify-between bg-neutral-50">
{{/* Main content */}}
<div class="mb-6">
<div class="flex flex-col justify-center items-center py-6 mb-6 w-full rounded-md shadow-md text-xl {{ .BannerClasses }}">
Expand Down Expand Up @@ -49,7 +49,7 @@ <h3 class="text-lg font-bold inline-block mr-2">{{ .Service.Name }}</h2>

{{/* Footer */}}
<div class="text-center text-gray-400">
Built with <a class="text-avocado-600" href="https://github.com/henrywhitaker3/prompage">PromPage</a> |
Built with <a class="text-avocado-600" href="https://github.com/henrywhitaker3/prompage">PromPage 🔗</a> |
Checked {{ .Age }} ago
</div>

Expand Down

0 comments on commit f44e7c1

Please sign in to comment.