update alembic to use environment to get postgresql username, password and host

main
3wish 2024-04-15 16:19:43 +08:00
parent 65d1a6233e
commit 166d71272d
5 changed files with 34 additions and 11 deletions

View File

@ -1,11 +1,10 @@
FROM python:3
WORKDIR /app
WORKDIR /backend
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"]
CMD ["sh", "-c", "alembic upgrade head && uvicorn src.main:app --host 0.0.0.0 --port 8000 --env-file ./.dev.env"]

View File

@ -1,8 +1,10 @@
FROM python:3
WORKDIR /app
WORKDIR /backend
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", "./.prod.env"]
CMD ["sh", "-c", "alembic upgrade head && uvicorn src.main:app --host 0.0.0.0 --port 8000 --env-file ./.prod.env"]

View File

@ -68,5 +68,21 @@ alembe upgrade +1
alembic downgrade -1
```
# Run Container
Create a volume for persisting data:
```shell
docker volumn create together-backend
```
Run a container and mount the volume:
```shell
docker run --name together-backend \
-v together-backend:/backend/static \
-itd 120.77.202.88/together-backend-dev:v1.0
```

View File

@ -28,6 +28,13 @@ target_metadata = Base.metadata
# my_important_option = config.get_main_option("my_important_option")
# ... etc.
host = os.getenv("POSTGRES_HOST", "localhost")
user = os.getenv("POSTGRES_USER")
password = os.getenv("POSTGRES_PASSWORD")
url = f"postgresql+asyncpg://{user}:{password}@{host}/{user}"
config.set_main_option("sqlalchemy.url", url)
def run_migrations_offline() -> None:
"""Run migrations in 'offline' mode.
@ -41,10 +48,9 @@ def run_migrations_offline() -> None:
script output.
"""
host = os.getenv('POSTGRES_HOST', 'localhost')
user = os.getenv('POSTGRES_USER')
passwd = os.getenv('POSTGRES_PASSWORD')
url = f'postgresql+asyncpg://{user}:{passwd}@{host}/{user}'
# url = config.get_main_option("sqlalchemy.url")
context.configure(
url=url,

View File

@ -6,7 +6,7 @@ from src.database.redis_api import redis_server
host = "smtp.163.com"
username = "together_app@163.com"
password = "ZHHLUQSTDTQSBEQZ"
password = "AIAMTGHKFWKHXSUY"
smtp = SMTP(host=host)
smtp.ehlo()