Skip to content

Commit

Permalink
feat: hide open API in prod
Browse files Browse the repository at this point in the history
  • Loading branch information
pmareke committed Sep 24, 2024
1 parent 793e39c commit f6777d1
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 6 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ add-package: ## Installs a new package in the app. ex: make install package=XXX

.PHONY: run
run: ## Runs the app in production mode
poetry run fastapi run
OPENAPI_URL= poetry run fastapi run

.PHONY: dev
dev: ## Runs the app in development mode
Expand Down
14 changes: 10 additions & 4 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,16 @@
from fastapi import FastAPI
from src.common import config
from src.common.logger import setup_logging
from src.common.settings import Settings
from src.delivery.api.v1.health.health_router import health
from src.delivery.api.v1.hello.hello_router import hello
from time import sleep


logger = logging.getLogger(__name__)


@asynccontextmanager
async def lifespan(_app: FastAPI) -> AsyncGenerator:
setup_logging()
logger = logging.getLogger(__name__)
logger.info("Starting FastAPI server...")

yield
Expand All @@ -25,7 +24,14 @@ async def lifespan(_app: FastAPI) -> AsyncGenerator:
logger.info("FastAPI server finished!")


app = FastAPI(title=config.PROJECT_NAME, lifespan=lifespan)
settings = Settings()

app = FastAPI(
title=config.PROJECT_NAME,
description="This is the documentation of the FastAPI template project.",
lifespan=lifespan,
openapi_url=settings.openapi_url,
)

app.include_router(prefix=config.API_V1_PREFIX, router=health)
app.include_router(prefix=config.API_V1_PREFIX, router=hello)
22 changes: 21 additions & 1 deletion poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ readme = "README.md"
[tool.poetry.dependencies]
python = "^3.12"
fastapi = {extras = ["standard"], version = "^0.115.0"}
pydantic-settings = "^2.5.2"

[tool.poetry.group.test.dependencies]
pytest = "^8.3.3"
Expand Down
5 changes: 5 additions & 0 deletions src/common/settings.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from pydantic_settings import BaseSettings


class Settings(BaseSettings):
openapi_url: str = "/openapi.json"

0 comments on commit f6777d1

Please sign in to comment.