From 65d1a6233eb37e0c5a5f530d159552b872796914 Mon Sep 17 00:00:00 2001 From: 3wish Date: Thu, 11 Apr 2024 17:38:13 +0800 Subject: [PATCH] add environment variables --- .dev.Dockerfile | 11 ++++++ .dev.env | 5 +++ .dockerignore | 6 ++++ .local.env | 5 +++ .prod.Dockerfile | 8 +++++ .prod.env | 0 Pipfile.lock | 12 +++---- README.md | 72 ++++++++++++++++++++++++++++++++++++++ migrations/env.py | 7 +++- requirements.txt | Bin 0 -> 2512 bytes src/database/db.py | 10 ++++-- src/database/redis_api.py | 4 ++- 12 files changed, 129 insertions(+), 11 deletions(-) create mode 100644 .dev.Dockerfile create mode 100644 .dev.env create mode 100644 .dockerignore create mode 100644 .local.env create mode 100644 .prod.Dockerfile create mode 100644 .prod.env create mode 100644 README.md create mode 100644 requirements.txt 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 0000000000000000000000000000000000000000..4dae514d008c7b2bf321c06b11b2fc634f03d873 GIT binary patch literal 2512 zcmZve>r&f542Ac1XZj9(0aId9S~}AwVfc|7;9P7sj)C;y+n!HaWfO++m?XQB_UIf* z%fG)>YSNeVoDO>WG^C&DtDc=!_j)?LL%K?{wAIQnjj7eIh1Pen>b25q{i|#mt@N^5 zrO)Y;>>JrU>Mtup`lY8%H}b9ZPO=SqyEK>8!Yn4XhgDeY(r-n@`&;(C&c3aXbizE- zyGqMc=@T?@+O_O&^%nc1VItO4>zopMgFIVdAO?A?6nWJ5uqD<_J~wRp^kn<&)#>gO zZLT1?;=yZ4?#=ZWMTO$S^%oY|verwUN-5X|@25qe< zwdF@dKXV}+V&54qyetgMB5ln3hkUU3o};5VdzE2W3K6`(&rE!Ao--4Am1vdK!YGa! zS-Spy_D8m4N$s3R3^qEgQ-w2~J3CpRU~mv@cRCHbUe$aw%c&TE*DF;IRk<`QRP8}F zUCsd;81b50DdVLOm$N2k>Qb-N_Ewy{=o<_z^?5Liu7Gps(H^xoXcttC^u%MEwFK8h zkAH#>{3|`JY*z9jp4VyA-?MoHPZLwxnR-_IQZ0c5Z?A&raC#RzEKsDd6BgtG>#iF6 zg`Kc@Gk^AKRZG+^T(rtDaJxc{G8v4eRvA*UR8hicp_ofYEzzBR{6a zXnU8PfGX@qR^;to?CiBSN&!@T%(zO<+ePt=O@MI|_|@8eZE^-Zzq!1uE_&M8>WkyM75`v>)( z^s8V;g+L!Y%QW(-&y1!Q!``Rxm2&MAcX2;C#MRs@PEo!RhL_{;0=Wv_;)r&c)|*ubkCw<~o>36p49u3=L-{S? zoJ8zVbwPhH5nAQyd9-#OH@0URnT8iWZO)PN3f5P#bujiW{~PiM=XQ(qYRIVb;L5ws Wm3>wGx$ty~wlP+nor$;%9RC40Ep-_H literal 0 HcmV?d00001 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)