mirror of
https://github.com/jokob-sk/NetAlertX.git
synced 2025-12-07 09:36:05 -08:00
GraphQl 0.11.16.1 - better port initialization
This commit is contained in:
@@ -5,7 +5,7 @@ import json
|
|||||||
import conf
|
import conf
|
||||||
from const import (apiPath, sql_appevents, sql_devices_all, sql_events_pending_alert, sql_settings, sql_plugins_events, sql_plugins_history, sql_plugins_objects,sql_language_strings, sql_notifications_all, sql_online_history)
|
from const import (apiPath, sql_appevents, sql_devices_all, sql_events_pending_alert, sql_settings, sql_plugins_events, sql_plugins_history, sql_plugins_objects,sql_language_strings, sql_notifications_all, sql_online_history)
|
||||||
from logger import mylog
|
from logger import mylog
|
||||||
from helper import write_file
|
from helper import write_file, get_setting_value
|
||||||
|
|
||||||
# Import the start_server function
|
# Import the start_server function
|
||||||
from graphql_server.graphql_server_start import start_server
|
from graphql_server.graphql_server_start import start_server
|
||||||
@@ -47,7 +47,16 @@ def update_api(db, all_plugins, isNotification = False, updateOnlyDataSources =
|
|||||||
api_endpoint_class(db, dsSQL[1], folder + 'table_' + dsSQL[0] + '.json')
|
api_endpoint_class(db, dsSQL[1], folder + 'table_' + dsSQL[0] + '.json')
|
||||||
|
|
||||||
# Start the GraphQL server
|
# Start the GraphQL server
|
||||||
start_server()
|
graphql_port_value = get_setting_value("GRAPHQL_PORT")
|
||||||
|
|
||||||
|
if graphql_port_value is not None:
|
||||||
|
try:
|
||||||
|
graphql_port_value = int(graphql_port_value)
|
||||||
|
start_server(graphql_port=graphql_port_value) # Pass the port if the server accepts it
|
||||||
|
except ValueError:
|
||||||
|
mylog('none', [f"[API] Invalid GRAPHQL_PORT value, must be an integer: {graphql_port_value}"])
|
||||||
|
else:
|
||||||
|
mylog('none', [f"[API] GRAPHQL_PORT is not set, will try later."])
|
||||||
|
|
||||||
|
|
||||||
#-------------------------------------------------------------------------------
|
#-------------------------------------------------------------------------------
|
||||||
|
|||||||
@@ -17,7 +17,6 @@ from notification import write_notification
|
|||||||
|
|
||||||
app = Flask(__name__)
|
app = Flask(__name__)
|
||||||
|
|
||||||
GRAPHQL_PORT = get_setting_value("GRAPHQL_PORT")
|
|
||||||
API_TOKEN = get_setting_value("API_TOKEN")
|
API_TOKEN = get_setting_value("API_TOKEN")
|
||||||
|
|
||||||
@app.route("/graphql", methods=["POST"])
|
@app.route("/graphql", methods=["POST"])
|
||||||
@@ -39,20 +38,26 @@ def graphql_endpoint():
|
|||||||
# Return the data from the query in JSON format
|
# Return the data from the query in JSON format
|
||||||
return jsonify(result.data)
|
return jsonify(result.data)
|
||||||
|
|
||||||
def start_server():
|
def start_server(graphql_port):
|
||||||
"""Function to start the GraphQL server in a background thread."""
|
"""Function to 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 the 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()
|
||||||
|
|
||||||
# updateState(newState, settingsSaved = None (timestamp), settingsImported = None (timestamp), showSpinner = False (1/0), graphQLServerStarted = False (1/0))
|
|
||||||
# update GraphQL = started
|
# update API endpoint to indicate that the GraphQL backend started
|
||||||
|
# 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)
|
state = updateState("Process: Wait", None, None, None, 1)
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user