mirror of
				https://github.com/gravitl/netmaker.git
				synced 2025-10-31 12:16:29 +08:00 
			
		
		
		
	added mq and database connected funcs and endpoint
This commit is contained in:
		| @@ -6,8 +6,10 @@ import ( | |||||||
| 	"strings" | 	"strings" | ||||||
|  |  | ||||||
| 	"github.com/gorilla/mux" | 	"github.com/gorilla/mux" | ||||||
|  | 	"github.com/gravitl/netmaker/database" | ||||||
| 	"github.com/gravitl/netmaker/logic" | 	"github.com/gravitl/netmaker/logic" | ||||||
| 	"github.com/gravitl/netmaker/models" | 	"github.com/gravitl/netmaker/models" | ||||||
|  | 	"github.com/gravitl/netmaker/mq" | ||||||
| 	"github.com/gravitl/netmaker/servercfg" | 	"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/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/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 | // allowUsers - allow all authenticated (valid) users - only used by getConfig, may be able to remove during refactor | ||||||
|   | |||||||
| @@ -80,6 +80,8 @@ const ( | |||||||
| 	FETCH_ALL = "fetchall" | 	FETCH_ALL = "fetchall" | ||||||
| 	// CLOSE_DB - graceful close of db const | 	// CLOSE_DB - graceful close of db const | ||||||
| 	CLOSE_DB = "closedb" | 	CLOSE_DB = "closedb" | ||||||
|  | 	// isconnected | ||||||
|  | 	isConnected = "isconnected" | ||||||
| ) | ) | ||||||
|  |  | ||||||
| func getCurrentDB() map[string]interface{} { | func getCurrentDB() map[string]interface{} { | ||||||
| @@ -241,3 +243,8 @@ func initializeUUID() error { | |||||||
| func CloseDB() { | func CloseDB() { | ||||||
| 	getCurrentDB()[CLOSE_DB].(func())() | 	getCurrentDB()[CLOSE_DB].(func())() | ||||||
| } | } | ||||||
|  |  | ||||||
|  | // IsConnected - tell if the database is connected or not | ||||||
|  | func IsConnected() bool { | ||||||
|  | 	return getCurrentDB()[isConnected].(func() bool)() | ||||||
|  | } | ||||||
|   | |||||||
| @@ -22,6 +22,7 @@ var PG_FUNCTIONS = map[string]interface{}{ | |||||||
| 	DELETE_ALL:   pgDeleteAllRecords, | 	DELETE_ALL:   pgDeleteAllRecords, | ||||||
| 	FETCH_ALL:    pgFetchRecords, | 	FETCH_ALL:    pgFetchRecords, | ||||||
| 	CLOSE_DB:     pgCloseDB, | 	CLOSE_DB:     pgCloseDB, | ||||||
|  | 	isConnected:  pgIsConnected, | ||||||
| } | } | ||||||
|  |  | ||||||
| func getPGConnString() string { | func getPGConnString() string { | ||||||
| @@ -135,3 +136,8 @@ func pgFetchRecords(tableName string) (map[string]string, error) { | |||||||
| func pgCloseDB() { | func pgCloseDB() { | ||||||
| 	PGDB.Close() | 	PGDB.Close() | ||||||
| } | } | ||||||
|  |  | ||||||
|  | func pgIsConnected() bool { | ||||||
|  | 	stats := PGDB.Stats() | ||||||
|  | 	return stats.OpenConnections > 0 | ||||||
|  | } | ||||||
|   | |||||||
| @@ -20,6 +20,7 @@ var RQLITE_FUNCTIONS = map[string]interface{}{ | |||||||
| 	DELETE_ALL:   rqliteDeleteAllRecords, | 	DELETE_ALL:   rqliteDeleteAllRecords, | ||||||
| 	FETCH_ALL:    rqliteFetchRecords, | 	FETCH_ALL:    rqliteFetchRecords, | ||||||
| 	CLOSE_DB:     rqliteCloseDB, | 	CLOSE_DB:     rqliteCloseDB, | ||||||
|  | 	isConnected:  rqliteConnected, | ||||||
| } | } | ||||||
|  |  | ||||||
| func initRqliteDatabase() error { | func initRqliteDatabase() error { | ||||||
| @@ -104,3 +105,8 @@ func rqliteFetchRecords(tableName string) (map[string]string, error) { | |||||||
| func rqliteCloseDB() { | func rqliteCloseDB() { | ||||||
| 	RQliteDatabase.Close() | 	RQliteDatabase.Close() | ||||||
| } | } | ||||||
|  |  | ||||||
|  | func rqliteConnected() bool { | ||||||
|  | 	leader, err := RQliteDatabase.Leader() | ||||||
|  | 	return err == nil && len(leader) > 0 | ||||||
|  | } | ||||||
|   | |||||||
| @@ -25,6 +25,7 @@ var SQLITE_FUNCTIONS = map[string]interface{}{ | |||||||
| 	DELETE_ALL:   sqliteDeleteAllRecords, | 	DELETE_ALL:   sqliteDeleteAllRecords, | ||||||
| 	FETCH_ALL:    sqliteFetchRecords, | 	FETCH_ALL:    sqliteFetchRecords, | ||||||
| 	CLOSE_DB:     sqliteCloseDB, | 	CLOSE_DB:     sqliteCloseDB, | ||||||
|  | 	isConnected:  sqliteConnected, | ||||||
| } | } | ||||||
|  |  | ||||||
| func initSqliteDB() error { | func initSqliteDB() error { | ||||||
| @@ -135,3 +136,8 @@ func sqliteFetchRecords(tableName string) (map[string]string, error) { | |||||||
| func sqliteCloseDB() { | func sqliteCloseDB() { | ||||||
| 	SqliteDB.Close() | 	SqliteDB.Close() | ||||||
| } | } | ||||||
|  |  | ||||||
|  | func sqliteConnected() bool { | ||||||
|  | 	stats := SqliteDB.Stats() | ||||||
|  | 	return stats.OpenConnections > 0 | ||||||
|  | } | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user
	 0xdcarns
					0xdcarns