Skip to content

Commit c84f0ad

Browse files
committed
updated config
1 parent cfba655 commit c84f0ad

File tree

3 files changed

+19
-58
lines changed

3 files changed

+19
-58
lines changed

.flaskenv

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,2 @@
11
FLASK_RUN_PORT=8080
2-
FLASK_APP=wsgi.py
3-
FLASK_DEBUG=true
2+
FLASK_APP=wsgi.py

App/config.py

Lines changed: 15 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,18 @@
11
import os
2-
import importlib.util
3-
import sys
42

5-
def load_config():
6-
config = {'ENV': os.environ.get('ENV', 'DEVELOPMENT')}
7-
config_base_path = './App'
8-
9-
# Check for custom_config.py file; adjust path as needed
10-
custom_config_path = os.path.join(config_base_path, 'custom_config.py')
11-
default_config_path = os.path.join(config_base_path, 'default_config.py')
12-
13-
if os.path.exists(custom_config_path):
14-
spec = importlib.util.spec_from_file_location("custom_config", custom_config_path)
15-
else:
16-
spec = importlib.util.spec_from_file_location("default_config", default_config_path)
17-
18-
config_module = importlib.util.module_from_spec(spec)
19-
sys.modules[spec.name] = config_module
20-
spec.loader.exec_module(config_module)
21-
22-
if config['ENV'] == "DEVELOPMENT":
23-
config['SQLALCHEMY_DATABASE_URI'] = config_module.SQLALCHEMY_DATABASE_URI
24-
config['SECRET_KEY'] = config_module.SECRET_KEY
25-
config['JWT_SECRET_KEY'] = config_module.SECRET_KEY
3+
def load_config(app, overrides):
4+
if os.path.exists(os.path.join('./App', 'custom_config.py')):
5+
app.config.from_object('App.custom_config')
266
else:
27-
config['SQLALCHEMY_DATABASE_URI'] = os.environ.get('SQLALCHEMY_DATABASE_URI', config_module.SQLALCHEMY_DATABASE_URI)
28-
config['SECRET_KEY'] = os.environ.get('SECRET_KEY', config_module.SECRET_KEY)
29-
config['JWT_SECRET_KEY'] = os.environ.get('JWT_SECRET_KEY', config_module.SECRET_KEY)
30-
config['DEBUG'] = config['ENV'].upper() != 'PRODUCTION'
31-
32-
# Default configurations that don't depend on the environment
33-
config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
34-
config['TEMPLATES_AUTO_RELOAD'] = True
35-
config['PREFERRED_URL_SCHEME'] = 'https'
36-
config['UPLOADED_PHOTOS_DEST'] = "App/uploads"
37-
38-
return config
39-
40-
config = load_config()
7+
app.config.from_object('App.default_config')
8+
app.config.from_prefixed_env()
9+
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
10+
app.config['TEMPLATES_AUTO_RELOAD'] = True
11+
app.config['PREFERRED_URL_SCHEME'] = 'https'
12+
app.config['UPLOADED_PHOTOS_DEST'] = "App/uploads"
13+
app.config['JWT_ACCESS_COOKIE_NAME'] = 'access_token'
14+
app.config["JWT_TOKEN_LOCATION"] = ["cookies", "headers"]
15+
app.config["JWT_COOKIE_SECURE"] = True
16+
app.config["JWT_COOKIE_CSRF_PROTECT"] = False
17+
for key in overrides:
18+
app.config[key] = overrides[key]

App/main.py

Lines changed: 3 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,9 @@
44
from flask_cors import CORS
55
from werkzeug.utils import secure_filename
66
from werkzeug.datastructures import FileStorage
7-
from datetime import timedelta
87

98
from App.database import init_db
10-
from App.config import config
9+
from App.config import load_config
1110

1211
from App.controllers import (
1312
setup_jwt,
@@ -20,24 +19,9 @@ def add_views(app):
2019
for view in views:
2120
app.register_blueprint(view)
2221

23-
def configure_app(app, config, overrides):
24-
for key, value in config.items():
25-
if key in overrides:
26-
app.config[key] = overrides[key]
27-
else:
28-
app.config[key] = config[key]
29-
30-
def create_app(config_overrides={}):
22+
def create_app(overrides={}):
3123
app = Flask(__name__, static_url_path='/static')
32-
configure_app(app, config, config_overrides)
33-
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
34-
app.config['TEMPLATES_AUTO_RELOAD'] = True
35-
app.config['PREFERRED_URL_SCHEME'] = 'https'
36-
app.config['UPLOADED_PHOTOS_DEST'] = "App/uploads"
37-
app.config['JWT_ACCESS_COOKIE_NAME'] = 'access_token'
38-
app.config["JWT_TOKEN_LOCATION"] = ["cookies", "headers"]
39-
app.config["JWT_COOKIE_SECURE"] = True
40-
app.config["JWT_COOKIE_CSRF_PROTECT"] = False
24+
load_config(app, overrides)
4125
CORS(app)
4226
add_auth_context(app)
4327
photos = UploadSet('photos', TEXT + DOCUMENTS + IMAGES)

0 commit comments

Comments
 (0)