Upload files to "/"

This commit is contained in:
s490122 2024-06-23 21:52:05 +02:00
parent 3d37f6c420
commit 12f4ba32b1

105
serwer.py Normal file
View File

@ -0,0 +1,105 @@
from wsgiref.simple_server import make_server
import json
import pyodbc
response_headers = [
('Content-Type', 'application/json'),
('Access-Control-Allow-Origin', '*'),
('Access-Control-Allow-Methods', 'POST, GET, OPTIONS, DELETE'),
('Access-Control-Allow-Headers', 'Content-Type')
]
# Database connection
conn = pyodbc.connect(
'DRIVER={ODBC Driver 17 for SQL Server};'
'SERVER=mssql-2017.labs.wmi.amu.edu.pl;'
'DATABASE=dbad_s490122;'
'UID=dbad_s490122;'
'PWD=c1Dp8y8iWJ'
)
cursor = conn.cursor()
def get_request_body(environ):
request_body_size = int(environ.get('CONTENT_LENGTH', 0))
request_body = environ['wsgi.input'].read(request_body_size)
return json.loads(request_body)
def parse_task_id_from_path(path):
try:
return int(path.split('/')[-1])
except (IndexError, ValueError):
return None
def create_response(start_response, status, response_body):
start_response(status, response_headers)
return [json.dumps(response_body).encode('utf-8')]
def add_task(environ, start_response):
data = get_request_body(environ)
task_description = data.get('task_description', '')
# Lambda expression to format task description
format_description = lambda desc: desc.upper()
if task_description:
formatted_description = format_description(task_description)
cursor.execute("INSERT INTO Tasks (task_description) OUTPUT INSERTED.id VALUES (?)", (formatted_description,))
task_id = cursor.fetchone()[0]
conn.commit()
response_body = {'message': 'Task added successfully', 'id': task_id}
return create_response(start_response, '200 OK', response_body)
else:
response_body = {'message': 'Task description is required'}
return create_response(start_response, '400 Bad Request', response_body)
def get_tasks(environ, start_response):
cursor.execute("SELECT id, task_description FROM Tasks")
tasks = cursor.fetchall()
tasks_list = [{'id': task[0], 'task_description': task[1]} for task in tasks]
return create_response(start_response, '200 OK', tasks_list)
def delete_task(environ, start_response, task_id):
cursor.execute("DELETE FROM Tasks WHERE id = ?", (task_id,))
conn.commit()
response_body = {'message': 'Task deleted successfully'}
return create_response(start_response, '200 OK', response_body)
def handle_request(environ, start_response):
path = environ.get('PATH_INFO', '')
method = environ.get('REQUEST_METHOD', '')
if method == 'OPTIONS':
start_response('200 OK', response_headers)
return [b'']
if method == 'POST' and path == '/add_task':
return add_task(environ, start_response)
elif method == 'GET' and path == '/get_tasks':
return get_tasks(environ, start_response)
elif method == 'DELETE' and path.startswith('/delete_task/'):
task_id = parse_task_id_from_path(path)
if task_id is not None:
return delete_task(environ, start_response, task_id)
else:
response_body = {'message': 'Invalid task ID'}
return create_response(start_response, '400 Bad Request', response_body)
else:
response_body = {'message': 'Not Found'}
return create_response(start_response, '404 Not Found', response_body)
def application(environ, start_response):
try:
return handle_request(environ, start_response)
except Exception as e:
response_body = {'message': str(e)}
return create_response(start_response, '500 Internal Server Error', response_body)
if __name__ == "__main__":
httpd = make_server('', 8000, application)
print("Serving on port 8000...")
httpd.serve_forever()