1- from fastapi import FastAPI , Request , Response
1+ from fastapi import HTTPException , FastAPI , Request , Response
22import os
33import sqlite3
4+ from os .path import isfile
45
56app = FastAPI ()
67con = sqlite3 .connect (':memory:' )
@@ -16,7 +17,7 @@ async def debug_exception_handler(request: Request, exc: Exception):
1617 status_code = 500 ,
1718 content = "" .join (
1819 traceback .format_exception (
19- etype = type (exc ), value = exc , tb = exc .__traceback__
20+ type (exc ), exc , exc .__traceback__
2021 )
2122 )
2223 )
@@ -33,3 +34,22 @@ async def startup_event():
3334@app .get ("/" )
3435async def root ():
3536 return {"message" : "Hello World" }
37+
38+ @app .get ("/login" )
39+ async def login (email : str , password : str ):
40+ cur = con .cursor ()
41+ cur .execute ("SELECT * FROM users WHERE email = '%s' and password = '%s'" % (email , password ))
42+ return cur .fetchone () is not None
43+
44+ @app .get ("/logout" )
45+ async def root (email : str ):
46+ return {"message" : "Logged out %s!" % email }
47+
48+ @app .get ("/attachment" )
49+ async def attachment (attachment_name : str ):
50+ attachment_path = 'attachments/' + attachment_name
51+ if not isfile (attachment_path ):
52+ raise HTTPException (status_code = 404 , detail = "Attachment not found" )
53+
54+ with open (attachment_path ) as f :
55+ return f .readlines ()
0 commit comments