diff --git a/cmd/webserver.go b/cmd/webserver.go index bf9ff41d..53ac9f2d 100644 --- a/cmd/webserver.go +++ b/cmd/webserver.go @@ -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))) diff --git a/ezbookkeeping.go b/ezbookkeeping.go index e48b7990..7303d0c9 100644 --- a/ezbookkeeping.go +++ b/ezbookkeeping.go @@ -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.", diff --git a/pkg/api/healths.go b/pkg/api/healths.go new file mode 100644 index 00000000..cd64c6e5 --- /dev/null +++ b/pkg/api/healths.go @@ -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 +} diff --git a/pkg/settings/setting_container.go b/pkg/settings/setting_container.go index 3221d550..c9d161d6 100644 --- a/pkg/settings/setting_container.go +++ b/pkg/settings/setting_container.go @@ -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