From c87c841febe155f169fc862d7fb0f2e60c551c3c Mon Sep 17 00:00:00 2001 From: root Date: Mon, 31 Aug 2020 17:10:18 +0000 Subject: [PATCH] add api to check db status --- src/bunker.go | 14 ++++++++++++++ src/storage/storage.go | 5 +++++ 2 files changed, 19 insertions(+) diff --git a/src/bunker.go b/src/bunker.go index 4c5dffe..cbc1c82 100644 --- a/src/bunker.go +++ b/src/bunker.go @@ -153,12 +153,26 @@ func (e mainEnv) backupDB(w http.ResponseWriter, r *http.Request, ps httprouter. e.db.store.BackupDB(w) } +func (e mainEnv) checkStatus(w http.ResponseWriter, r *http.Request, ps httprouter.Params) { + err := e.db.store.Ping() + if err != nil { + w.WriteHeader(500) + w.Write([]byte("error")) + } else { + w.WriteHeader(200) + w.Write([]byte("OK")) + } +} + // setupRouter() setup HTTP Router object. func (e mainEnv) setupRouter() *httprouter.Router { box := packr.NewBox("../ui") router := httprouter.New() + router.GET("/v1/status", e.checkStatus) + router.GET("/status", e.checkStatus) + router.GET("/v1/sys/backup", e.backupDB) router.POST("/v1/user", e.userNew) diff --git a/src/storage/storage.go b/src/storage/storage.go index 41e2e70..458e3c7 100644 --- a/src/storage/storage.go +++ b/src/storage/storage.go @@ -174,6 +174,11 @@ func InitDB(filepath *string) (DBStorage, error){ return dbobj, nil } + +func (dbobj DBStorage) Ping() error { + return dbobj.db.Ping() +} + // CloseDB function closes the open database func (dbobj DBStorage) CloseDB() { dbobj.db.Close()