From 6ccd36e0dfcde2c018c721b7054694b7ac40d63a Mon Sep 17 00:00:00 2001 From: Riley Flynn Date: Sun, 25 Feb 2024 19:41:34 -0330 Subject: [PATCH] Get target page ID --- cmd/server_run.go | 3 ++- go.mod | 1 + go.sum | 2 ++ pkg/config/server.go | 6 ++++++ pkg/server/server.go | 31 +++++++++++++++++++++++++++---- 5 files changed, 38 insertions(+), 5 deletions(-) diff --git a/cmd/server_run.go b/cmd/server_run.go index 0d58c73..273ffb7 100644 --- a/cmd/server_run.go +++ b/cmd/server_run.go @@ -15,7 +15,8 @@ var serverRunCmd = &cobra.Command{ serverCfg, err := config.LoadServerConfig() checkError(err, "failed to load server configuration") - serverInst := server.New(serverCfg) + serverInst, err := server.New(serverCfg) + checkError(err, "failed to create server instance") err = serverInst.Start() checkError(err, "failed to start server") diff --git a/go.mod b/go.mod index 9fce6c4..fc8f44a 100644 --- a/go.mod +++ b/go.mod @@ -7,6 +7,7 @@ require ( github.com/joho/godotenv v1.5.1 github.com/kelseyhightower/envconfig v1.4.0 github.com/labstack/echo/v4 v4.11.4 + github.com/nint8835/instatus-go v0.0.0-20240225230047-b21863857528 github.com/rs/zerolog v1.32.0 github.com/spf13/cobra v1.8.0 ) diff --git a/go.sum b/go.sum index 54fd417..ecba743 100644 --- a/go.sum +++ b/go.sum @@ -31,6 +31,8 @@ github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/ github.com/mattn/go-isatty v0.0.19/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y= github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY= github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y= +github.com/nint8835/instatus-go v0.0.0-20240225230047-b21863857528 h1:+QXFbr5lXZkS8PVpYEnB3Ww0mgLGih0IO5ELllCwX+Y= +github.com/nint8835/instatus-go v0.0.0-20240225230047-b21863857528/go.mod h1:wDyLS68x1XOSX4itoetnnIsfVgAjxbjraAXpjbQfhSA= github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= diff --git a/pkg/config/server.go b/pkg/config/server.go index 7ccc949..33af7ea 100644 --- a/pkg/config/server.go +++ b/pkg/config/server.go @@ -16,6 +16,12 @@ type ServerConfig struct { // UpdateFrequency is the amount of time between status updates UpdateFrequency time.Duration `split_words:"true" default:"1m"` + + // InstatusKey is the API key for Instatus + InstatusKey string `split_words:"true" required:"true"` + + // InstatusTargetSubdomain is the Instatus subdomain to submit statuses to + TargetSubdomain string `split_words:"true" required:"true"` } func LoadServerConfig() (*ServerConfig, error) { diff --git a/pkg/server/server.go b/pkg/server/server.go index 841901d..0f82481 100644 --- a/pkg/server/server.go +++ b/pkg/server/server.go @@ -2,12 +2,14 @@ package server import ( "context" + "fmt" "net/http" "sync" "time" "github.com/go-playground/validator/v10" "github.com/labstack/echo/v4" + instatus_go "github.com/nint8835/instatus-go" "github.com/rs/zerolog/log" "github.com/nint8835/instatus-cluster-monitor/pkg/config" @@ -40,6 +42,9 @@ type Server struct { config *config.ServerConfig echoInst *echo.Echo + instatusClient *instatus_go.Client + instatusPageId string + statuses sync.Map } @@ -132,11 +137,29 @@ func (s *Server) handlePing(c echo.Context) error { return c.JSON(http.StatusOK, map[string]any{}) } -func New(c *config.ServerConfig) *Server { +func New(c *config.ServerConfig) (*Server, error) { echoInst := echo.New() serverInst := &Server{ - config: c, - echoInst: echoInst, + config: c, + echoInst: echoInst, + instatusClient: instatus_go.New(c.InstatusKey), + } + + pages, err := serverInst.instatusClient.GetPages(instatus_go.GetPagesRequest{}) + if err != nil { + return nil, fmt.Errorf("error listing pages: %w", err) + } + + for _, page := range pages { + if page.Subdomain == c.TargetSubdomain { + log.Debug().Str("subdomain", c.TargetSubdomain).Str("id", page.Id).Msg("Found target subdomain") + serverInst.instatusPageId = page.Id + break + } + } + + if serverInst.instatusPageId == "" { + return nil, fmt.Errorf("target subdomain not found") } echoInst.HideBanner = true @@ -145,5 +168,5 @@ func New(c *config.ServerConfig) *Server { echoInst.GET("/statuses", serverInst.getStatuses) echoInst.POST("/ping", serverInst.requireAuth(serverInst.handlePing)) - return serverInst + return serverInst, nil }