Delete serwer.py

This commit is contained in:
s490122 2024-06-22 14:37:42 +02:00
parent 1a02cadf0a
commit a897920423

View File

@ -1,97 +0,0 @@
# Imperatywne i Deklaratywne
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')
]
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 application(environ, start_response):
path = environ.get('PATH_INFO', '') # Pobranie ścieżki
method = environ.get('REQUEST_METHOD', '') # Pobranie HTTP
response_headers = [
('Content-Type', 'application/json'),
('Access-Control-Allow-Origin', '*'),
('Access-Control-Allow-Methods', 'POST, GET, OPTIONS, DELETE'),
('Access-Control-Allow-Headers', 'Content-Type')
]
if method == 'OPTIONS':
start_response('200 OK', response_headers)
return [b'']
# Dodawanie zadania
if path == '/add_task' and method == 'POST':
try:
# Parsowanie
request_body_size = int(environ.get('CONTENT_LENGTH', 0))
request_body = environ['wsgi.input'].read(request_body_size)
data = json.loads(request_body)
task_description = data.get('task_description', '')
# Dodanie
if task_description:
cursor.execute("INSERT INTO Tasks (task_description) OUTPUT INSERTED.id VALUES (?)", (task_description,))
task_id = cursor.fetchone()[0]
conn.commit()
response = json.dumps({'message': 'Task added successfully', 'id': task_id})
status = '200 OK'
else:
response = json.dumps({'message': 'Task description is required'})
status = '400 Bad Request'
except Exception as e:
response = json.dumps({'message': str(e)}) # Obsługa błędu
status = '500 Internal Server Error'
# Pobieranie zadań
elif path == '/get_tasks' and method == 'GET':
try:
cursor.execute("SELECT id, task_description FROM Tasks") # Pobranie zadań z bazy danych
tasks = cursor.fetchall()
tasks_list = [{'id': task[0], 'task_description': task[1]} for task in tasks]
response = json.dumps(tasks_list)
status = '200 OK'
except Exception as e:
response = json.dumps({'message': str(e)}) # Obsługa błędu
status = '500 Internal Server Error'
# Usuwanie zadania
elif path.startswith('/delete_task/') and method == 'DELETE':
try:
task_id = int(path.split('/')[-1])
cursor.execute("DELETE FROM Tasks WHERE id = ?", (task_id,)) # Usunięcie zadania z bazy danych
conn.commit()
response = json.dumps({'message': 'Task deleted successfully'})
status = '200 OK'
except Exception as e:
response = json.dumps({'message': str(e)}) # Obsługa błędu
status = '500 Internal Server Error'
else:
response = json.dumps({'message': 'Not Found'})
status = '404 Not Found'
start_response(status, response_headers)
return [response.encode('utf-8')]
if __name__ == "__main__":
httpd = make_server('', 8000, application) # Utworzenie serwera na porcie 8000
print("Serving on port 8000...")
httpd.serve_forever() # Rozpoczęcie obsługi żądań