add environment variables

main
3wish 2024-04-11 17:38:13 +08:00
parent 622748cb37
commit 65d1a6233e
12 changed files with 129 additions and 11 deletions

11
.dev.Dockerfile 100644
View File

@ -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"]

5
.dev.env 100644
View File

@ -0,0 +1,5 @@
POSTGRES_HOST=together-postgres
POSTGRES_USER=together
POSTGRES_PASSWORD=togetherno.1
REDIS_HOST=together-redis
REDIS_PORT=6379

6
.dockerignore 100644
View File

@ -0,0 +1,6 @@
.venv
.idea
__pycache__
static/temp/
static/chat/

5
.local.env 100644
View File

@ -0,0 +1,5 @@
POSTGRES_HOST=localhost
POSTGRES_USER=together
POSTGRES_PASSWORD=togetherno.1
REDIS_HOST=localhost
REDIS_PORT=6379

8
.prod.Dockerfile 100644
View File

@ -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"]

0
.prod.env 100644
View File

12
Pipfile.lock generated
View File

@ -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": {

72
README.md 100644
View File

@ -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
```

View File

@ -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,

BIN
requirements.txt 100644

Binary file not shown.

View File

@ -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,
)

View File

@ -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)