Skip to content

Commit

Permalink
도커 이미지파일 작성
Browse files Browse the repository at this point in the history
  • Loading branch information
riroan committed Aug 24, 2024
1 parent fdc39f8 commit 8c9b985
Show file tree
Hide file tree
Showing 10 changed files with 110 additions and 26 deletions.
5 changes: 5 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
.dockerignore
/venv
Dockerfile
.env
*.db
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -193,3 +193,5 @@ pyrightconfig.json
.ionide

# End of https://www.toptal.com/developers/gitignore/api/python,visualstudiocode

*.db
13 changes: 13 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
FROM python:3.12

WORKDIR /app

COPY . .

RUN pip install -r requirements.txt

EXPOSE 8000

ENV DB_NAME="" DB_USER="" DB_PASSWORD="" DB_PORT=0 DB_HOST=""

CMD ["python", "main.py"]
10 changes: 1 addition & 9 deletions db.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,8 @@

class Database:
def __init__(self, settings: Settings):
url = URL.create(
drivername="mysql+pymysql",
username=settings.db_user,
password=settings.db_password,
host=settings.db_host,
port=settings.db_port,
database=settings.db_name
)
self.engine = create_engine(
url=url
url=settings.get_db_url()
)
self.session = sessionmaker(
autocommit=False,
Expand Down
8 changes: 4 additions & 4 deletions dto/product.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@ class ProductResponseDto(ProductDto):
@staticmethod
def from_entity(
product: Product,
like_count: int,
dislike_count: int,
is_like: bool,
is_dislike: bool,
like_count: int = 0,
dislike_count: int = 0,
is_like: bool = False,
is_dislike: bool = False,
):
return ProductResponseDto(
id=product.id,
Expand Down
5 changes: 5 additions & 0 deletions main.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
import uvicorn

from app import create_app
from settings import Settings

app = create_app(Settings())

if __name__ == "__main__":
uvicorn.run("main:app", port=8000, host="0.0.0.0")
60 changes: 60 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
alembic==1.13.2
annotated-types==0.7.0
anyio==4.4.0
certifi==2024.7.4
cffi==1.17.0
cfgv==3.4.0
click==8.1.7
cryptography==43.0.0
distlib==0.3.8
dnspython==2.6.1
email_validator==2.2.0
fastapi==0.112.1
fastapi-cli==0.0.5
filelock==3.15.4
h11==0.14.0
httpcore==1.0.5
httptools==0.6.1
httpx==0.27.0
identify==2.6.0
idna==3.8
iniconfig==2.0.0
itsdangerous==2.2.0
Jinja2==3.1.4
load-config==0.2.0b6
Mako==1.3.5
markdown-it-py==3.0.0
MarkupSafe==2.1.5
mdurl==0.1.2
nodeenv==1.9.1
orjson==3.10.7
packaging==24.1
platformdirs==4.2.2
pluggy==1.5.0
pre-commit==3.8.0
pycparser==2.22
pydantic==2.8.2
pydantic-extra-types==2.9.0
pydantic-settings==2.4.0
pydantic_core==2.20.1
Pygments==2.18.0
PyMySQL==1.1.1
pytest==8.3.2
pytest-asyncio==0.24.0
python-dotenv==1.0.1
python-multipart==0.0.9
PyYAML==6.0.2
rich==13.7.1
shellingham==1.5.4
sniffio==1.3.1
SQLAlchemy==2.0.32
starlette==0.38.2
toml==0.10.2
typer==0.12.4
typing_extensions==4.12.2
ujson==5.10.0
uvicorn==0.30.6
uvloop==0.20.0
virtualenv==20.26.3
watchfiles==0.23.0
websockets==13.0
19 changes: 17 additions & 2 deletions settings.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
from typing import override

from pydantic_settings import BaseSettings, SettingsConfigDict
from sqlalchemy import URL


class Settings(BaseSettings):
Expand All @@ -12,7 +15,19 @@ class Settings(BaseSettings):
db_host: str
db_port: int

def get_db_url(self):
return URL.create(
drivername="mysql+pymysql",
username=self.db_user,
password=self.db_password,
host=self.db_host,
port=self.db_port,
database=self.db_name
)


class TestSettings(Settings):
db_port: int = 9999
db_name: str = "test"

@override
def get_db_url(self):
return "sqlite:///pangtok.db"
12 changes: 2 additions & 10 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,8 @@ async def test_fastapi_app(settings):

@pytest_asyncio.fixture(scope="function")
async def engine(settings):
url = URL.create(
drivername="mysql+pymysql",
username=settings.db_user,
password=settings.db_password,
host=settings.db_host,
port=settings.db_port,
database=settings.db_name
)
eng = create_engine(
url=url
url=settings.get_db_url()
)
Base.metadata.create_all(eng)
yield eng
Expand All @@ -45,7 +37,7 @@ async def engine(settings):
@pytest_asyncio.fixture(scope="function")
async def session(engine):
sessionLocal = sessionmaker(engine)
return sessionLocal()
yield sessionLocal()


@pytest_asyncio.fixture(scope="function")
Expand Down
2 changes: 1 addition & 1 deletion tests/e2e/test_product.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ async def test_show_product_detail_returns_200(
async with AsyncClient(
transport=ASGITransport(app=test_fastapi_app), base_url="http://test"
) as client:
response = await client.get("/product/0")
response = await client.get("/product/0/1")

assert response.status_code == status.HTTP_200_OK

Expand Down

0 comments on commit 8c9b985

Please sign in to comment.