Update app.py
Browse files
app.py
CHANGED
|
@@ -1,82 +1,69 @@
|
|
| 1 |
-
from fastapi import FastAPI, Request, Form
|
| 2 |
from fastapi.responses import HTMLResponse, RedirectResponse
|
| 3 |
-
from fastapi.staticfiles import StaticFiles
|
| 4 |
import sqlite3
|
| 5 |
-
import os
|
| 6 |
-
from datetime import datetime
|
| 7 |
|
| 8 |
app = FastAPI()
|
| 9 |
|
| 10 |
-
# create folders
|
| 11 |
-
os.makedirs("uploads", exist_ok=True)
|
| 12 |
-
|
| 13 |
-
# database
|
| 14 |
conn = sqlite3.connect("chat.db", check_same_thread=False)
|
| 15 |
c = conn.cursor()
|
| 16 |
|
| 17 |
-
c.execute("""
|
| 18 |
-
|
|
|
|
| 19 |
password TEXT
|
| 20 |
-
)
|
|
|
|
| 21 |
|
| 22 |
-
c.execute("""
|
| 23 |
-
|
| 24 |
username TEXT,
|
| 25 |
-
message TEXT
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
)""")
|
| 29 |
|
| 30 |
conn.commit()
|
| 31 |
|
| 32 |
-
app.mount("/uploads", StaticFiles(directory="uploads"), name="uploads")
|
| 33 |
|
| 34 |
-
# homepage
|
| 35 |
@app.get("/", response_class=HTMLResponse)
|
| 36 |
def home():
|
| 37 |
return """
|
| 38 |
<h1>Emalawi19 Chat</h1>
|
| 39 |
-
<a href=
|
| 40 |
-
<a href=
|
| 41 |
"""
|
| 42 |
|
| 43 |
-
|
| 44 |
@app.get("/register", response_class=HTMLResponse)
|
| 45 |
-
def
|
| 46 |
return """
|
| 47 |
<h2>Register</h2>
|
| 48 |
-
<form
|
| 49 |
-
|
| 50 |
-
<input name="
|
| 51 |
-
Password:<br>
|
| 52 |
-
<input type="password" name="password"><br><br>
|
| 53 |
<button type="submit">Register</button>
|
| 54 |
</form>
|
| 55 |
"""
|
| 56 |
|
|
|
|
| 57 |
@app.post("/register")
|
| 58 |
def register(username: str = Form(...), password: str = Form(...)):
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
conn.commit()
|
| 62 |
-
except:
|
| 63 |
-
pass
|
| 64 |
return RedirectResponse("/login", status_code=302)
|
| 65 |
|
| 66 |
-
|
| 67 |
@app.get("/login", response_class=HTMLResponse)
|
| 68 |
-
def
|
| 69 |
return """
|
| 70 |
<h2>Login</h2>
|
| 71 |
-
<form
|
| 72 |
-
Username:<br>
|
| 73 |
<input name="username"><br>
|
| 74 |
-
|
| 75 |
-
<input type="password" name="password"><br><br>
|
| 76 |
<button type="submit">Login</button>
|
| 77 |
</form>
|
| 78 |
"""
|
| 79 |
|
|
|
|
| 80 |
@app.post("/login")
|
| 81 |
def login(username: str = Form(...), password: str = Form(...)):
|
| 82 |
user = c.execute(
|
|
@@ -87,77 +74,39 @@ def login(username: str = Form(...), password: str = Form(...)):
|
|
| 87 |
if user:
|
| 88 |
return RedirectResponse(f"/chat/{username}", status_code=302)
|
| 89 |
|
| 90 |
-
return
|
|
|
|
| 91 |
|
| 92 |
-
# chat page
|
| 93 |
@app.get("/chat/{username}", response_class=HTMLResponse)
|
| 94 |
def chat(username: str):
|
| 95 |
|
| 96 |
-
|
| 97 |
-
"SELECT username,message,image,time FROM messages"
|
| 98 |
-
).fetchall()
|
| 99 |
|
| 100 |
chat_html = ""
|
| 101 |
|
| 102 |
-
for u,m
|
| 103 |
-
|
| 104 |
-
if m:
|
| 105 |
-
chat_html += f"<p><b>{u}</b>: {m} ({t})</p>"
|
| 106 |
-
|
| 107 |
-
if i:
|
| 108 |
-
chat_html += f"<p><b>{u}</b>:<br><img src='/uploads/{i}' width='200'></p>"
|
| 109 |
|
| 110 |
return f"""
|
| 111 |
-
<html>
|
| 112 |
-
<head>
|
| 113 |
-
<meta http-equiv="refresh" content="3">
|
| 114 |
-
</head>
|
| 115 |
-
|
| 116 |
-
<body>
|
| 117 |
-
|
| 118 |
<h2>Emalawi19 Chat - {username}</h2>
|
| 119 |
|
| 120 |
-
<div
|
| 121 |
-
{chat_html}
|
| 122 |
-
</div>
|
| 123 |
-
|
| 124 |
-
<br>
|
| 125 |
-
|
| 126 |
-
<form action="/send/{username}" method="post" enctype="multipart/form-data">
|
| 127 |
-
|
| 128 |
-
<input name="message" placeholder="Type message">
|
| 129 |
-
<input type="file" name="image">
|
| 130 |
-
|
| 131 |
-
<button type="submit">Send</button>
|
| 132 |
|
|
|
|
|
|
|
|
|
|
| 133 |
</form>
|
| 134 |
-
|
| 135 |
-
</body>
|
| 136 |
-
</html>
|
| 137 |
"""
|
| 138 |
|
| 139 |
-
# send message
|
| 140 |
-
@app.post("/send/{username}")
|
| 141 |
-
async def send(username: str,
|
| 142 |
-
message: str = Form(""),
|
| 143 |
-
image: UploadFile = File(None)):
|
| 144 |
-
|
| 145 |
-
filename = None
|
| 146 |
-
|
| 147 |
-
if image:
|
| 148 |
-
filename = image.filename
|
| 149 |
-
path = f"uploads/{filename}"
|
| 150 |
-
|
| 151 |
-
with open(path,"wb") as f:
|
| 152 |
-
f.write(await image.read())
|
| 153 |
-
|
| 154 |
-
time = datetime.now().strftime("%H:%M")
|
| 155 |
-
|
| 156 |
-
c.execute(
|
| 157 |
-
"INSERT INTO messages(username,message,image,time) VALUES(?,?,?,?)",
|
| 158 |
-
(username,message,filename,time)
|
| 159 |
-
)
|
| 160 |
|
|
|
|
|
|
|
|
|
|
| 161 |
conn.commit()
|
|
|
|
|
|
|
| 162 |
|
| 163 |
-
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import FastAPI, Request, Form
|
| 2 |
from fastapi.responses import HTMLResponse, RedirectResponse
|
|
|
|
| 3 |
import sqlite3
|
|
|
|
|
|
|
| 4 |
|
| 5 |
app = FastAPI()
|
| 6 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
conn = sqlite3.connect("chat.db", check_same_thread=False)
|
| 8 |
c = conn.cursor()
|
| 9 |
|
| 10 |
+
c.execute("""
|
| 11 |
+
CREATE TABLE IF NOT EXISTS users(
|
| 12 |
+
username TEXT,
|
| 13 |
password TEXT
|
| 14 |
+
)
|
| 15 |
+
""")
|
| 16 |
|
| 17 |
+
c.execute("""
|
| 18 |
+
CREATE TABLE IF NOT EXISTS messages(
|
| 19 |
username TEXT,
|
| 20 |
+
message TEXT
|
| 21 |
+
)
|
| 22 |
+
""")
|
|
|
|
| 23 |
|
| 24 |
conn.commit()
|
| 25 |
|
|
|
|
| 26 |
|
|
|
|
| 27 |
@app.get("/", response_class=HTMLResponse)
|
| 28 |
def home():
|
| 29 |
return """
|
| 30 |
<h1>Emalawi19 Chat</h1>
|
| 31 |
+
<a href='/login'>Login</a><br>
|
| 32 |
+
<a href='/register'>Register</a>
|
| 33 |
"""
|
| 34 |
|
| 35 |
+
|
| 36 |
@app.get("/register", response_class=HTMLResponse)
|
| 37 |
+
def register_page():
|
| 38 |
return """
|
| 39 |
<h2>Register</h2>
|
| 40 |
+
<form method="post">
|
| 41 |
+
<input name="username" placeholder="username"><br>
|
| 42 |
+
<input name="password" type="password" placeholder="password"><br>
|
|
|
|
|
|
|
| 43 |
<button type="submit">Register</button>
|
| 44 |
</form>
|
| 45 |
"""
|
| 46 |
|
| 47 |
+
|
| 48 |
@app.post("/register")
|
| 49 |
def register(username: str = Form(...), password: str = Form(...)):
|
| 50 |
+
c.execute("INSERT INTO users VALUES(?,?)",(username,password))
|
| 51 |
+
conn.commit()
|
|
|
|
|
|
|
|
|
|
| 52 |
return RedirectResponse("/login", status_code=302)
|
| 53 |
|
| 54 |
+
|
| 55 |
@app.get("/login", response_class=HTMLResponse)
|
| 56 |
+
def login_page():
|
| 57 |
return """
|
| 58 |
<h2>Login</h2>
|
| 59 |
+
<form method="post">
|
|
|
|
| 60 |
<input name="username"><br>
|
| 61 |
+
<input name="password" type="password"><br>
|
|
|
|
| 62 |
<button type="submit">Login</button>
|
| 63 |
</form>
|
| 64 |
"""
|
| 65 |
|
| 66 |
+
|
| 67 |
@app.post("/login")
|
| 68 |
def login(username: str = Form(...), password: str = Form(...)):
|
| 69 |
user = c.execute(
|
|
|
|
| 74 |
if user:
|
| 75 |
return RedirectResponse(f"/chat/{username}", status_code=302)
|
| 76 |
|
| 77 |
+
return "Login failed"
|
| 78 |
+
|
| 79 |
|
|
|
|
| 80 |
@app.get("/chat/{username}", response_class=HTMLResponse)
|
| 81 |
def chat(username: str):
|
| 82 |
|
| 83 |
+
msgs = c.execute("SELECT username,message FROM messages").fetchall()
|
|
|
|
|
|
|
| 84 |
|
| 85 |
chat_html = ""
|
| 86 |
|
| 87 |
+
for u,m in msgs:
|
| 88 |
+
chat_html += f"<p><b>{u}</b>: {m}</p>"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 89 |
|
| 90 |
return f"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 91 |
<h2>Emalawi19 Chat - {username}</h2>
|
| 92 |
|
| 93 |
+
<div>{chat_html}</div>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 94 |
|
| 95 |
+
<form action="/send/{username}" method="post">
|
| 96 |
+
<input name="message">
|
| 97 |
+
<button>Send</button>
|
| 98 |
</form>
|
|
|
|
|
|
|
|
|
|
| 99 |
"""
|
| 100 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 101 |
|
| 102 |
+
@app.post("/send/{username}")
|
| 103 |
+
def send(username: str, message: str = Form(...)):
|
| 104 |
+
c.execute("INSERT INTO messages VALUES(?,?)",(username,message))
|
| 105 |
conn.commit()
|
| 106 |
+
return RedirectResponse(f"/chat/{username}", status_code=302)
|
| 107 |
+
|
| 108 |
|
| 109 |
+
# IMPORTANT: start server
|
| 110 |
+
if __name__ == "__main__":
|
| 111 |
+
import uvicorn
|
| 112 |
+
uvicorn.run(app, host="0.0.0.0", port=7860)
|