Skip to content

Commit

Permalink
add healthz API endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
stv0g committed Jun 10, 2022
1 parent e1ddcd4 commit 02ac7f9
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 0 deletions.
1 change: 1 addition & 0 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ func run(cfg *config.Config) {
router.Use(APIMiddleware(svrs, short, cfg))
router.Use(StaticMiddleware(cfg))

router.GET(apiBase+"/healthz", handlers.HandleHealthz)
router.GET(apiBase+"/config", handlers.HandleConfigWith(version, commit, date))
router.POST(apiBase+"/initiate", handlers.HandleInitiate)
router.POST(apiBase+"/part", handlers.HandlePart)
Expand Down
23 changes: 23 additions & 0 deletions pkg/handlers/healthz.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package handlers

import (
"net/http"

"github.com/gin-gonic/gin"
"github.com/stv0g/gose/pkg/server"
)

// HandleConfigWith returns runtime configuration to the frontend
func HandleHealthz(c *gin.Context) {
svrs := c.MustGet("servers").(server.List)

// TODO: check health status of notifier?
// TODO: check health status of shortener?
// shortener := c.MustGet("shortener").(*shortener.Shortener)

for _, svr := range svrs {
if !svr.Healthy() {
c.AbortWithStatus(http.StatusInternalServerError)
}
}
}
10 changes: 10 additions & 0 deletions pkg/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"net/url"
"strings"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/s3"
"github.com/stv0g/gose/pkg/config"
)
Expand Down Expand Up @@ -87,3 +88,12 @@ func (s *Server) DetectImplementation() Implementation {
}
}
}

func (s *Server) Healthy() bool {
_, err := s.S3.ListObjects(&s3.ListObjectsInput{
Bucket: aws.String(s.Config.Bucket),
MaxKeys: aws.Int64(0),
})

return err == nil
}

0 comments on commit 02ac7f9

Please sign in to comment.