|
1 | | -from fastapi import FastAPI, Request, Response |
| 1 | +from fastapi import FastAPI, Request, Response, HTTPException |
2 | 2 | import os |
3 | 3 | import sqlite3 |
| 4 | +from os.path import isfile |
4 | 5 |
|
5 | 6 | app = FastAPI() |
6 | 7 | con = sqlite3.connect(':memory:') |
@@ -36,12 +37,33 @@ async def root(): |
36 | 37 | return {"message": "Hello World"} |
37 | 38 |
|
38 | 39 |
|
39 | | -@app.get("/login") |
| 40 | +@app.post("/login") |
40 | 41 | async def login(email: str, password: str): |
41 | 42 | cur = con.cursor() |
42 | 43 | cur.execute("SELECT * FROM users WHERE email = '%s' and password = '%s'" % (email, password)) |
43 | | - return cur.fetchone() is not None |
| 44 | + if not cur.fetchone(): |
| 45 | + raise HTTPException(status_code=401, detail="Unknown username/password") |
| 46 | + |
| 47 | + return email; |
44 | 48 |
|
45 | 49 | @app.get("/logout") |
46 | 50 | async def root(email: str): |
47 | 51 | return {"message": "Logged out %s!" % email} |
| 52 | + |
| 53 | +@app.get("/attachment") |
| 54 | +async def attachment(attachment_name: str): |
| 55 | + attachment_path = 'attachments/' + attachment_name |
| 56 | + if not isfile(attachment_path): |
| 57 | + raise HTTPException(status_code=404, detail="Attachment not found") |
| 58 | + |
| 59 | + with open(attachment_path) as f: |
| 60 | + return f.readlines() |
| 61 | + |
| 62 | +@app.post("/job") |
| 63 | +async def job(job_name: str): |
| 64 | + job_path = 'jobs/' + job_name |
| 65 | + if not isfile(job_path): |
| 66 | + raise HTTPException(status_code=404, detail="Job not found") |
| 67 | + |
| 68 | + exec(open(job_path).read()) |
| 69 | + |
0 commit comments