From 726e9a5a82f11e5d694b8aa24d0611baf484fefd Mon Sep 17 00:00:00 2001 From: Matthew Wang Date: Fri, 27 Sep 2024 03:17:51 -0400 Subject: [PATCH] Fix workflows and Dockerfiles Fix ruff lint Cleanup db-init --- .gitattributes | 1 - .github/workflows/lint.yml | 26 ++++++++++++-------------- backend/Dockerfile | 10 +++++----- backend/app/server.py | 8 +------- db-init/create-multiple-dbs.sh | 22 ---------------------- docker-compose.yml | 23 +++++++---------------- frontend/Dockerfile | 11 +++++++++++ 7 files changed, 36 insertions(+), 65 deletions(-) delete mode 100644 .gitattributes delete mode 100755 db-init/create-multiple-dbs.sh create mode 100644 frontend/Dockerfile diff --git a/.gitattributes b/.gitattributes deleted file mode 100644 index 8cfbd30..0000000 --- a/.gitattributes +++ /dev/null @@ -1 +0,0 @@ -db-init/create-multiple-dbs.sh eol=lf diff --git a/.github/workflows/lint.yml b/.github/workflows/lint.yml index 2950c9f..04f91d5 100644 --- a/.github/workflows/lint.yml +++ b/.github/workflows/lint.yml @@ -6,25 +6,23 @@ on: - main paths: - "frontend/**" - - "backend/typescript/**" - - "backend/python/**" + - "backend/**" pull_request: branches: - main paths: - "frontend/**" - - "backend/typescript/**" - - "backend/python/**" + - "backend/**" jobs: run-lint: runs-on: ubuntu-latest steps: - name: Checkout code - uses: actions/checkout@v2 + uses: actions/checkout@v4 - name: Filter changed files - uses: dorny/paths-filter@v2 + uses: dorny/paths-filter@v3 id: changes with: filters: | @@ -35,23 +33,23 @@ jobs: - name: Set up Node.js if: steps.changes.outputs.frontend == 'true' - uses: actions/setup-node@v2 + uses: actions/setup-node@v4 with: node-version: "20.11.1" - cache: "yarn" + cache: "npm" cache-dependency-path: | - frontend/yarn.lock + frontend/package-lock.json - name: Install Node.js dependencies if: steps.changes.outputs.frontend == 'true' - run: yarn --cwd ./frontend --prefer-offline + run: npm ci --prefix ./frontend - name: Lint frontend if: steps.changes.outputs.frontend == 'true' working-directory: ./frontend - run: yarn lint + run: npm run lint - name: Lint Python backend - if: steps.changes.outputs.python-backend == 'true' - working-directory: ./backend/python - run: pip install black && python -m black --check . + if: steps.changes.outputs.backend == 'true' + working-directory: ./backend + run: pip install ruff && ruff check . diff --git a/backend/Dockerfile b/backend/Dockerfile index 2b9239b..527a6a5 100644 --- a/backend/Dockerfile +++ b/backend/Dockerfile @@ -2,10 +2,10 @@ FROM python:3.12 WORKDIR /app -COPY requirements.txt ./ -RUN pip install -r requirements.txt - -COPY . ./app +COPY pyproject.toml ./ +COPY pdm.lock ./ +RUN pip install pdm && pdm install +COPY . . EXPOSE 8080 -ENTRYPOINT ["python", "server.py"] +CMD ["pdm", "run", "fastapi", "run", "app/server.py", "--port", "8080"] diff --git a/backend/app/server.py b/backend/app/server.py index 36467c7..fec549b 100644 --- a/backend/app/server.py +++ b/backend/app/server.py @@ -1,9 +1,8 @@ -import os -import uvicorn from dotenv import load_dotenv from typing import Union from fastapi import FastAPI +load_dotenv() app = FastAPI() @app.get("/") @@ -14,8 +13,3 @@ def read_root(): @app.get("/items/{item_id}") def read_item(item_id: int, q: Union[str, None] = None): return {"item_id": item_id, "q": q} - -if __name__ == "__main__": - load_dotenv() - - uvicorn.run(app, host="0.0.0.0", port=int(os.getenv("PORT", 8080))) diff --git a/db-init/create-multiple-dbs.sh b/db-init/create-multiple-dbs.sh deleted file mode 100755 index aa665fa..0000000 --- a/db-init/create-multiple-dbs.sh +++ /dev/null @@ -1,22 +0,0 @@ -#!/bin/bash - -set -e -set -u - -function create_user_and_database() { - local database=$1 - echo " Creating user and database '$database'" - psql -v ON_ERROR_STOP=1 --username "$POSTGRES_USER" <<-EOSQL - CREATE USER $database; - CREATE DATABASE $database; - GRANT ALL PRIVILEGES ON DATABASE $database TO $database; -EOSQL -} - -if [ -n "$POSTGRES_MULTIPLE_DATABASES" ]; then - echo "Multiple database creation requested: $POSTGRES_MULTIPLE_DATABASES" - for db in $(echo $POSTGRES_MULTIPLE_DATABASES | tr ',' ' '); do - create_user_and_database $db - done - echo "Multiple databases created" -fi diff --git a/docker-compose.yml b/docker-compose.yml index 378ee67..3432640 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -6,44 +6,35 @@ services: build: context: ./frontend dockerfile: Dockerfile - volumes: - - ./frontend:/app - - /app/node_modules ports: - 3000:3000 environment: - CHOKIDAR_USEPOLLING=true env_file: - ./frontend/.env - py-backend: - container_name: llsc_py_backend + backend: + container_name: llsc_backend restart: always build: - context: ./backend/python + context: ./backend dockerfile: Dockerfile ports: - 8080:8080 - dns: - - 8.8.8.8 - volumes: - - ./backend/python:/app depends_on: db: condition: service_healthy - env_file: - - ./.env db: container_name: llsc_db - image: postgres:12-alpine + image: postgres:16-alpine ports: - 5432:5432 volumes: - postgres_data:/var/lib/postgresql/data/ - ./db-init:/docker-entrypoint-initdb.d - env_file: - - ./.env environment: - - POSTGRES_MULTIPLE_DATABASES=${POSTGRES_DB_DEV},${POSTGRES_DB_TEST} + POSTGRES_USER: ${POSTGRES_USER} + POSTGRES_PASSWORD: ${POSTGRES_PASSWORD} + POSTGRES_DB: ${POSTGRES_DATABASE} healthcheck: test: ["CMD-SHELL", "pg_isready -U postgres"] interval: 5s diff --git a/frontend/Dockerfile b/frontend/Dockerfile new file mode 100644 index 0000000..01be6c1 --- /dev/null +++ b/frontend/Dockerfile @@ -0,0 +1,11 @@ +FROM node:20.11.1 + +WORKDIR /app + +COPY package.json package-lock.json ./ +RUN npm install + +COPY . . + +EXPOSE 3000 +CMD ["npm", "run", "dev"]