diff --git a/.dev.Dockerfile b/.dev.Dockerfile new file mode 100644 index 0000000..91bff49 --- /dev/null +++ b/.dev.Dockerfile @@ -0,0 +1,11 @@ +FROM python:3 +WORKDIR /app +ENV POSTGRES_HOST=together-postgres +ENV POSTGRES_USER=together +ENV POSTGRES_PASSWORD=togetherno.1 +COPY ./requirements.txt . +RUN pip install --no-cache-dir --upgrade -r /app/requirements.txt -i https://mirrors.aliyun.com/pypi/simple +RUN alembic +COPY . . +EXPOSE 8000 +CMD ["uvicorn", "src.main:app", "--host", "0.0.0.0", "--port", "8000", "--env-file", "./.dev.env"] \ No newline at end of file diff --git a/.dev.env b/.dev.env new file mode 100644 index 0000000..a49b2c0 --- /dev/null +++ b/.dev.env @@ -0,0 +1,5 @@ +POSTGRES_HOST=together-postgres +POSTGRES_USER=together +POSTGRES_PASSWORD=togetherno.1 +REDIS_HOST=together-redis +REDIS_PORT=6379 \ No newline at end of file diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..a1b0469 --- /dev/null +++ b/.dockerignore @@ -0,0 +1,6 @@ +.venv +.idea +__pycache__ + +static/temp/ +static/chat/ diff --git a/.local.env b/.local.env new file mode 100644 index 0000000..2dddbb3 --- /dev/null +++ b/.local.env @@ -0,0 +1,5 @@ +POSTGRES_HOST=localhost +POSTGRES_USER=together +POSTGRES_PASSWORD=togetherno.1 +REDIS_HOST=localhost +REDIS_PORT=6379 \ No newline at end of file diff --git a/.prod.Dockerfile b/.prod.Dockerfile new file mode 100644 index 0000000..1f274b7 --- /dev/null +++ b/.prod.Dockerfile @@ -0,0 +1,8 @@ +FROM python:3 +WORKDIR /app +COPY ./requirements.txt . +RUN pip install --no-cache-dir --upgrade -r /app/requirements.txt -i https://mirrors.aliyun.com/pypi/simple +RUN alembic +COPY . . +EXPOSE 8000 +CMD ["uvicorn", "src.main:app", "--host", "0.0.0.0", "--port", "8000", "--env-file", "./.prod.env"] \ No newline at end of file diff --git a/.prod.env b/.prod.env new file mode 100644 index 0000000..e69de29 diff --git a/Pipfile.lock b/Pipfile.lock index 0b83c46..b8f6ce7 100644 --- a/Pipfile.lock +++ b/Pipfile.lock @@ -61,7 +61,7 @@ "sha256:4640d96be84d82d02ed59ea2b7105a0f7b33abe8703703cd0ab0bf87c427522f", "sha256:7405140ff1230c310e51dc27b3145b9092d659ce68ff733fb0cefe3ee42be028" ], - "markers": "python_version < '3.12.0'", + "markers": "python_full_version < '3.12.0'", "version": "==4.0.3" }, "asyncpg": { @@ -612,11 +612,11 @@ }, "mako": { "hashes": [ - "sha256:2a0c8ad7f6274271b3bb7467dd37cf9cc6dab4bc19cb69a4ef10669402de698e", - "sha256:32a99d70754dfce237019d17ffe4a282d2d3351b9c476e90d8a60e63f133b80c" + "sha256:5324b88089a8978bf76d1629774fcc2f1c07b82acdf00f4c5dd8ceadfffc4b40", + "sha256:e16c01d9ab9c11f7290eef1cfefc093fb5a45ee4a3da09e2fec2e4d1bae54e73" ], "markers": "python_version >= '3.8'", - "version": "==1.3.2" + "version": "==1.3.3" }, "markupsafe": { "hashes": [ @@ -906,7 +906,7 @@ "sha256:37dd54208da7e1cd875388217d5e00ebd4179249f90fb72437e91a35459a0ad3", "sha256:a8b2bc7bffae282281c8140a97d3aa9c14da0b136dfe83f850eea9a5f7470427" ], - "markers": "python_version >= '2.7' and python_version not in '3.0, 3.1, 3.2'", + "markers": "python_version >= '2.7' and python_version not in '3.0, 3.1, 3.2, 3.3'", "version": "==2.9.0.post0" }, "python-dotenv": { @@ -1016,7 +1016,7 @@ "sha256:1e61c37477a1626458e36f7b1d82aa5c9b094fa4802892072e49de9c60c4c926", "sha256:8abb2f1d86890a2dfb989f9a77cfcfd3e47c2a354b01111771326f8aa26e0254" ], - "markers": "python_version >= '2.7' and python_version not in '3.0, 3.1, 3.2'", + "markers": "python_version >= '2.7' and python_version not in '3.0, 3.1, 3.2, 3.3'", "version": "==1.16.0" }, "sniffio": { diff --git a/README.md b/README.md new file mode 100644 index 0000000..b810f41 --- /dev/null +++ b/README.md @@ -0,0 +1,72 @@ +# Initialization + +## Virtue Environment + +First install `pipenv` (if you don't have) running: + +```shell +pip install pipenv +``` + +Then use `pipenv` to create virtue environment running in the root directory of the project: + +```shell +pipenv install +``` + +This command will install all the dependencies. + +## Requirements + +Generate requirements file running + +```shell +pipenv requirements > requirements.txt +``` + +# Start Up + +Start up the app according to different environment variables running: + +## fastapi + +```shell +uvicorn src.main:app --reload --env-file ./.local.env +``` + +# Migration + +## Generate a migration + +```shell +alembic revision -m "comment" --autogenerate +``` + +## Upgrade a migration + +This will upgrade to the newest version + +```shell +alembic upgrade head +``` + +or: + +```shell +alembic upgrade verions_name +``` + +or relative upgrades: + +```shell +alembe upgrade +1 +``` + +## Downgrade a migration + +```shell +alembic downgrade -1 +``` + + + diff --git a/migrations/env.py b/migrations/env.py index fbc6aa8..f8750b1 100755 --- a/migrations/env.py +++ b/migrations/env.py @@ -1,4 +1,5 @@ import asyncio +import os from logging.config import fileConfig from sqlalchemy import pool @@ -40,7 +41,11 @@ def run_migrations_offline() -> None: script output. """ - url = config.get_main_option("sqlalchemy.url") + host = os.getenv('POSTGRES_HOST', 'localhost') + user = os.getenv('POSTGRES_USER') + passwd = os.getenv('POSTGRES_PASSWORD') + url = f'postgresql+asyncpg://{user}:{passwd}@{host}/{user}' + context.configure( url=url, target_metadata=target_metadata, diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..4dae514 Binary files /dev/null and b/requirements.txt differ diff --git a/src/database/db.py b/src/database/db.py index cae9fa9..b3e744c 100755 --- a/src/database/db.py +++ b/src/database/db.py @@ -1,9 +1,13 @@ +import os + from sqlalchemy import create_engine from sqlalchemy.ext.asyncio import async_sessionmaker, create_async_engine - -db_url = "postgresql+psycopg2://together:togetherno.1@localhost/together" -async_db_url = "postgresql+asyncpg://together:togetherno.1@localhost/together" +postgres_user = os.getenv('POSTGRES_USER') +postgres_passwd = os.getenv('POSTGRES_PASSWORD') +postgres_host = os.getenv('POSTGRES_HOST') +db_url = f"postgresql+psycopg2://{postgres_user}:{postgres_passwd}@{postgres_host}/{postgres_user}" +async_db_url = f"postgresql+asyncpg://{postgres_user}:{postgres_passwd}@{postgres_host}/{postgres_user}" engine = create_engine( db_url, ) diff --git a/src/database/redis_api.py b/src/database/redis_api.py index 387a5b6..b28f5a4 100755 --- a/src/database/redis_api.py +++ b/src/database/redis_api.py @@ -1,4 +1,6 @@ +import os + import redis -redis_server = redis.Redis(host='localhost', port=6379, decode_responses=True) +redis_server = redis.Redis(host=os.getenv('REDIS_HOST'), port=os.getenv('REDIS_PORT'), decode_responses=True)