From c4e4d8ded066472662b6a2f05f625a48d140066c Mon Sep 17 00:00:00 2001 From: Joakim Persson Date: Thu, 18 Jul 2024 17:13:02 +0200 Subject: [PATCH] =?UTF-8?q?Lagt=20till=20konfigurering=20fr=C3=A5n=20fil?= =?UTF-8?q?=20samt=20rensat=20ut=20oanv=C3=A4nd=20kod?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- smartassist/src/startservices.py | 57 ++++++++++++++++++++++++-------- 1 file changed, 43 insertions(+), 14 deletions(-) diff --git a/smartassist/src/startservices.py b/smartassist/src/startservices.py index 4292f46..102f1fa 100644 --- a/smartassist/src/startservices.py +++ b/smartassist/src/startservices.py @@ -1,21 +1,45 @@ # Start all services import subprocess -import threading - +import os +import yaml from backend import run_flask - import socket +import urllib.parse -def start_frontend(): - host = 'localhost' # or the address of your server - port = 8000 # change to your server's port + +def configure(): + # Load configuration file that defines parameters for services + with open('./smartassist/config/smartassist.yaml') as f: + config = yaml.safe_load(f) + + # 1. Find the API Key environment variable name + api_key_env = None + for key, value in config['ollama'].items(): + if isinstance(value, str) and value.startswith("${") and value.endswith("}"): + api_key_env = value[2:-1] # Extract name between ${} + + # 2. Obtain API Key if an environment variable was found + api_key = None + if api_key_env: + api_key = os.getenv(api_key_env) + else: + print("Warning: Environment variable reference not found in YAML configuration.") + + # Update the config dictionary with the actual API key (or None if not found) + config['ollama']['api_key'] = api_key + return config + + +def start_frontend(config): + parsed_url = urllib.parse.urlparse(config['frontend']['url']) + hostname = parsed_url.netloc.split(':')[0] # Split by ':' and take the first part, i.e., 'localhost', IP, or domain name + port = parsed_url.port # This is the server port # Use the socket module in Python to check whether a port is in use, # which would indicate that a server is already running on that port. - with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s: try: - s.bind((host, port)) + s.bind((hostname, port)) print("No server is running on this host and port.") # Start frontend (web server) as a separate process subprocess.Popen(["python", "-m", "http.server", str(port)]) @@ -28,15 +52,20 @@ def start_frontend(): print(f"Failed to start frontend: {e}") -def start_backend(): +def start_backend(config): + parsed_url = urllib.parse.urlparse(config['backend']['url']) + # hostname = parsed_url.netloc.split(':')[0] # Split by ':' and take the first part, i.e., 'localhost', IP, or domain name + port = parsed_url.port # This is the server port + print(f"{port}") + try: - # Start backend as a separate thread - # threading.Thread(target=run_flask).start() # Flask's built-in server doesn't support running in a separate thread. - run_flask() + run_flask(port = port) except Exception as e: print(f"Failed to start backend: {e}") if __name__ == '__main__': - start_frontend() # No need for this as the backend starts a web server on its own. - start_backend() + conf = configure() # Read config from file and set up config dict + print(conf) + start_frontend(config=conf) # No need for this as the backend starts a web server on its own. + start_backend(config=conf)