add health check api

This commit is contained in:
MaysWind
2022-12-05 22:59:26 +08:00
parent fd7905833e
commit 6f88e6ef26
4 changed files with 35 additions and 1 deletions
+2
View File
@@ -131,6 +131,8 @@ func startWebServer(c *cli.Context) error {
router.StaticFile("/desktop/touchicon.png", filepath.Join(config.StaticRootPath, "touchicon.png"))
router.StaticFile("/desktop/manifest.json", filepath.Join(config.StaticRootPath, "manifest.json"))
router.GET("/healthz.json", bindApi(api.Healths.HealthStatusHandler))
apiRoute := router.Group("/api")
apiRoute.Use(bindMiddleware(middlewares.RequestId(config)))
+4
View File
@@ -9,6 +9,7 @@ import (
"github.com/urfave/cli/v2"
"github.com/mayswind/ezbookkeeping/cmd"
"github.com/mayswind/ezbookkeeping/pkg/settings"
"github.com/mayswind/ezbookkeeping/pkg/utils"
)
@@ -24,6 +25,9 @@ var (
)
func main() {
settings.Version = Version
settings.CommitHash = CommitHash
app := &cli.App{
Name: "ezBookkeeping",
Usage: "A lightweight personal bookkeeping app hosted by yourself.",
+26
View File
@@ -0,0 +1,26 @@
package api
import (
"github.com/mayswind/ezbookkeeping/pkg/core"
"github.com/mayswind/ezbookkeeping/pkg/errs"
"github.com/mayswind/ezbookkeeping/pkg/settings"
)
// HealthsApi represents health api
type HealthsApi struct{}
// Initialize a healths api singleton instance
var (
Healths = &HealthsApi{}
)
// HealthStatusHandler returns the health status of current service
func (a *HealthsApi) HealthStatusHandler(c *core.Context) (interface{}, *errs.Error) {
result := make(map[string]string)
result["version"] = settings.Version
result["commit"] = settings.CommitHash
result["status"] = "ok"
return result, nil
}
+3 -1
View File
@@ -7,7 +7,9 @@ type ConfigContainer struct {
// Initialize a config container singleton instance
var (
Container = &ConfigContainer{}
Version string
CommitHash string
Container = &ConfigContainer{}
)
// SetCurrentConfig sets the current config by a given config