Skip to content

Commit fe00313

Browse files
first commit
0 parents  commit fe00313

File tree

6 files changed

+73
-0
lines changed

6 files changed

+73
-0
lines changed

.env

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
ANOTHER_SECRET_KEY=base
2+
PORT=5080
3+
USERNAME=base

config.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import os
2+
from typing import Union
3+
4+
from pydantic import BaseSettings, Field
5+
6+
7+
class Base(BaseSettings):
8+
secret_key: str = Field('random_string', env='ANOTHER_SECRET_KEY')
9+
port: int = 5050
10+
username: str = "ANAND"
11+
12+
class Config:
13+
case_sensitive = False
14+
env_file = '.env'
15+
16+
17+
class Dev(Base):
18+
username = "TRIPATHI"
19+
20+
class Config:
21+
env_file = 'dev.env'
22+
23+
24+
class Prod(Base):
25+
username = "Production"
26+
port = 5051
27+
28+
class Config:
29+
env_file = 'prod.env'
30+
31+
32+
config = dict(
33+
dev=Dev,
34+
prod=Prod
35+
)
36+
settings: Union[Dev, Prod] = config[os.environ.get('ENV', 'dev').lower()]()

dev.env

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
ANOTHER_SECRET_KEY=dev
2+
PORT=5080
3+
USERNAME=DevUsername

main.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
from flask import Flask
2+
3+
from dotenv import load_dotenv
4+
5+
from config import settings
6+
7+
load_dotenv() # take environment variables from .env.
8+
9+
app = Flask(__name__)
10+
11+
12+
@app.get("/")
13+
async def hello_world():
14+
template = f'''
15+
<p>Hello, {vars(settings)}!</p>
16+
'''
17+
return template
18+
19+
if __name__ == '__main__':
20+
app.run(port=settings.port)

prod.env

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
ANOTHER_SECRET_KEY=prod
2+
PORT=5090
3+
USERNAME=ProdUsername

run_with_env.sh

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
ENV_FILE="$1"
2+
CMD=${@:2}
3+
4+
set -o allexport
5+
source $ENV_FILE
6+
set +o allexport
7+
8+
$CMD

0 commit comments

Comments
 (0)