Skip to content

Commit

Permalink
Updated example to include two services
Browse files Browse the repository at this point in the history
  • Loading branch information
ulises-jeremias committed Dec 4, 2023
1 parent bd72ea1 commit ab4e046
Show file tree
Hide file tree
Showing 92 changed files with 440 additions and 12,577 deletions.
30 changes: 6 additions & 24 deletions examples/kubernetes-tilt-dev/Tiltfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,37 +3,19 @@ version_settings(constraint='>=0.22.2')
k8s_yaml(['k8s/namespaces.yml'])

docker_build(
'k8s-tilt-dev-example-node-nestjs-api',
context='./apps/node-nestjs-api/',
dockerfile='./apps/node-nestjs-api/Dockerfile',
'k8s-tilt-dev-example-node-nestjs-app',
'./apps/node-nestjs-app/',
target='development',
only=['./apps/node-nestjs-api/'],
live_update=[
sync('./apps/node-nestjs-api/', '/app/'),
run(
'npm install',
trigger=['./apps/node-nestjs-api/package.json']
)
]
)

k8s_yaml(['k8s/node-nestjs-api/deployment.yml', 'k8s/node-nestjs-api/service.yml'])
k8s_yaml(['k8s/node-nestjs-app/deployment.yml', 'k8s/node-nestjs-app/service.yml'])

docker_build(
'k8s-tilt-dev-example-vite-react-app',
context='./apps/vite-react-app/',
dockerfile='./apps/vite-react-app/Dockerfile',
'k8s-tilt-dev-example-python-fastapi-app',
'./apps/python-fastapi-app/',
target='development',
only=['./apps/vite-react-app/'],
live_update=[
sync('./apps/vite-react-app/', '/app/'),
run(
'npm install',
trigger=['./apps/vite-react-app/package.json']
)
]
)

k8s_yaml(['k8s/vite-react-app/deployment.yml', 'k8s/vite-react-app/service.yml'])
k8s_yaml(['k8s/python-fastapi-app/deployment.yml', 'k8s/python-fastapi-app/service.yml'])

k8s_yaml('k8s/ingress.yml')
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,11 @@ async function bootstrap() {
.addBearerAuth()
.setVersion('1.0')
.build();

const document = SwaggerModule.createDocument(app, config);
SwaggerModule.setup('api', app, document);
SwaggerModule.setup('docs', app, document);
const port = process.env.PORT || 3000;
await app.listen(port);
}

bootstrap();
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,3 @@ indent_size = 2
end_of_line = lf
insert_final_newline = true
trim_trailing_whitespace = true

[*.md]
insert_final_newline = false
trim_trailing_whitespace = false
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
MONGO_ROOT_USERNAME=root
MONGO_ROOT_PASSWORD=supersecret
MONGO_USERNAME=user1
MONGO_PASSWORD=supersecurepassword
MONGO_DATABASE=test_db
8 changes: 8 additions & 0 deletions examples/kubernetes-tilt-dev/apps/python-fastapi-app/.flake8
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[flake8]
ignore = E203, E266, E501, W503, F403, F401, B008
max-line-length = 89
max-complexity = 18
select = B,C,E,F,W,T4,B9
require-plugins =
flake8-bugbear
flake8-black
146 changes: 146 additions & 0 deletions examples/kubernetes-tilt-dev/apps/python-fastapi-app/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
##### IDE's #####
# VisualStudioCode
/.vscode/
.vscode/*
!.vscode/settings.json
!.vscode/tasks.json
!.vscode/launch.json
!.vscode/extensions.json
*.code-workspace
.history

# IntelliJ IDEA
.idea/*

##### Database #####
*.accdb
*.db
*.dbf
*.mdb
*.pdb
*.sqlite3

##### Logs #####
*.log
*.log*

##### Python #####
# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
*$py.class

# Distribution / packaging
.Python
build/
develop-eggs/
dist/
downloads/
eggs/
.eggs/
lib/
lib64
lib64/
parts/
sdist/
var/
wheels/
pip-wheel-metadata/
share/python-wheels/
*.egg-info/
.installed.cfg
*.egg
MANIFEST
bin/

# Environments
Makefile
.env
.env.*
!.env.example
!.env.local
.venv
env/
venv/
ENV/
env.bak/
venv.bak/
.flaskenv
pyvenv.cfg

# PyInstaller
*.manifest
*.spec

# Installer logs
pip-log.txt
pip-delete-this-directory.txt

# Unit test / coverage reports
htmlcov/
.tox/
.nox/
.coverage
.coverage.*
.cache
nosetests.xml
coverage.xml
*.cover
*.py,cover
.hypothesis/
.pytest_cache/

# Flask stuff:
instance/
.webassets-cache

# Translations
*.mo
*.pot

# Sphinx documentation
docs/_build/

# PyBuilder
target/

# IPython
profile_default/
ipython_config.py

# PEP 582
__pypackages__/

# mkdocs documentation
/site

# mypy
.mypy_cache/
.dmypy.json
dmypy.json

# Pyre type checker
.pyre/

# pytype static type analyzer
.pytype/

# C extensions
*.so

##### Heroku (old) #####
Procfile

###
postgres-calculate/

app/authentication/test_view.py

# Serverless
node_modules/
.serverless/

# wsgi
wsgi_handler.py
serverless_wsgi.py
.serverless-wsgi
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# See https://pre-commit.com for more information
# See https://pre-commit.com/hooks.html for more hooks
repos:
- repo: https://github.com/ambv/black
rev: 23.1.0
hooks:
- id: black
language_version: python3
- repo: https://github.com/pycqa/flake8
rev: 6.0.0
hooks:
- id: flake8
additional_dependencies: [flake8-black, flake8-bugbear]
58 changes: 58 additions & 0 deletions examples/kubernetes-tilt-dev/apps/python-fastapi-app/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
# Stage 1: Build development dependencies
FROM python:3.12.0 AS builder-dev

WORKDIR /code

# Copy development requirements files
COPY requirements*.txt ./

# Install development dependencies
RUN pip install --no-cache-dir -r requirements.dev.txt

# Stage 2: Build production dependencies
FROM python:3.12.0-alpine AS builder-prod

WORKDIR /code

# Copy production requirements file
COPY requirements.txt ./

# Install production dependencies
RUN pip install --no-cache-dir -r requirements.txt

FROM builder-dev AS development

# Copy development dependencies from builder-dev stage
COPY --from=builder-dev /usr/local /usr/local

# Copy the application code
COPY ./app ./app

# Create a non-root user to run the application
RUN adduser --disabled-password nonroot

# Switch to the non-root user
USER nonroot

# Command to run the application
CMD ["uvicorn", "app.main:app", "--host", "0.0.0.0", "--port", "80", "--reload"]

# Stage 3: Final image
FROM python:3.12.0-alpine as production

WORKDIR /code

# Copy production dependencies from builder-prod stage
COPY --from=builder-prod /usr/local /usr/local

# Copy the application code
COPY ./app ./app

# Create a non-root user to run the application
RUN adduser --disabled-password nonroot

# Switch to the non-root user
USER nonroot

# Command to run the application
CMD ["uvicorn", "app.main:app", "--host", "0.0.0.0", "--port", "80"]
57 changes: 57 additions & 0 deletions examples/kubernetes-tilt-dev/apps/python-fastapi-app/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
# FastAPI Example 🚀

## Requirements 🛠️

To get started, make sure you have the following tools installed on your machine:

- Docker 🐳
- Docker Compose 🐳
- Python 🐍

**Python Version:** You'll need Python 3.12 installed on your local development machine. You can use [pyenv](https://github.com/pyenv/pyenv) to easily switch Python versions between different projects. If you're on Windows, consider using [pyenv-win](https://github.com/pyenv-win/pyenv-win).

```sh
pyenv install
pyenv local
```

### Gotchas for Certain Environments 🧐

Depending on your development environment, you may also need to create and activate a virtual environment for Python. Follow these steps:

```sh
python3 -m venv venv
source venv/bin/activate
pip install -r requirements.dev.txt
```

For Windows users using WSL2, ensure you have Docker Desktop installed, running, and configured with a [WSL2 connection](https://learn.microsoft.com/en-us/windows/wsl/tutorials/wsl-containers).

## Setup the Development Environment 🛠️

- Install the project dependencies:

```sh
pip install -r requirements.dev.txt
```

- Install pre-commit hooks:

```sh
pre-commit install
```

## Running the App 🏃‍♀️🏃‍♂️

To launch the app, simply run:

```sh
cp .env.example .env # Then Edit .env file with your own values
docker-compose up
```

Your app will be up and running in no time! 🚀🎉

This will start the following services:

- `web`: FastAPI server running at [http://localhost:80](http://localhost:80)
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import os
from typing import Any

APP_VERSION: str = "1"

app_configs: dict[str, Any] = {
"title": "FastAPI Example",
"root_path": os.getenv("ROOT_PATH", ""),
}
18 changes: 18 additions & 0 deletions examples/kubernetes-tilt-dev/apps/python-fastapi-app/app/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
from .config import app_configs
from .router.routes import router as api_router

ALLOWED_ORIGINS = [
"*",
]
app = FastAPI(**app_configs)
app.add_middleware(
CORSMiddleware,
allow_origins=ALLOWED_ORIGINS,
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)

app.include_router(api_router)
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
from fastapi import APIRouter

router = APIRouter()


@router.get("/")
async def root():
return {"message": "Hello World :D"}

@router.get("/healthz")
async def healthcheck() -> dict[str, str]:
return {"status": "ok"}
Loading

0 comments on commit ab4e046

Please sign in to comment.