Skip to content
Snippets Groups Projects
Commit 16a20670 authored by Sara Savanovic Djordjevic's avatar Sara Savanovic Djordjevic
Browse files

refactor: clean up main.py

parent fd01ac43
No related branches found
No related tags found
No related merge requests found
from flask import Flask
from http.server import HTTPServer, BaseHTTPRequestHandler
from consts import DB_NAME, COLLECTION, SSL_CERT_PATH, SSL_KEY_PATH, HOST, PORT
from consts import SSL_CERT_PATH, SSL_KEY_PATH, HOST, PORT
from map.get_markers import get_all_markers
import ssl
import keyboard
......@@ -9,20 +9,6 @@ import sqlite3
app = Flask(__name__)
terminate_server = 0
# Initialise SQLite database connection
def init_database():
#try:
# Connect to the SQLite database
conn = sqlite3.connect('server/sql_db/icedb')
# Return the connection object
return conn
#except Exception as e:
# print(f"Failed to connect to SQLite database: {e}")
# return None
class IceHTTPServer(HTTPServer):
def __init__(self, server_address, handler_class, cursor):
super().__init__(server_address, handler_class)
......@@ -33,15 +19,15 @@ class IceHTTPServer(HTTPServer):
return request, client_address
# Define custom HTTP class
# Custom HTTP class
class IceHTTP(BaseHTTPRequestHandler):
def __init__(self, request, client_address, server):
self.cursor = server.cursor # Accessing cursor from server
self.cursor = server.cursor
super().__init__(request, client_address, server)
def do_GET(self):
# Root path
if self.path == '/':
if self.path == '/': # NB: temporary root path behavior
self.send_response(200)
self.send_header("Content-type", "text/plain")
self.end_headers()
......@@ -54,7 +40,7 @@ class IceHTTP(BaseHTTPRequestHandler):
elif self.path == '/get_valid_markers': # NB: should be POST?
get_all_markers(self, self.cursor, True) # Get only valid markers
# Listen for pressing of q key to terminate server
# Terminate server on key press q
def on_key_press(server, event, cursor, conn):
if event.name == 'q':
print('Terminating server...')
......@@ -67,11 +53,11 @@ def on_key_press(server, event, cursor, conn):
# Start a server on port 8443 using self defined HTTP class
if __name__ == "__main__":
# Initialise database connection
conn = init_database()
cursor = conn.cursor()
try:
# Initialize database connection
conn = sqlite3.connect('server/sql_db/icedb')
cursor = conn.cursor()
# Load SSL certificate and private key
ssl_context = ssl.SSLContext(ssl.PROTOCOL_TLS_SERVER)
ssl_context.load_cert_chain(SSL_CERT_PATH, SSL_KEY_PATH)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment