diff --git a/controllers/server.go b/controllers/server.go index 154077e6..faef6ec7 100644 --- a/controllers/server.go +++ b/controllers/server.go @@ -6,8 +6,10 @@ import ( "strings" "github.com/gorilla/mux" + "github.com/gravitl/netmaker/database" "github.com/gravitl/netmaker/logic" "github.com/gravitl/netmaker/models" + "github.com/gravitl/netmaker/mq" "github.com/gravitl/netmaker/servercfg" ) @@ -19,6 +21,35 @@ func serverHandlers(r *mux.Router) { })) r.HandleFunc("/api/server/getconfig", allowUsers(http.HandlerFunc(getConfig))).Methods(http.MethodGet) r.HandleFunc("/api/server/getserverinfo", authorize(true, false, "node", http.HandlerFunc(getServerInfo))).Methods(http.MethodGet) + r.HandleFunc("/api/server/status", http.HandlerFunc(getStatus)).Methods(http.MethodGet) +} + +// swagger:route GET /api/server/status server getStatus +// +// Get the server configuration. +// +// Schemes: https +// +// Security: +// oauth +// +// Responses: +// 200: serverConfigResponse +func getStatus(w http.ResponseWriter, r *http.Request) { + // TODO + // - check health of broker + type status struct { + DB bool `json:"db_connected"` + Broker bool `json:"broker_connected"` + } + + currentServerStatus := status{ + DB: database.IsConnected(), + Broker: mq.IsConnected(), + } + + w.Header().Set("Content-Type", "application/json") + json.NewEncoder(w).Encode(¤tServerStatus) } // allowUsers - allow all authenticated (valid) users - only used by getConfig, may be able to remove during refactor diff --git a/database/database.go b/database/database.go index 156d18a2..64baf49d 100644 --- a/database/database.go +++ b/database/database.go @@ -80,6 +80,8 @@ const ( FETCH_ALL = "fetchall" // CLOSE_DB - graceful close of db const CLOSE_DB = "closedb" + // isconnected + isConnected = "isconnected" ) func getCurrentDB() map[string]interface{} { @@ -241,3 +243,8 @@ func initializeUUID() error { func CloseDB() { getCurrentDB()[CLOSE_DB].(func())() } + +// IsConnected - tell if the database is connected or not +func IsConnected() bool { + return getCurrentDB()[isConnected].(func() bool)() +} diff --git a/database/postgres.go b/database/postgres.go index cc0dbd9f..790e3659 100644 --- a/database/postgres.go +++ b/database/postgres.go @@ -22,6 +22,7 @@ var PG_FUNCTIONS = map[string]interface{}{ DELETE_ALL: pgDeleteAllRecords, FETCH_ALL: pgFetchRecords, CLOSE_DB: pgCloseDB, + isConnected: pgIsConnected, } func getPGConnString() string { @@ -135,3 +136,8 @@ func pgFetchRecords(tableName string) (map[string]string, error) { func pgCloseDB() { PGDB.Close() } + +func pgIsConnected() bool { + stats := PGDB.Stats() + return stats.OpenConnections > 0 +} diff --git a/database/rqlite.go b/database/rqlite.go index 5af68d52..3632310a 100644 --- a/database/rqlite.go +++ b/database/rqlite.go @@ -20,6 +20,7 @@ var RQLITE_FUNCTIONS = map[string]interface{}{ DELETE_ALL: rqliteDeleteAllRecords, FETCH_ALL: rqliteFetchRecords, CLOSE_DB: rqliteCloseDB, + isConnected: rqliteConnected, } func initRqliteDatabase() error { @@ -104,3 +105,8 @@ func rqliteFetchRecords(tableName string) (map[string]string, error) { func rqliteCloseDB() { RQliteDatabase.Close() } + +func rqliteConnected() bool { + leader, err := RQliteDatabase.Leader() + return err == nil && len(leader) > 0 +} diff --git a/database/sqlite.go b/database/sqlite.go index bbbdec6f..97d77466 100644 --- a/database/sqlite.go +++ b/database/sqlite.go @@ -25,6 +25,7 @@ var SQLITE_FUNCTIONS = map[string]interface{}{ DELETE_ALL: sqliteDeleteAllRecords, FETCH_ALL: sqliteFetchRecords, CLOSE_DB: sqliteCloseDB, + isConnected: sqliteConnected, } func initSqliteDB() error { @@ -135,3 +136,8 @@ func sqliteFetchRecords(tableName string) (map[string]string, error) { func sqliteCloseDB() { SqliteDB.Close() } + +func sqliteConnected() bool { + stats := SqliteDB.Stats() + return stats.OpenConnections > 0 +} diff --git a/mq/mq.go b/mq/mq.go index d0ccc420..e24811ae 100644 --- a/mq/mq.go +++ b/mq/mq.go @@ -129,3 +129,8 @@ func Keepalive(ctx context.Context) { } } } + +// IsConnected - function for determining if the mqclient is connected or not +func IsConnected() bool { + return mqclient != nil && mqclient.IsConnected() +}