-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
67 lines (56 loc) · 2.51 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
from fastapi import FastAPI, Header, Response, status, Request, File, UploadFile
import aiomysql
import asyncio
from typing import *
from fastapi.responses import RedirectResponse
from dotenv import load_dotenv
import os
load_dotenv()
host = os.environ.get("HOST")
port = int(os.environ.get("PORT"))
db = os.environ.get("DATABASE")
user = os.environ.get("USER")
password = os.environ.get("PASSWORD")
app = FastAPI()
@app.get("/")
async def main(request: Request, response: Response):
response = RedirectResponse(url='/docs')
return response
@app.get("/files")
async def files(request:Request, response: Response, authorization: str = Header(None)):
print(request.headers)
con = await aiomysql.connect(autocommit=True, host=host, port=port, db=db, user=user, password=password)
c = await con.cursor()
await c.execute("SELECT * from apikeys WHERE apikey='"+ str(authorization)+"'")
keys = await c.fetchall()
if len(keys) == 0:
response.status_code = status.HTTP_401_UNAUTHORIZED
return {"error": True, "message": "Invalid token has been passed"}
await c.execute("SELECT * FROM files")
fls = await c.fetchall()
return {"message": fls}
@app.post("/upload")
async def upload(request: Request, response: Response, authorization: str = Header(None), file: str = Header(None), name: str = Header(None)):
print(request.headers)
con = await aiomysql.connect(autocommit=True, host=host, port=port, db=db, user=user, password=password)
c = await con.cursor()
await c.execute("SELECT * from apikeys WHERE apikey='" + str(authorization)+"'")
keys = await c.fetchall()
if len(keys) == 0:
response.status_code = status.HTTP_401_UNAUTHORIZED
return {"error": True, "message": "Invalid token has been passed"}
await c.execute("INSERT into files (name, url) VALUES ('"+str(name)+"', '"+str(file)+"')")
return len(file)
@app.get("/tags")
async def files(request: Request, response: Response, authorization: str = Header(None)):
print(request.headers)
con = await aiomysql.connect(autocommit=True, host=host, port=port, db=db, user=user, password=password)
c = await con.cursor()
await c.execute("SELECT * from apikeys WHERE apikey='" + str(authorization)+"'")
keys = await c.fetchall()
if len(keys) == 0:
response.status_code = status.HTTP_401_UNAUTHORIZED
return {"error": True, "message": "Invalid token has been passed"}
await c.execute("SELECT * FROM tags")
fls = await c.fetchall()
return {"message": fls}