-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
123 lines (100 loc) · 4.01 KB
/
main.py
File metadata and controls
123 lines (100 loc) · 4.01 KB
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
from fastapi import FastAPI, WebSocket, Request, Depends
from fastapi.responses import HTMLResponse
from fastapi.templating import Jinja2Templates
from fastapi.logger import logger
from fastapi.staticfiles import StaticFiles
from sqlalchemy.orm import Session
from typing import List
from fastapi.security import OAuth2PasswordBearer, OAuth2PasswordRequestForm
from schema import ChatRequest, ChatRequestCreate, UserCreate, UserLogin, FriendRequest
from crud import get_chatlist, set_chatlist, get_user, create_user, authenticate_user, add_friend, get_friend, is_friend
from models import Base, ChatList, LoginList, FriendList
from database import SessionLocal, engine
Base.metadata.create_all(bind=engine)
app = FastAPI()
oauth2_scheme = OAuth2PasswordBearer(tokenUrl="token")
def get_db():
db = SessionLocal()
try:
yield db
finally:
db.close()
app.mount("/static", StaticFiles(directory="static"), name="static")
templates =Jinja2Templates(directory="templates")
class ConnectionManager:
def __init__(self):
self.active_connections=[]
async def connect(self, websocket: WebSocket):
await websocket.accept()
self.active_connections.append(websocket)
async def disconnect(self, websocket: WebSocket):
self.active_connections.remove(websocket)
async def broadcast(self, message: str):
for connection in self.active_connections:
await connection.send_text(message)
manager = ConnectionManager()
@app.websocket("/ws")
async def websocket_endpoint(websocket: WebSocket):
await manager.connect(websocket)
try:
while True:
data = await websocket.receive_text()
await manager.broadcast(f"{data}")
except Exception as e:
pass
finally:
await manager.disconnect(websocket)
@app.get("/")
async def client(request: Request):
return templates.TemplateResponse("login.html", {"request": request})
@app.get("/chat")
async def client(request: Request):
return templates.TemplateResponse("chat.html", {"request": request})
@app.get("/groupChat")
async def client(request: Request):
return templates.TemplateResponse("groupChat.html", {"request": request})
@app.get("/friendList")
async def client(request: Request):
return templates.TemplateResponse("friendList.html", {"request": request})
@app.get("/chatList")
async def client(request: Request):
return templates.TemplateResponse("chatList.html", {"request": request})
@app.get("/getchatlist", response_model=List[ChatRequest])
def get_data(db: Session = Depends(get_db)):
return get_chatlist(db)
@app.post("/setchatlist", response_model=List[ChatRequest])
def set_data(chat_req: ChatRequestCreate, db: Session = Depends(get_db)):
return set_chatlist(db, chat_req)
@app.post("/register")
def register_user(user: UserCreate, db: Session = Depends(get_db)):
isUser = get_user(db, user.userID)
if isUser is None: # Only when that user is not registered
db_user = create_user(db, user.userID, user.password)
return db_user
@app.post("/token")
def login_user(
form_data: OAuth2PasswordRequestForm = Depends(), db: Session = Depends(get_db)
):
# print('Form data:', form_data)
user = authenticate_user(db, form_data.username, form_data.password)
if user is None:
return {"access_token": "-1", "token_type": "bearer", "isFailed": "1"}
else:
return {"access_token": user.userID, "token_type": "bearer", "isFailed": "0"}
@app.post("/addFriend")
def register_friend(friend: FriendRequest, db: Session = Depends(get_db)):
isFriend = is_friend(db, friend.userID, friend.friend)
if isFriend is None:
db_friend = add_friend(db, friend.userID, friend.friend)
return db_friend
else:
return -1;
@app.post("/getFriend")
def retrieve_friend(friend: FriendRequest, db: Session = Depends(get_db)):
friendList = get_friend(db, friend.userID)
return friendList
def run():
import uvicorn
uvicorn.run(app)
if __name__=="__main__":
run()