mirror of
https://github.com/gravitl/netmaker.git
synced 2025-09-06 05:04:27 +08:00
added mq and database connected funcs and endpoint
This commit is contained in:
parent
f4851937c1
commit
7a2c225eb1
6 changed files with 61 additions and 0 deletions
|
@ -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
|
||||
|
|
|
@ -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)()
|
||||
}
|
||||
|
|
|
@ -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
|
||||
}
|
||||
|
|
|
@ -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
|
||||
}
|
||||
|
|
|
@ -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
|
||||
}
|
||||
|
|
5
mq/mq.go
5
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()
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue