From e263b88170be706fbb29c4d5351581d84e813fc9 Mon Sep 17 00:00:00 2001 From: Kanishk Pachauri Date: Thu, 4 Jan 2024 05:52:45 +0530 Subject: [PATCH 1/2] :memo: Fix: removed /docs url from prod --- src/paste/__init__.py | 4 ++++ src/paste/main.py | 45 ++++++++++++++++++++++--------------------- 2 files changed, 27 insertions(+), 22 deletions(-) diff --git a/src/paste/__init__.py b/src/paste/__init__.py index e69de29..bc06ece 100644 --- a/src/paste/__init__.py +++ b/src/paste/__init__.py @@ -0,0 +1,4 @@ +__version__: str = "2.0.0" +__author__: str = "fosscu" +__contact__: str = "fosscu@gmail.com" +__url__: str = "https://fosscu.org" diff --git a/src/paste/main.py b/src/paste/main.py index f61a411..38777d1 100644 --- a/src/paste/main.py +++ b/src/paste/main.py @@ -31,9 +31,24 @@ from pygments.formatters import HtmlFormatter from pygments.util import ClassNotFound from typing import List, Optional +from . import __version__, __author__, __contact__, __url__ + +description: str = "paste.py 🐍 - A pastebin written in python." limiter = Limiter(key_func=get_remote_address) -app: FastAPI = FastAPI(title="paste.py 🐍") +app: FastAPI = FastAPI( + title="paste.py 🐍", + version=__version__, + contact=dict( + name=__author__, + url=__url__, + email=__contact__, + ), + license_info=dict(name="MIT", url="https://opensource.org/license/mit/"), + openapi_url=None, + docs_url=None, + redoc_url=None, +) app.state.limiter = limiter app.add_exception_handler(RateLimitExceeded, _rate_limit_exceeded_handler) @@ -60,9 +75,7 @@ @app.post("/file") @limiter.limit("100/minute") -async def post_as_a_file( - request: Request, file: UploadFile = File(...) -) -> PlainTextResponse: +async def post_as_a_file(request: Request, file: UploadFile = File(...)) -> PlainTextResponse: try: uuid: str = generate_uuid() if uuid in large_uuid_storage: @@ -91,9 +104,7 @@ async def post_as_a_file( @app.get("/paste/{uuid}") -async def get_paste_data( - uuid: str, user_agent: Optional[str] = Header(None) -) -> Response: +async def get_paste_data(uuid: str, user_agent: Optional[str] = Header(None)) -> Response: path: str = f"data/{uuid}" try: with open(path, "rb") as f: @@ -116,9 +127,7 @@ async def get_paste_data( lexer = get_lexer_by_name(file_extension, stripall=True) except ClassNotFound: lexer = get_lexer_by_name("text", stripall=True) # Default lexer - formatter = HtmlFormatter( - style="colorful", full=True, linenos="inline", cssclass="code" - ) + formatter = HtmlFormatter(style="colorful", full=True, linenos="inline", cssclass="code") highlighted_code: str = highlight(content, lexer, formatter) custom_style = """ .code pre span.linenos { @@ -190,13 +199,9 @@ async def delete_paste(uuid: str) -> PlainTextResponse: os.remove(path) return PlainTextResponse(f"File successfully deleted {uuid}") except FileNotFoundError: - raise HTTPException( - detail="File Not Found", status_code=status.HTTP_404_NOT_FOUND - ) + raise HTTPException(detail="File Not Found", status_code=status.HTTP_404_NOT_FOUND) except Exception as e: - raise HTTPException( - detail=f"The exception is {e}", status_code=status.HTTP_409_CONFLICT - ) + raise HTTPException(detail=f"The exception is {e}", status_code=status.HTTP_409_CONFLICT) @app.get("/web", response_class=HTMLResponse) @@ -206,9 +211,7 @@ async def web(request: Request) -> Response: @app.post("/web", response_class=PlainTextResponse) @limiter.limit("100/minute") -async def web_post( - request: Request, content: str = Form(...), extension: Optional[str] = Form(None) -) -> RedirectResponse: +async def web_post(request: Request, content: str = Form(...), extension: Optional[str] = Form(None)) -> RedirectResponse: try: file_content: bytes = content.encode() uuid: str = generate_uuid() @@ -228,9 +231,7 @@ async def web_post( status_code=status.HTTP_403_FORBIDDEN, ) - return RedirectResponse( - f"{BASE_URL}/paste/{uuid_}", status_code=status.HTTP_303_SEE_OTHER - ) + return RedirectResponse(f"{BASE_URL}/paste/{uuid_}", status_code=status.HTTP_303_SEE_OTHER) @app.get("/health", status_code=status.HTTP_200_OK) From 69ee68c4ccd8e08597c565be953ac9bc8a2cfa6a Mon Sep 17 00:00:00 2001 From: Kanishk Pachauri Date: Thu, 4 Jan 2024 05:53:16 +0530 Subject: [PATCH 2/2] :test_tube: Docs: added test action in pdm --- pyproject.toml | 1 + 1 file changed, 1 insertion(+) diff --git a/pyproject.toml b/pyproject.toml index 8f9c709..ed82110 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -19,6 +19,7 @@ license = {text = "MIT"} [tool.pdm.scripts] start = "uvicorn src.paste.main:app --host 0.0.0.0 --port 8080 --workers 4" +dev = "uvicorn src.paste.main:app --host 0.0.0.0 --port 8080 --reload" test = "pytest" mypy = "mypy src/paste"