Skip to content

Commit 934fbcf

Browse files
committedJun 2, 2024
Dockerizing for deployment!
1 parent 1a2b64f commit 934fbcf

8 files changed

+100
-12
lines changed
 

‎.env

+2-2
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ JWT_SECRET_KEY="KCXU$3R$3CR3T4JWT_"
99
JWT_TOKEN_EXPIRES_MINUTES=360 # 6 hours
1010

1111
# A custom API server built with Redis
12-
REDIS_HOST="localhost"
12+
REDIS_HOST="db-redis"
1313
REDIS_PORT=6379
1414
REDIS_DB=0
15-
REDIS_UDPATE_INTERVAL_IN_SECONDS=1
15+
REDIS_UDPATE_INTERVAL_IN_SECONDS=1

‎Dockerfile.backend

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# Dockerfile for FastAPI backend
2+
3+
FROM python:3.12-slim
4+
WORKDIR /app
5+
COPY requirements.txt .
6+
RUN apt-get update && apt-get install -y gcc
7+
RUN pip install --no-cache-dir -r requirements.txt
8+
COPY . .
9+
10+
# Expose the port the app runs on
11+
EXPOSE 8000
12+
13+
# Command to run the app
14+
CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "8000"]

‎Dockerfile.frontend

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# Dockerfile for Svelte frontend
2+
3+
FROM node:16
4+
WORKDIR /app
5+
COPY frontend/package*.json ./
6+
RUN npm install
7+
COPY frontend .
8+
# No need to run the build step for development
9+
# RUN npm run build
10+
11+
# Expose the port the dev server runs on
12+
EXPOSE 5173
13+
14+
# Command to run the app in development mode
15+
CMD ["npm", "run", "dev", "--", "--host"]

‎docker-compose.yml

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
version: '3.12'
2+
3+
networks:
4+
kcx-network:
5+
driver: bridge
6+
7+
services:
8+
backend:
9+
build:
10+
context: .
11+
dockerfile: Dockerfile.backend
12+
env_file:
13+
- .env
14+
ports:
15+
- "8000:8000"
16+
depends_on:
17+
- db-redis
18+
- db-sqlite3
19+
networks:
20+
- kcx-network
21+
22+
frontend:
23+
build:
24+
context: .
25+
dockerfile: Dockerfile.frontend
26+
ports:
27+
- "3000:3000"
28+
- "5173:5173"
29+
depends_on:
30+
- backend
31+
environment:
32+
VITE_BASE_URL: "http://backend:8000"
33+
networks:
34+
- kcx-network
35+
36+
db-redis:
37+
image: "redis:alpine"
38+
ports:
39+
- "6379:6379"
40+
networks:
41+
- kcx-network
42+
43+
db-sqlite3:
44+
image: "nouchka/sqlite3"
45+
volumes:
46+
- ./data:/data
47+
environment:
48+
SQLITE_DATABASE: "kcx.db"
49+
ports:
50+
- "8080:8080"
51+
networks:
52+
- kcx-network

‎frontend/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
"scripts": {
77
"dev": "vite",
88
"build": "vite build",
9-
"preview": "vite preview"
9+
"preview": "vite preview --host"
1010
},
1111
"devDependencies": {
1212
"@sveltejs/vite-plugin-svelte": "^3.0.2",

‎frontend/vite.config.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,4 @@ import { svelte } from '@sveltejs/vite-plugin-svelte'
44
// https://vitejs.dev/config/
55
export default defineConfig({
66
plugins: [svelte()],
7-
})
7+
})

‎main.py

+10-6
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
# main.py
2+
13
from fastapi import FastAPI
24
from fastapi.middleware.cors import CORSMiddleware
35

@@ -25,10 +27,10 @@ async def lifespan(app: FastAPI):
2527
load_dotenv()
2628

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

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

8789
allowed_origins: list = [
88-
"http://localhost:5173",
90+
"http://localhost:5173", # Vite dev server default
91+
"http://localhost:4173", # Vite preview default
92+
"http://frontend:4173" # Frontend service in Docker
8993
]
9094
app.add_middleware(
9195
CORSMiddleware,
92-
allow_origins=["*"],
96+
allow_origins=allowed_origins,
9397
allow_credentials=True,
9498
allow_methods=["*"],
9599
allow_headers=["*"],

‎requirements.txt

+5-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
fastapi==0.111.0
2-
pydantic==2.7.1
2+
pydantic==2.7.2
3+
PyJWT==2.8.0
34
python-dotenv==1.0.1
4-
python_bcrypt==0.3.2
5+
bcrypt==4.1.2
6+
redis==5.0.4
7+
Requests==2.32.3
58
rich==13.7.1
69
SQLAlchemy==2.0.29

0 commit comments

Comments
 (0)
Please sign in to comment.