Skip to content
This repository was archived by the owner on Aug 24, 2024. It is now read-only.

Commit

Permalink
Get target page ID
Browse files Browse the repository at this point in the history
  • Loading branch information
nint8835 committed Feb 25, 2024
1 parent 350bf63 commit 6ccd36e
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 5 deletions.
3 changes: 2 additions & 1 deletion cmd/server_run.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
6 changes: 6 additions & 0 deletions pkg/config/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
31 changes: 27 additions & 4 deletions pkg/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -40,6 +42,9 @@ type Server struct {
config *config.ServerConfig
echoInst *echo.Echo

instatusClient *instatus_go.Client
instatusPageId string

statuses sync.Map
}

Expand Down Expand Up @@ -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
Expand All @@ -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
}

0 comments on commit 6ccd36e

Please sign in to comment.