Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/docker #4

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
**/__pycache__
oortclwd marked this conversation as resolved.
Show resolved Hide resolved
**/.classpath
**/.dockerignore
**/.env
**/.git
**/.gitignore
**/.project
**/.settings
**/.toolstarget
**/.vs
**/.vscode
**/*.*proj.user
**/*.dbmdl
**/*.jfm
**/bin
**/charts
**/docker-compose*
**/compose*
**/Dockerfile*
**/node_modules
**/npm-debug.log
**/obj
**/secrets.dev.yaml
**/values.dev.yaml
README.md

venv/
.pre-commit-config.yaml
51 changes: 51 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
name: TEST

on: [push]

env:
APP_ENV: TEST

jobs:
test:
runs-on: ubuntu-latest
services:
mysql:
image: mysql:8.0
ports:
- 3306:3306
env:
MYSQL_USER: admin
MYSQL_PASSWORD: 1234
oortclwd marked this conversation as resolved.
Show resolved Hide resolved
MYSQL_ROOT_PASSWORD: 1234
MYSQL_DATABASE: teting
options: --health-cmd="mysqladmin ping" --health-interval=5s --health-timeout=2s --health-retries=3

steps:
- name: MySQL Check
env:
PORT: ${{ job.services.mysql.ports[3306] }}
run: |
while ! mysqladmin ping -h"127.0.0.1" -P"$PORT" --silent; do
sleep 1
done

- uses: actions/checkout@v2
- name: Set Up Python
uses: actions/setup-python@v2
with:
python-version: "3.8"

- name: Install Dependencies
run: |
python -m pip install --upgrade pip
pip install -r requirements.txt

- name: Test
env:
DB_USERNAME: admin
DB_PASSWORD: 1234
DB_HOST: 127.0.0.1
DB_PORT: 3306
DB_NAME: testing
run:
pytest
6 changes: 6 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
repos:
- repo: https://github.com/psf/black
rev: 22.3.0
hooks:
- id: black
language_version: python3
25 changes: 25 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# For more information, please refer to https://aka.ms/vscode-docker-python
FROM python:3.8-slim

EXPOSE 8000

# Keeps Python from generating .pyc files in the container
ENV PYTHONDONTWRITEBYTECODE=1

# Turns off buffering for easier container logging
ENV PYTHONUNBUFFERED=1

# Install pip requirements
COPY requirements.txt .
RUN python -m pip install -r requirements.txt

WORKDIR /app
COPY . /app
oortclwd marked this conversation as resolved.
Show resolved Hide resolved

# Creates a non-root user with an explicit UID and adds permission to access the /app folder
# For more info, please refer to https://aka.ms/vscode-docker-python-configure-containers
RUN adduser -u 5678 --disabled-password --gecos "" appuser && chown -R appuser /app
USER appuser

# During debugging, this entry point will be overridden. For more information, please refer to https://aka.ms/vscode-docker-python-debug
CMD ["gunicorn", "--bind", "0.0.0.0:8000", "-k", "uvicorn.workers.UvicornWorker", "backend\main:app"]
22 changes: 13 additions & 9 deletions backend/core/config.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from pydantic import BaseSettings, SecretStr

from pathlib import Path
import os

BASE_DIR = Path(__file__).resolve().parents[2]

Expand All @@ -19,27 +19,31 @@ class Settings(BaseSettings):
SECRET_ALGORITHM = "HS256"
ACCESS_TOKEN_EXPIRE_MINUTES = 30


class Config:
env_file = str(BASE_DIR / ".env")
env_file_encoding = "utf-8"


class TestSettings(BaseSettings):
TEST_DB_USERNAME: str
TEST_DB_PASSWORD: SecretStr
TEST_DB_HOST: str
TEST_DB_PORT: int
TEST_DB_NAME: str
DB_USERNAME: str
DB_PASSWORD: SecretStr
DB_HOST: str
DB_PORT: int
DB_NAME: str

SECRET_KEY: str
SECRET_ALGORITHM = "HS256"
ACCESS_TOKEN_EXPIRE_MINUTES = 30


class Config:
env_file = str(BASE_DIR / ".env")
env_file_encoding = "utf-8"


def get_settings():
if os.getenv("APP_ENV", "TEST") == "TEST":
return TestSettings()
return Settings


settings = Settings()
settings = get_settings()
15 changes: 4 additions & 11 deletions backend/schemas/tokens.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,6 @@
from pydantic import BaseModel, EmailStr
from pydantic import BaseModel

class UserCreate(BaseModel):
username: str
email: EmailStr
password: str

class ShowUser(BaseModel):
username: str
email: EmailStr

class Config:
orm_mode = True
class Token(BaseModel):
access_token: str
token_type: str
18 changes: 14 additions & 4 deletions backend/schemas/users.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
from pydantic import BaseModel
from pydantic import BaseModel, EmailStr

class Token(BaseModel):
access_token: str
token_type: str

class UserCreate(BaseModel):
username: str
email: EmailStr
password: str


class ShowUser(BaseModel):
username: str
email: EmailStr

class Config:
orm_mode = True
20 changes: 12 additions & 8 deletions backend/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,39 +7,42 @@

import sys
import os

sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))

from apis.base import api_router
from db.base import Base
from db.session import get_db
from core.config import TestSettings
from core.config import settings


def start_application() -> FastAPI:
app = FastAPI()
app.include_router(api_router)
return app


settings = TestSettings()
engine = create_engine(
"mysql+pymysql://{username}:{password}@{host}:{port}/{name}?charset=utf8mb4".format(
username=settings.TEST_DB_USERNAME,
password=settings.TEST_DB_PASSWORD.get_secret_value(),
host=settings.TEST_DB_HOST,
port=settings.TEST_DB_PORT,
name=settings.TEST_DB_NAME,
username=settings.DB_USERNAME,
password=settings.DB_PASSWORD.get_secret_value(),
host=settings.DB_HOST,
port=settings.DB_PORT,
name=settings.DB_NAME,
)
)

SessionTesting = sessionmaker(autocommit=False, autoflush=False, bind=engine)


@pytest.fixture(scope="module")
def app() -> Generator[FastAPI, Any, None]:
Base.metadata.create_all(engine)
_app = start_application()
yield _app
Base.metadata.drop_all(engine)


@pytest.fixture(scope="module")
def db_session(app: FastAPI) -> Generator[SessionTesting, Any, None]:
connection = engine.connect()
Expand All @@ -50,6 +53,7 @@ def db_session(app: FastAPI) -> Generator[SessionTesting, Any, None]:
transaction.rollback()
connection.close()


@pytest.fixture(scope="module")
def client(
app: FastAPI, db_session: SessionTesting
Expand All @@ -59,7 +63,7 @@ def _get_test_db():
yield db_session
finally:
pass

app.dependency_overrides[get_db] = _get_test_db
with TestClient(app) as client:
yield client
64 changes: 54 additions & 10 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,10 +1,54 @@
pymysql
pydantic[dotenv]

sqlalchemy
fastapi
requests
pytest
uvicorn[standard]
python-jose[cryptography]
passlib[bcrypt]
anyio==3.5.0
asgiref==3.5.0
atomicwrites==1.4.0
attrs==21.4.0
bcrypt==3.2.0
certifi==2021.10.8
cffi==1.15.0
cfgv==3.3.1
charset-normalizer==2.0.12
click==8.0.4
colorama==0.4.4
cryptography==36.0.2
distlib==0.3.4
dnspython==2.2.1
ecdsa==0.17.0
email-validator==1.1.3
fastapi==0.75.0
filelock==3.6.0
greenlet==1.1.2
h11==0.13.0
identify==2.4.12
idna==3.3
iniconfig==1.1.1
install==1.3.5
nodeenv==1.6.0
packaging==21.3
passlib==1.7.4
platformdirs==2.5.1
pluggy==1.0.0
pre-commit==2.17.0
py==1.11.0
pyasn1==0.4.8
pycparser==2.21
pydantic==1.9.0
pydantic[email]
PyMySQL==1.0.2
pyparsing==3.0.7
pytest==7.1.1
python-dotenv==0.20.0
python-jose==3.3.0
python-multipart==0.0.5
PyYAML==6.0
requests==2.27.1
rsa==4.8
six==1.16.0
sniffio==1.2.0
SQLAlchemy==1.4.32
starlette==0.17.1
toml==0.10.2
tomli==2.0.1
typing-extensions==4.1.1
urllib3==1.26.9
uvicorn==0.17.6
virtualenv==20.14.0