diff --git a/core/api/routes.go b/core/api/routes.go index d54ce8d..71171af 100644 --- a/core/api/routes.go +++ b/core/api/routes.go @@ -44,5 +44,6 @@ func GetRouter(auth *AuthMiddleware, prefix string) (rest.App, error) { rest.Post(prefix+"/orgs/:orgId/invites", auth.RequireAuth(PostInvite)), rest.Put(prefix+"/orgs/:orgId/invites/:inviteId", auth.RequireAuth(PutInvite)), rest.Delete(prefix+"/orgs/:orgId/invites/:inviteId", auth.RequireAuth(DeleteInvite)), + rest.Get(prefix+"/health-check", GetSystemHealthStatus), ) } diff --git a/core/api/syshealth.go b/core/api/syshealth.go new file mode 100644 index 0000000..de2a810 --- /dev/null +++ b/core/api/syshealth.go @@ -0,0 +1,38 @@ +package api + +import ( + "github.com/ant0ine/go-json-rest/rest" + "github.com/openaccounting/oa-server/core/model" +) + +/** + * @api {get} /health-check Get system health status + * @apiVersion 1.0.1 + * @apiName GetSystemHealthStatus + * @apiGroup SystemHealth + * + * + * @apiHeader {String} Accept-Version: 1.1.0 semver versioning + * + * @apiSuccess {String} database Database status: "ok"; "fail" + * @apiSuccess {String} api API status: "ok" + * + * @apiSuccessExample Success-Response: + * HTTP/1.1 200 OK + * { + * "database": "ok", + * "api": "ok", + * } + * + * @apiUse InternalServerError + */ +func GetSystemHealthStatus(w rest.ResponseWriter, r *rest.Request) { + status := map[string]string{ + "database": "ok", + "api": "ok", + } + if err := model.Instance.PingDatabase(); err != nil { + status["database"] = "fail" + } + w.WriteJson(status) +} diff --git a/core/model/db/db.go b/core/model/db/db.go index 73e8b77..168a413 100644 --- a/core/model/db/db.go +++ b/core/model/db/db.go @@ -18,6 +18,7 @@ type Datastore interface { PriceInterface SessionInterface ApiKeyInterface + SystemHealthInteface } func NewDB(dataSourceName string) (*DB, error) { diff --git a/core/model/db/syshealth.go b/core/model/db/syshealth.go new file mode 100644 index 0000000..3a40bd0 --- /dev/null +++ b/core/model/db/syshealth.go @@ -0,0 +1,5 @@ +package db + +type SystemHealthInteface interface { + Ping() error +} diff --git a/core/model/model.go b/core/model/model.go index 47629ba..b84ddbf 100644 --- a/core/model/model.go +++ b/core/model/model.go @@ -22,6 +22,7 @@ type Interface interface { PriceInterface SessionInterface ApiKeyInterface + SystemHealthInteface } func NewModel(db db.Datastore, bcrypt util.Bcrypt, config types.Config) *Model { diff --git a/core/model/syshealth.go b/core/model/syshealth.go new file mode 100644 index 0000000..2b6de73 --- /dev/null +++ b/core/model/syshealth.go @@ -0,0 +1,9 @@ +package model + +type SystemHealthInteface interface { + PingDatabase() error +} + +func (model *Model) PingDatabase() error { + return model.db.Ping() +}