-
Notifications
You must be signed in to change notification settings - Fork 3
/
app.py
81 lines (64 loc) · 1.98 KB
/
app.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
import re
from fastapi import FastAPI, Response, status
from fastapi.middleware.cors import CORSMiddleware
from starlette.responses import RedirectResponse
from encryptMessage import Encryptor
from pydantic import BaseModel
app = FastAPI(title="The encryptii api")
enc = Encryptor()
origins = [
"http://127.0.0.1",
"http://localhost:3000",
"https://.*"
]
app.add_middleware(
CORSMiddleware,
allow_origins=origins,
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
class encryptINP(BaseModel):
msg: str
auto_destroy: bool = False
emoji: bool = False
class decryptINP(BaseModel):
dec: str
destroy: bool = True
@app.get("/", status_code=status.HTTP_307_TEMPORARY_REDIRECT)
async def routePage():
response = RedirectResponse(url="/docs")
return response
@app.get("/ping")
def pingpong():
return {"ping": "pong"}
@app.post("/encrypt", status_code=status.HTTP_201_CREATED)
async def encryption(pmt: encryptINP):
msg = pmt.msg
encrypted = enc.encrypt(msg, pmt.emoji)
resp = {"encrypted": encrypted}
return resp
@app.get("/encrypt", status_code=status.HTTP_201_CREATED)
async def getEncryption(msg: str):
encrypted = enc.encrypt(msg)
resp = {"encrypted": encrypted}
return resp
@app.post("/decrypt", status_code=status.HTTP_200_OK)
async def decryption(pmt: decryptINP, response: Response):
dec = pmt.dec.encode()
des = pmt.destroy
d = enc.decrypt(dec, des)
if d == -1:
response.status_code = status.HTTP_404_NOT_FOUND
return {"msg": "Message does not exist or was destroyed"}
else:
return {"msg": str(d)}
@app.get("/decrypt", status_code=status.HTTP_200_OK)
async def getDecryption(dec: str, response: Response, des: bool = True):
dec = dec.encode()
d = enc.decrypt(dec, des)
if d == -1:
response.status_code = status.HTTP_404_NOT_FOUND
return {"msg": "Message does not exist or was destroyed"}
else:
return {"msg": str(d)}