Skip to content

Commit

Permalink
cors
Browse files Browse the repository at this point in the history
  • Loading branch information
rohittp0 committed Jun 4, 2023
1 parent 2d6c3e0 commit f19eda5
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 41 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -123,3 +123,7 @@ dmypy.json
# Pyre type checker
.pyre/
.idea

gramup/anon.session
anon.session
gramup/anon.session-journal
62 changes: 23 additions & 39 deletions gramup/main.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
import asyncio

from fastapi import FastAPI
from fastapi.responses import RedirectResponse
from starlette.requests import Request
from starlette.responses import FileResponse
from fastapi import FastAPI, HTTPException
from starlette import status
from starlette.middleware.cors import CORSMiddleware
from starlette.staticfiles import StaticFiles
from starlette.templating import Jinja2Templates
from starlette.websockets import WebSocket
from telethon import TelegramClient

Expand All @@ -14,17 +12,25 @@
client = TelegramClient('anon', API_ID, API_HASH)
app = FastAPI()

app.mount("/static", StaticFiles(directory="static"), name="static")
app.add_middleware(
CORSMiddleware,
allow_origins=["*"], # Allow all origins
allow_credentials=True,
allow_methods=["*"], # Allow all methods
allow_headers=["*"], # Allow all headers
)

icons_path = "static/icons"

templates = Jinja2Templates(directory="templates")


@app.websocket("/ws")
@app.websocket("/api/ws/")
async def websocket_endpoint(websocket: WebSocket):
await websocket.accept()

if not client.is_connected():
await client.connect()

if await client.is_user_authorized():
return await websocket.send_json({"type": "connection", "status": "connected"})

qr = await client.qr_login()
await websocket.send_json({"url": qr.url, "type": "qr"})

Expand All @@ -41,34 +47,12 @@ async def websocket_endpoint(websocket: WebSocket):
await websocket.close()


@app.get("/sw.js")
def get_sw():
return FileResponse("static/js/sw.js", media_type="text/javascript")


@app.get("/manifest.json")
def get_manifest():
return FileResponse("static/manifest.json", media_type="application/json")


@app.get("/login")
async def login(request: Request):
if not client.is_connected():
await client.connect()

if await client.is_user_authorized():
return RedirectResponse(url="/", status_code=302)

context = {"request": request}

return templates.TemplateResponse("login.html", context=context)


@app.get("/{folder:path}")
async def files(request: Request, folder="", error=""):
@app.get("/api/files/")
async def files(path=""):
if not client.is_connected() or not await client.is_user_authorized():
return RedirectResponse(url="/login", status_code=302)
raise HTTPException(status_code=status.HTTP_401_UNAUTHORIZED, detail="User not authorized")

return []

context = {}

return templates.TemplateResponse("files.html", context=context)
app.mount("/", StaticFiles(directory="static"), name="static")
3 changes: 1 addition & 2 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
shortuuid~=1.0.11
fastapi~=0.95.2
fastapi==0.96.0
starlette~=0.27.0
Telethon~=1.28.5

0 comments on commit f19eda5

Please sign in to comment.