mirror of
https://github.com/jokob-sk/NetAlertX.git
synced 2025-12-07 09:36:05 -08:00
GraphQl 0.11.16.2 - better api_token initialization
Some checks are pending
docker / docker_dev (push) Waiting to run
Some checks are pending
docker / docker_dev (push) Waiting to run
This commit is contained in:
@@ -367,12 +367,6 @@ function mapColumnIndexToFieldName(index, tableColumnVisible) {
|
|||||||
"devSourcePlugin"
|
"devSourcePlugin"
|
||||||
];
|
];
|
||||||
|
|
||||||
console.log(index);
|
|
||||||
console.log(tableColumnVisible);
|
|
||||||
console.log(tableColumnOrder); // this
|
|
||||||
console.log(missingNumbers);
|
|
||||||
console.log(columnNames[tableColumnOrder[index]]);
|
|
||||||
|
|
||||||
return columnNames[tableColumnOrder[index]] || null;
|
return columnNames[tableColumnOrder[index]] || null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -48,15 +48,17 @@ def update_api(db, all_plugins, isNotification = False, updateOnlyDataSources =
|
|||||||
|
|
||||||
# Start the GraphQL server
|
# Start the GraphQL server
|
||||||
graphql_port_value = get_setting_value("GRAPHQL_PORT")
|
graphql_port_value = get_setting_value("GRAPHQL_PORT")
|
||||||
|
api_token_value = get_setting_value("API_TOKEN")
|
||||||
|
|
||||||
if graphql_port_value is not None:
|
# Validate and start the server if settings are available
|
||||||
|
if graphql_port_value is not None and api_token_value is not None:
|
||||||
try:
|
try:
|
||||||
graphql_port_value = int(graphql_port_value)
|
graphql_port_value = int(graphql_port_value) # Ensure port is an integer
|
||||||
start_server(graphql_port=graphql_port_value) # Pass the port if the server accepts it
|
start_server(graphql_port=graphql_port_value) # Start the server
|
||||||
except ValueError:
|
except ValueError:
|
||||||
mylog('none', [f"[API] Invalid GRAPHQL_PORT value, must be an integer: {graphql_port_value}"])
|
mylog('none', [f"[API] Invalid GRAPHQL_PORT value, must be an integer: {graphql_port_value}"])
|
||||||
else:
|
else:
|
||||||
mylog('none', [f"[API] GRAPHQL_PORT is not set, will try later."])
|
mylog('none', [f"[API] GRAPHQL_PORT or API_TOKEN is not set, will try later."])
|
||||||
|
|
||||||
|
|
||||||
#-------------------------------------------------------------------------------
|
#-------------------------------------------------------------------------------
|
||||||
|
|||||||
@@ -4,60 +4,57 @@ from .graphql_schema import devicesSchema
|
|||||||
from graphene import Schema
|
from graphene import Schema
|
||||||
import sys
|
import sys
|
||||||
|
|
||||||
# Global variable to track the server thread
|
|
||||||
server_thread = None
|
|
||||||
|
|
||||||
# Register NetAlertX directories
|
# Register NetAlertX directories
|
||||||
INSTALL_PATH="/app"
|
INSTALL_PATH = "/app"
|
||||||
sys.path.extend([f"{INSTALL_PATH}/server"])
|
sys.path.extend([f"{INSTALL_PATH}/server"])
|
||||||
|
|
||||||
from logger import mylog
|
from logger import mylog
|
||||||
from helper import get_setting_value, timeNowTZ, updateState
|
from helper import get_setting_value, timeNowTZ, updateState
|
||||||
from notification import write_notification
|
from notification import write_notification
|
||||||
|
|
||||||
|
# Flask application
|
||||||
app = Flask(__name__)
|
app = Flask(__name__)
|
||||||
|
|
||||||
API_TOKEN = get_setting_value("API_TOKEN")
|
# Retrieve API token and port
|
||||||
|
graphql_port_value = get_setting_value("GRAPHQL_PORT")
|
||||||
|
api_token_value = get_setting_value("API_TOKEN")
|
||||||
|
|
||||||
|
# Endpoint for GraphQL queries
|
||||||
@app.route("/graphql", methods=["POST"])
|
@app.route("/graphql", methods=["POST"])
|
||||||
def graphql_endpoint():
|
def graphql_endpoint():
|
||||||
# Check for API token in headers
|
# Check for API token in headers
|
||||||
token = request.headers.get("Authorization")
|
token = request.headers.get("Authorization")
|
||||||
if token != f"Bearer {API_TOKEN}":
|
if token != f"Bearer {api_token_value}":
|
||||||
mylog('verbose', [f'[graphql_server] Unauthorized access attempt'])
|
mylog('verbose', [f'[graphql_server] Unauthorized access attempt'])
|
||||||
|
|
||||||
return jsonify({"error": "Unauthorized"}), 401
|
return jsonify({"error": "Unauthorized"}), 401
|
||||||
|
|
||||||
|
# Retrieve and log request data
|
||||||
data = request.get_json()
|
data = request.get_json()
|
||||||
mylog('verbose', [f'[graphql_server] data: {data}'])
|
mylog('verbose', [f'[graphql_server] data: {data}'])
|
||||||
|
|
||||||
|
# Execute the GraphQL query
|
||||||
# Use the schema to execute the GraphQL query
|
|
||||||
result = devicesSchema.execute(data.get("query"), variables=data.get("variables"))
|
result = devicesSchema.execute(data.get("query"), variables=data.get("variables"))
|
||||||
|
|
||||||
# Return the data from the query in JSON format
|
# Return the result as JSON
|
||||||
return jsonify(result.data)
|
return jsonify(result.data)
|
||||||
|
|
||||||
def start_server(graphql_port):
|
def start_server(graphql_port):
|
||||||
"""Function to start the GraphQL server in a background thread."""
|
"""Start the GraphQL server in a background thread."""
|
||||||
|
state = updateState("GraphQL: Starting", None, None, None, None)
|
||||||
state = updateState("GraphQL: Starting", None, None, None, None)
|
|
||||||
|
|
||||||
if state.graphQLServerStarted == 0:
|
if state.graphQLServerStarted == 0:
|
||||||
|
|
||||||
mylog('verbose', [f'[graphql_server] Starting on port: {graphql_port}'])
|
mylog('verbose', [f'[graphql_server] Starting on port: {graphql_port}'])
|
||||||
|
|
||||||
# Start the Flask app in a separate thread
|
# Start Flask app in a separate thread
|
||||||
thread = threading.Thread(target=lambda: app.run(host="0.0.0.0", port=graphql_port, debug=True, use_reloader=False))
|
thread = threading.Thread(
|
||||||
|
target=lambda: app.run(
|
||||||
|
host="0.0.0.0",
|
||||||
|
port=graphql_port,
|
||||||
|
debug=True,
|
||||||
|
use_reloader=False
|
||||||
|
)
|
||||||
|
)
|
||||||
thread.start()
|
thread.start()
|
||||||
|
|
||||||
|
# Update the state to indicate the server has started
|
||||||
# update API endpoint to indicate that the GraphQL backend started
|
state = updateState("Process: Wait", None, None, None, 1)
|
||||||
# updateState(newState (text),
|
|
||||||
# settingsSaved = None (timestamp),
|
|
||||||
# settingsImported = None (timestamp),
|
|
||||||
# showSpinner = False (1/0),
|
|
||||||
# graphQLServerStarted = False (1/0)
|
|
||||||
# )
|
|
||||||
state = updateState("Process: Wait", None, None, None, 1)
|
|
||||||
|
|
||||||
Reference in New Issue
Block a user