Delete serwer.py
This commit is contained in:
parent
2c534a21ca
commit
d3b6f771b6
89
serwer.py
89
serwer.py
@ -1,89 +0,0 @@
|
|||||||
from wsgiref.simple_server import make_server
|
|
||||||
import json
|
|
||||||
import pyodbc
|
|
||||||
|
|
||||||
# Konfiguracja połączenia z bazą danych
|
|
||||||
conn = pyodbc.connect(
|
|
||||||
'DRIVER={ODBC Driver 17 for SQL Server};'
|
|
||||||
'SERVER=mssql-2017.labs.wmi.amu.edu.pl;'
|
|
||||||
'DATABASE=dbad_s490127;'
|
|
||||||
'UID=dbad_s490127;'
|
|
||||||
'PWD=Bjf5w2Hhco'
|
|
||||||
)
|
|
||||||
cursor = conn.cursor()
|
|
||||||
|
|
||||||
def application(environ, start_response):
|
|
||||||
path = environ.get('PATH_INFO', '') # Pobranie ścieżki żądania
|
|
||||||
method = environ.get('REQUEST_METHOD', '') # Pobranie metody 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')
|
|
||||||
]
|
|
||||||
|
|
||||||
# Obsługa preflight requests dla CORS
|
|
||||||
if method == 'OPTIONS':
|
|
||||||
start_response('200 OK', response_headers)
|
|
||||||
return [b'']
|
|
||||||
|
|
||||||
# Obsługa dodawania nowego zadania
|
|
||||||
if path == '/add_task' and method == 'POST':
|
|
||||||
try:
|
|
||||||
# Odczytanie i sparsowanie treści żądania
|
|
||||||
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 zadania do bazy danych, jeśli opis jest podany
|
|
||||||
if task_description:
|
|
||||||
cursor.execute("INSERT INTO Tasks (task_description) OUTPUT INSERTED.id VALUES (?)", (task_description,))
|
|
||||||
task_id = cursor.fetchone()[0] # Pobranie ID nowo dodanego zadania
|
|
||||||
conn.commit() # Zatwierdzenie zmian w bazie danych
|
|
||||||
response = json.dumps({'message': 'Task added successfully', 'id': task_id})
|
|
||||||
status = '200 OK'
|
|
||||||
else:
|
|
||||||
response = json.dumps({'message': 'Task description is required'}) # Brak opisu zadania
|
|
||||||
status = '400 Bad Request'
|
|
||||||
except Exception as e:
|
|
||||||
response = json.dumps({'message': str(e)}) # Obsługa błędu
|
|
||||||
status = '500 Internal Server Error'
|
|
||||||
|
|
||||||
# Obsługa pobierania wszystkich 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] # Przygotowanie listy zadań
|
|
||||||
response = json.dumps(tasks_list) # Konwersja listy zadań do formatu JSON
|
|
||||||
status = '200 OK'
|
|
||||||
except Exception as e:
|
|
||||||
response = json.dumps({'message': str(e)}) # Obsługa błędu
|
|
||||||
status = '500 Internal Server Error'
|
|
||||||
|
|
||||||
# Obsługa usuwania konkretnego zadania
|
|
||||||
elif path.startswith('/delete_task/') and method == 'DELETE':
|
|
||||||
try:
|
|
||||||
task_id = int(path.split('/')[-1]) # Wyodrębnienie ID zadania z URL
|
|
||||||
cursor.execute("DELETE FROM Tasks WHERE id = ?", (task_id,)) # Usunięcie zadania z bazy danych
|
|
||||||
conn.commit() # Zatwierdzenie zmian w bazie danych
|
|
||||||
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'
|
|
||||||
|
|
||||||
# Obsługa nieznanych ścieżek
|
|
||||||
else:
|
|
||||||
response = json.dumps({'message': 'Not Found'}) # Nieznana ścieżka
|
|
||||||
status = '404 Not Found'
|
|
||||||
|
|
||||||
start_response(status, response_headers) # Wysłanie odpowiedzi HTTP
|
|
||||||
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ń
|
|
Loading…
Reference in New Issue
Block a user