51 lines
1.1 KiB
Go
51 lines
1.1 KiB
Go
package routes
|
|
|
|
import (
|
|
"fmt"
|
|
"github.com/labstack/echo/v4"
|
|
"google.golang.org/protobuf/proto"
|
|
"net/http"
|
|
models "vibeStonk/server/models/v1"
|
|
)
|
|
|
|
type healthRoute struct {
|
|
}
|
|
|
|
func NewHealthRoute() Provider {
|
|
return &healthRoute{}
|
|
}
|
|
|
|
func (h *healthRoute) Provide(_ *SystemMiddleware) []*Route {
|
|
return []*Route{
|
|
{endpoint("/health"), http.MethodGet, h.handleGet, nil},
|
|
{endpoint("/health"), http.MethodPost, h.handlePost, nil},
|
|
}
|
|
}
|
|
|
|
func (h *healthRoute) handleGet(c echo.Context) error {
|
|
resp := &models.ServerStatus{Status: "okay"}
|
|
|
|
payload, err := proto.Marshal(resp)
|
|
if err != nil {
|
|
return fmt.Errorf("failed to serialize health response: %w", err)
|
|
}
|
|
|
|
c.Response().Header().Set("Content-Type", "application/x-protobuf")
|
|
_, err = c.Response().Write(payload)
|
|
if err != nil {
|
|
return fmt.Errorf("failed to write health response: %w", err)
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func (h *healthRoute) handlePost(c echo.Context) error {
|
|
name := c.Request().URL.Query().Get("name")
|
|
err := c.String(200, name)
|
|
if err != nil {
|
|
return fmt.Errorf("failed to write response: %w", err)
|
|
}
|
|
|
|
return nil
|
|
}
|