-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
8d456fb
commit f44e7c1
Showing
4 changed files
with
78 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters