Skip to content

Commit

Permalink
refactor: Separate FastAPI routes from app
Browse files Browse the repository at this point in the history
  • Loading branch information
essteer committed Jul 3, 2024
1 parent 3b0fa0c commit 7368569
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 27 deletions.
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,4 @@ EXPOSE 8000
RUN addgroup -S appgroup && adduser -S appuser -G appgroup
USER appuser

CMD [ "fastapi", "run", "./masquer_api/main.py" ]
CMD [ "fastapi", "run", "./api/main.py" ]
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ To self-host the API, install the `FastAPI` optional dependency as declared in t
Activate the API from the root directory via:

```console
$ fastapi run src/masquer_api/main.py
$ fastapi run src/api/main.py
```

Then follow the instructions provided by FastAPI in the terminal.
Expand Down
File renamed without changes.
54 changes: 29 additions & 25 deletions src/masquer_api/main.py → src/api/main.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
from typing import Union
from fastapi import FastAPI
from fastapi.responses import JSONResponse
from masquer import masq
from masquer.__about__ import __version__
from .routes import router


DESCRIPTION = """
Expand All @@ -15,25 +13,31 @@
A basic header template with common attributes — like [`"Upgrade-Insecure-Requests": "1"`](https://stackoverflow.com/questions/31950470/what-is-the-upgrade-insecure-requests-http-header/32003517#32003517) — is also provided and defaults to the most common referer and user-agent data from the above lists.
"""

VERSION = __version__

app = FastAPI(
title="Masquer API",
summary="A tool to generate random user-agent and referer data for GET requests.",
description=DESCRIPTION,
version=VERSION,
license_info={
"name": "MIT License",
"url": "https://github.com/essteer/masquer/blob/main/LICENSE",
},
)


@app.get("/masq")
def get_masq(
ua: Union[bool, None] = True,
rf: Union[bool, None] = False,
hd: Union[bool, None] = False,
):
response = masq(ua, rf, hd)
return JSONResponse(content=response)

def get_app() -> FastAPI:
"""
Create a FastAPI app with the specified attributes
"""
app = FastAPI(
title="Masquer API",
summary="A tool to generate random user-agent and referer data for GET requests.",
description=DESCRIPTION,
version=__version__,
license_info={
"name": "MIT License",
"url": "https://github.com/essteer/masquer/blob/main/LICENSE",
},
)
# Add routes
app.include_router(router)

return app


app = get_app()


if __name__ == "__main__":
import uvicorn

uvicorn.run(app, host="127.0.0.1", port=8000)
17 changes: 17 additions & 0 deletions src/api/routes.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
from typing import Union
from fastapi import APIRouter
from fastapi.responses import JSONResponse
from masquer import masq


router = APIRouter()


@router.get("/masq")
def get_masq(
ua: Union[bool, None] = True,
rf: Union[bool, None] = False,
hd: Union[bool, None] = False,
):
response = masq(ua, rf, hd)
return JSONResponse(content=response)

0 comments on commit 7368569

Please sign in to comment.