Skip to content

Commit

Permalink
refactor: extract repeated logic (#21)
Browse files Browse the repository at this point in the history
* extract API content appending logic into a separate function

* rename home function to root for clarity

* update project name, version, and description

* change to packaged application
  • Loading branch information
jljl1337 authored Dec 21, 2024
1 parent 254d4af commit 2b66c4f
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 41 deletions.
5 changes: 5 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
.devcontainer
.dockerignore
.github
.git
.gitignore
20 changes: 12 additions & 8 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# Base image
FROM python:3.13-slim AS base

FROM base AS dependencies
# Builder image
FROM base AS builder

# Install uv
COPY --from=ghcr.io/astral-sh/uv:latest /uv /uvx /bin/
Expand All @@ -14,14 +16,16 @@ RUN --mount=type=cache,target=/root/.cache/uv \
--mount=type=bind,source=pyproject.toml,target=pyproject.toml \
uv sync --frozen --no-install-project --no-editable --compile-bytecode

FROM base AS runner
WORKDIR /app
# Install the application
ADD . /app
RUN --mount=type=cache,target=/root/.cache/uv \
uv sync --frozen --no-editable --compile-bytecode

# Copy the environment
COPY --from=dependencies --chown=app:app /app/.venv /app/.venv
# Runner image
FROM base AS runner

# Copy the application
COPY ./main.py /app
# Copy the environment, but not the source code
COPY --from=builder --chown=app:app /app/.venv /app/.venv

# Run the application
CMD [".venv/bin/python", "main.py"]
CMD ["/app/.venv/bin/main"]
13 changes: 10 additions & 3 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
[project]
name = "app"
version = "0.1.0"
description = "Add your description here"
name = "testpod"
version = "0.1.1"
description = "Simple containerized Flask app for orchestrator testing"
readme = "README.md"
requires-python = ">=3.13"
dependencies = [
"flask>=3.1.0",
"requests>=2.32.3",
"waitress>=3.0.2",
]

[build-system]
requires = ["hatchling"]
build-backend = "hatchling.build"

[project.scripts]
main = "testpod.main:main"
Empty file added src/testpod/__init__.py
Empty file.
24 changes: 11 additions & 13 deletions main.py → src/testpod/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,29 +10,27 @@
HOSTNAME = socket.gethostname()
API_URL = os.environ.get('API_URL')

@app.route('/')
def home():
html = f'<h1>Hello, World from {HOSTNAME}!</h1>'

def append_api_content(html: str) -> str:
if API_URL:
api_content = requests.get(API_URL).content.decode('utf-8')
html = f'{html}{api_content}'

return html

@app.route('/')
def root():
return append_api_content(f'<h1>Hello, World from {HOSTNAME}!</h1>')

@app.route('/api')
def api():
html = f'<p>Calling {HOSTNAME}</p>'
return append_api_content(f'<p>Calling {HOSTNAME}</p>')

if API_URL:
api_content = requests.get(API_URL).content.decode('utf-8')
html = f'{html}{api_content}'

return html

if __name__ == '__main__':
def main():
host = os.environ.get('HOST', '0.0.0.0')
port = os.environ.get('PORT', 8000)

print(f'Starting server on {host}:{port}')
serve(app, host=host, port=port)
serve(app, host=host, port=port)

if __name__ == '__main__':
main()
34 changes: 17 additions & 17 deletions uv.lock

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

0 comments on commit 2b66c4f

Please sign in to comment.