-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #16 from openfort-xyz/feat/health-endpoint
feat: add health endpoint
- Loading branch information
Showing
7 changed files
with
142 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package healthzhdl | ||
|
||
import ( | ||
"encoding/json" | ||
"errors" | ||
"net/http" | ||
"time" | ||
|
||
"go.openfort.xyz/shield/internal/applications/healthzapp" | ||
domainErrors "go.openfort.xyz/shield/internal/core/domain/errors" | ||
) | ||
|
||
type Handler struct { | ||
app *healthzapp.Application | ||
} | ||
|
||
func New(app *healthzapp.Application) *Handler { | ||
return &Handler{ | ||
app: app, | ||
} | ||
} | ||
|
||
type Status struct { | ||
Status string `json:"status"` | ||
At string `json:"at"` | ||
Checks []Check `json:"checks"` | ||
} | ||
|
||
type Check struct { | ||
Name string `json:"name"` | ||
Status string `json:"status"` | ||
} | ||
|
||
func (h *Handler) Healthz(w http.ResponseWriter, r *http.Request) { | ||
status := Status{ | ||
Status: "healthy", | ||
At: time.Now().UTC().Format(time.RFC3339), | ||
Checks: []Check{}, | ||
} | ||
|
||
err := h.app.Healthz(r.Context()) | ||
if err != nil { | ||
status.Status = "unhealthy" | ||
if errors.Is(err, domainErrors.ErrDatabaseUnavailable) { | ||
status.Checks = append(status.Checks, Check{ | ||
Name: "database", | ||
Status: "unhealthy", | ||
}) | ||
} | ||
} else { | ||
status.Checks = append(status.Checks, Check{ | ||
Name: "database", | ||
Status: "healthy", | ||
}) | ||
} | ||
|
||
w.Header().Set("Content-Type", "application/json") | ||
w.WriteHeader(http.StatusOK) | ||
_ = json.NewEncoder(w).Encode(status) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package healthzapp | ||
|
||
import ( | ||
"context" | ||
"log/slog" | ||
|
||
"go.openfort.xyz/shield/internal/adapters/repositories/sql" | ||
"go.openfort.xyz/shield/internal/core/domain/errors" | ||
"go.openfort.xyz/shield/pkg/logger" | ||
) | ||
|
||
type Application struct { | ||
db *sql.Client | ||
logger *slog.Logger | ||
} | ||
|
||
func New(db *sql.Client) *Application { | ||
return &Application{ | ||
db: db, | ||
logger: logger.New("health_application"), | ||
} | ||
} | ||
|
||
func (a *Application) Healthz(ctx context.Context) error { | ||
db, err := a.db.DB.DB() | ||
if err != nil { | ||
a.logger.ErrorContext(ctx, "failed to get database connection", logger.Error(err)) | ||
return errors.ErrDatabaseUnavailable | ||
} | ||
|
||
if err = db.PingContext(ctx); err != nil { | ||
a.logger.ErrorContext(ctx, "failed to ping database", logger.Error(err)) | ||
return errors.ErrDatabaseUnavailable | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package errors | ||
|
||
import "errors" | ||
|
||
var ( | ||
ErrDatabaseUnavailable = errors.New("database unavailable") | ||
) |