11from fastapi import FastAPI , Request , Response
22import os
33import sqlite3
4+ from os .path import isfile
45
56app = FastAPI ()
67con = sqlite3 .connect (':memory:' )
@@ -19,7 +20,7 @@ async def debug_exception_handler(request: Request, exc: Exception):
1920 etype = type (exc ), value = exc , tb = exc .__traceback__
2021 )
2122 )
22- )
23+ )
2324
2425@app .on_event ("startup" )
2526async def startup_event ():
@@ -29,7 +30,28 @@ async def startup_event():
2930 cur .execute ('''CREATE TABLE users (email text, password text)''' )
3031 cur .
execute (
'''INSERT INTO users VALUES ('[email protected] ', '123456')''' )
3132 con .commit ()
33+
3234
3335@app .get ("/" )
3436async def root ():
3537 return {"message" : "Hello World" }
38+
39+
40+ @app .get ("/login" )
41+ async def login (email : str , password : str ):
42+ cur = con .cursor ()
43+ cur .execute ("SELECT * FROM users WHERE email = '%s' and password = '%s'" % (email , password ))
44+ return cur .fetchone () is not None
45+
46+ @app .get ("/logout" )
47+ async def root (email : str ):
48+ return {"message" : "Logged out %s!" % email }
49+
50+ @app .get ("/attachment" )
51+ async def attachment (attachment_name : str ):
52+ attachment_path = 'attachments/' + attachment_name
53+ if not isfile (attachment_path ):
54+ raise HTTPException (status_code = 404 , detail = "Attachment not found" )
55+
56+ with open (attachment_path ) as f :
57+ return f .readlines ()
0 commit comments