From 934fbcfbf3eb24abe7266a49d802ec3663957a41 Mon Sep 17 00:00:00 2001 From: Lee Garam Date: Sun, 2 Jun 2024 17:14:32 +0900 Subject: [PATCH] Dockerizing for deployment! --- .env | 4 ++-- Dockerfile.backend | 14 +++++++++++ Dockerfile.frontend | 15 ++++++++++++ docker-compose.yml | 52 +++++++++++++++++++++++++++++++++++++++++ frontend/package.json | 2 +- frontend/vite.config.js | 2 +- main.py | 16 ++++++++----- requirements.txt | 7 ++++-- 8 files changed, 100 insertions(+), 12 deletions(-) create mode 100644 Dockerfile.backend create mode 100644 Dockerfile.frontend create mode 100644 docker-compose.yml diff --git a/.env b/.env index eb73a4f..9b82fbd 100644 --- a/.env +++ b/.env @@ -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 \ No newline at end of file +REDIS_UDPATE_INTERVAL_IN_SECONDS=1 diff --git a/Dockerfile.backend b/Dockerfile.backend new file mode 100644 index 0000000..4a5d5c0 --- /dev/null +++ b/Dockerfile.backend @@ -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"] \ No newline at end of file diff --git a/Dockerfile.frontend b/Dockerfile.frontend new file mode 100644 index 0000000..7cc0f35 --- /dev/null +++ b/Dockerfile.frontend @@ -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"] diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..9314373 --- /dev/null +++ b/docker-compose.yml @@ -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 diff --git a/frontend/package.json b/frontend/package.json index f726c84..b79fcbf 100644 --- a/frontend/package.json +++ b/frontend/package.json @@ -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", diff --git a/frontend/vite.config.js b/frontend/vite.config.js index d701969..ad1266d 100644 --- a/frontend/vite.config.js +++ b/frontend/vite.config.js @@ -4,4 +4,4 @@ import { svelte } from '@sveltejs/vite-plugin-svelte' // https://vitejs.dev/config/ export default defineConfig({ plugins: [svelte()], -}) +}) \ No newline at end of file diff --git a/main.py b/main.py index 179de96..8f8ee03 100644 --- a/main.py +++ b/main.py @@ -1,3 +1,5 @@ +# main.py + from fastapi import FastAPI from fastapi.middleware.cors import CORSMiddleware @@ -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, @@ -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, @@ -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=["*"], diff --git a/requirements.txt b/requirements.txt index 8468fd7..b6e91eb 100644 --- a/requirements.txt +++ b/requirements.txt @@ -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