Skip to content

Commit

Permalink
Dockerizing for deployment!
Browse files Browse the repository at this point in the history
  • Loading branch information
KnightChaser committed Jun 2, 2024
1 parent 1a2b64f commit 934fbcf
Show file tree
Hide file tree
Showing 8 changed files with 100 additions and 12 deletions.
4 changes: 2 additions & 2 deletions .env
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ JWT_SECRET_KEY="KCXU$3R$3CR3T4JWT_"
JWT_TOKEN_EXPIRES_MINUTES=360 # 6 hours

# A custom API server built with Redis
REDIS_HOST="localhost"
REDIS_HOST="db-redis"
REDIS_PORT=6379
REDIS_DB=0
REDIS_UDPATE_INTERVAL_IN_SECONDS=1
REDIS_UDPATE_INTERVAL_IN_SECONDS=1
14 changes: 14 additions & 0 deletions Dockerfile.backend
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# Dockerfile for FastAPI backend

FROM python:3.12-slim
WORKDIR /app
COPY requirements.txt .
RUN apt-get update && apt-get install -y gcc
RUN pip install --no-cache-dir -r requirements.txt
COPY . .

# Expose the port the app runs on
EXPOSE 8000

# Command to run the app
CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "8000"]
15 changes: 15 additions & 0 deletions Dockerfile.frontend
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Dockerfile for Svelte frontend

FROM node:16
WORKDIR /app
COPY frontend/package*.json ./
RUN npm install
COPY frontend .
# No need to run the build step for development
# RUN npm run build

# Expose the port the dev server runs on
EXPOSE 5173

# Command to run the app in development mode
CMD ["npm", "run", "dev", "--", "--host"]
52 changes: 52 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
version: '3.12'

networks:
kcx-network:
driver: bridge

services:
backend:
build:
context: .
dockerfile: Dockerfile.backend
env_file:
- .env
ports:
- "8000:8000"
depends_on:
- db-redis
- db-sqlite3
networks:
- kcx-network

frontend:
build:
context: .
dockerfile: Dockerfile.frontend
ports:
- "3000:3000"
- "5173:5173"
depends_on:
- backend
environment:
VITE_BASE_URL: "http://backend:8000"
networks:
- kcx-network

db-redis:
image: "redis:alpine"
ports:
- "6379:6379"
networks:
- kcx-network

db-sqlite3:
image: "nouchka/sqlite3"
volumes:
- ./data:/data
environment:
SQLITE_DATABASE: "kcx.db"
ports:
- "8080:8080"
networks:
- kcx-network
2 changes: 1 addition & 1 deletion frontend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"scripts": {
"dev": "vite",
"build": "vite build",
"preview": "vite preview"
"preview": "vite preview --host"
},
"devDependencies": {
"@sveltejs/vite-plugin-svelte": "^3.0.2",
Expand Down
2 changes: 1 addition & 1 deletion frontend/vite.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ import { svelte } from '@sveltejs/vite-plugin-svelte'
// https://vitejs.dev/config/
export default defineConfig({
plugins: [svelte()],
})
})
16 changes: 10 additions & 6 deletions main.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# main.py

from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware

Expand Down Expand Up @@ -25,10 +27,10 @@ async def lifespan(app: FastAPI):
load_dotenv()

# Get Redis connection information from environment variables
redis_host:str = os.getenv("REDIS_HOST", "localhost")
redis_host:str = os.getenv("REDIS_HOST", "db-redis") # Use new service name
redis_port:int = int(os.getenv("REDIS_PORT", 6379))
redis_database:int = int(os.getenv("REDIS_DATABASE", 0))
update_interval_in_seconds:int = int(os.getenv("UPDATE_INTERVAL_IN_SECONDS", 1))
redis_database:int = int(os.getenv("REDIS_DB", 0))
update_interval_in_seconds:int = int(os.getenv("REDIS_UDPATE_INTERVAL_IN_SECONDS", 1))
console.log(f"Setting up the Redis database at {redis_host}:{redis_port}/{redis_database} (update interval: {update_interval_in_seconds} seconds)")
start_fetch_and_store_market_data(redis_host=redis_host,
redis_port=redis_port,
Expand Down Expand Up @@ -69,7 +71,7 @@ async def lifespan(app: FastAPI):
console.log(
f"Test account created because it didn't exist [bold](UUID: {user_uuid})[/bold]")

# Manully insert the balance for the test account
# Manually insert the balance for the test account
from models import Balance
db = SessionLocal()
new_balance = Balance(user_id=new_user.id,
Expand All @@ -85,11 +87,13 @@ async def lifespan(app: FastAPI):
app = FastAPI(lifespan=lifespan)

allowed_origins: list = [
"http://localhost:5173",
"http://localhost:5173", # Vite dev server default
"http://localhost:4173", # Vite preview default
"http://frontend:4173" # Frontend service in Docker
]
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_origins=allowed_origins,
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
Expand Down
7 changes: 5 additions & 2 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
fastapi==0.111.0
pydantic==2.7.1
pydantic==2.7.2
PyJWT==2.8.0
python-dotenv==1.0.1
python_bcrypt==0.3.2
bcrypt==4.1.2
redis==5.0.4
Requests==2.32.3
rich==13.7.1
SQLAlchemy==2.0.29

0 comments on commit 934fbcf

Please sign in to comment.