-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
72 lines (60 loc) · 2.26 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
68
69
70
71
72
from distutils.log import debug
from fastapi import FastAPI, File, UploadFile, WebSocket
from starlette.middleware.cors import CORSMiddleware
from db2 import account
from chat import ConnectionManager
from models import *
import uvicorn
import os
mdb=account()
manager = ConnectionManager()
app = FastAPI()
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
@app.websocket("/ws/{userid}")
async def websocket_endpoint(websocket: WebSocket, userid: str):
await manager.connect(websocket,userid)
await manager.broadcast({'event':'online','userid':userid})
try:
while True:
data = await websocket.receive_json()
await manager.broadcast(data)
except Exception as e:
manager.disconnect(userid)
await manager.broadcast({'event':'offline','userid':userid})
@app.post("/signup", response_model=LoginSignUPResponse)
async def signup(data: LoginSignUP):
username,userid = await mdb.signup(data.username,data.password)
if username != None or userid != None:
return {"status":1,"username":username,"userid":userid}
else:return {"status":0}
@app.post("/login", response_model=LoginSignUPResponse)
async def login(data: LoginSignUP):
username,userid=await mdb.login(data.username,data.password)
if username != None or userid != None:
return {"status":1,"username":username,"userid":userid}
else:return {"status":0}
@app.get("/getavatar/{userid}")
async def getavatar(userid: str):
idata=await mdb.getavatar(userid)
if idata != None:
return {"status":1,"data":idata}
else:return {"status":0}
@app.post("/postavatar/{userid}", response_model=PostAvatarResponse)
async def postavatar(userid: str,img: UploadFile = File(...)):
byt=await img.read()
await mdb.postavatar(byt,userid)
return {"status":1}
@app.get("/getusers", response_model=GetUsersResponse)
async def getusers():
idata = await manager.get_active_users()
return {"status":1,"data":idata}
if __name__ == "__main__":
port = int(os.environ.get('PORT', 5000))
print(port)
uvicorn.run("main:app", host="0.0.0.0", port=port, workers=1, loop='asyncio', http='httptools', log_level="warning")