Lagt till konfigurering från fil samt rensat ut oanvänd kod
This commit is contained in:
@@ -1,21 +1,45 @@
|
|||||||
# Start all services
|
# Start all services
|
||||||
import subprocess
|
import subprocess
|
||||||
import threading
|
import os
|
||||||
|
import yaml
|
||||||
from backend import run_flask
|
from backend import run_flask
|
||||||
|
|
||||||
import socket
|
import socket
|
||||||
|
import urllib.parse
|
||||||
|
|
||||||
def start_frontend():
|
|
||||||
host = 'localhost' # or the address of your server
|
def configure():
|
||||||
port = 8000 # change to your server's port
|
# 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,
|
# 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.
|
# which would indicate that a server is already running on that port.
|
||||||
|
|
||||||
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
|
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
|
||||||
try:
|
try:
|
||||||
s.bind((host, port))
|
s.bind((hostname, port))
|
||||||
print("No server is running on this host and port.")
|
print("No server is running on this host and port.")
|
||||||
# Start frontend (web server) as a separate process
|
# Start frontend (web server) as a separate process
|
||||||
subprocess.Popen(["python", "-m", "http.server", str(port)])
|
subprocess.Popen(["python", "-m", "http.server", str(port)])
|
||||||
@@ -28,15 +52,20 @@ def start_frontend():
|
|||||||
print(f"Failed to start frontend: {e}")
|
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:
|
try:
|
||||||
# Start backend as a separate thread
|
run_flask(port = port)
|
||||||
# threading.Thread(target=run_flask).start() # Flask's built-in server doesn't support running in a separate thread.
|
|
||||||
run_flask()
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print(f"Failed to start backend: {e}")
|
print(f"Failed to start backend: {e}")
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
start_frontend() # No need for this as the backend starts a web server on its own.
|
conf = configure() # Read config from file and set up config dict
|
||||||
start_backend()
|
print(conf)
|
||||||
|
start_frontend(config=conf) # No need for this as the backend starts a web server on its own.
|
||||||
|
start_backend(config=conf)
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user