feat: Added optional health endpoint in the gateway

This commit is contained in:
jonaustin09
2024-02-23 15:08:15 -05:00
parent ab127c22df
commit 94051634a5
2 changed files with 23 additions and 0 deletions
+10
View File
@@ -42,6 +42,7 @@ var (
natsURL, natsTopic string
logWebhookURL string
accessLog string
healthPath string
debug bool
quiet bool
iamDir string
@@ -324,6 +325,12 @@ func initFlags() []cli.Flag {
Value: 3600,
Destination: &iamCachePrune,
},
&cli.StringFlag{
Name: "health",
Usage: `health check endpoint path. Health endpoint will be configured on GET http method: GET <health>
NOTICE: the path has to be specified with '/'. e.g /health`,
Destination: &healthPath,
},
}
}
@@ -360,6 +367,9 @@ func runGateway(ctx context.Context, be backend.Backend) error {
if quiet {
opts = append(opts, s3api.WithQuiet())
}
if healthPath != "" {
opts = append(opts, s3api.WithHealth(healthPath))
}
admApp := fiber.New(fiber.Config{
AppName: "versitygw",
+13
View File
@@ -16,6 +16,7 @@ package s3api
import (
"crypto/tls"
"net/http"
"github.com/gofiber/fiber/v2"
"github.com/gofiber/fiber/v2/middleware/logger"
@@ -34,6 +35,7 @@ type S3ApiServer struct {
cert *tls.Certificate
quiet bool
debug bool
health string
}
func New(app *fiber.App, be backend.Backend, root middlewares.RootUserConfig, port, region string, iam auth.IAMService, l s3log.AuditLogger, evs s3event.S3EventSender, opts ...Option) (*S3ApiServer, error) {
@@ -52,6 +54,12 @@ func New(app *fiber.App, be backend.Backend, root middlewares.RootUserConfig, po
if !server.quiet {
app.Use(logger.New())
}
// Set up health endpoint if specified
if server.health != "" {
app.Get(server.health, func(ctx *fiber.Ctx) error {
return ctx.SendStatus(http.StatusOK)
})
}
app.Use(middlewares.DecodeURL(l))
app.Use(middlewares.RequestLogger(server.debug))
@@ -90,6 +98,11 @@ func WithQuiet() Option {
return func(s *S3ApiServer) { s.quiet = true }
}
// WithHealth sets up a GET health endpoint
func WithHealth(health string) Option {
return func(s *S3ApiServer) { s.health = health }
}
func (sa *S3ApiServer) Serve() (err error) {
if sa.cert != nil {
return sa.app.ListenTLSWithCertificate(sa.port, *sa.cert)