Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
P
PROG2900
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Sara Savanovic Djordjevic
PROG2900
Commits
16a20670
Commit
16a20670
authored
1 year ago
by
Sara Savanovic Djordjevic
Browse files
Options
Downloads
Patches
Plain Diff
refactor: clean up main.py
parent
fd01ac43
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
server/main.py
+9
-23
9 additions, 23 deletions
server/main.py
with
9 additions
and
23 deletions
server/main.py
+
9
−
23
View file @
16a20670
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 c
ustom HTTP class
#
C
ustom 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
)
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment