This commit is contained in:
3
mealapi/utils/__init__.py
Normal file
3
mealapi/utils/__init__.py
Normal file
@ -0,0 +1,3 @@
|
||||
from .config import Config
|
||||
from .error_handlers import register_all_error_handlers
|
||||
from .healthchecks import health_database_status
|
11
mealapi/utils/config.py
Normal file
11
mealapi/utils/config.py
Normal file
@ -0,0 +1,11 @@
|
||||
#!/usr/bin/env python3
|
||||
import os
|
||||
|
||||
|
||||
class Config:
|
||||
PORT = 8080
|
||||
DEBUG = os.environ.get("INPUT_SERVICE_DEBUG", "true").lower() in ["true", "yes", "1"]
|
||||
|
||||
SQLALCHEMY_DATABASE_URI = os.environ.get('DATABASE_URI')
|
||||
# disable this for better performance
|
||||
SQLALCHEMY_TRACK_MODIFICATIONS = False
|
18
mealapi/utils/error_handlers.py
Normal file
18
mealapi/utils/error_handlers.py
Normal file
@ -0,0 +1,18 @@
|
||||
#!/usr/bin/env python3
|
||||
|
||||
|
||||
def get_standard_error_handler(code: int):
|
||||
def error_handler(err):
|
||||
return {"msg": str(err)}, code
|
||||
|
||||
return error_handler
|
||||
|
||||
|
||||
# function to register all handlers
|
||||
|
||||
|
||||
def register_all_error_handlers(app):
|
||||
error_codes_to_override = [404, 403, 401, 405, 400, 409, 422, 500]
|
||||
|
||||
for code in error_codes_to_override:
|
||||
app.register_error_handler(code, get_standard_error_handler(code))
|
14
mealapi/utils/healthchecks.py
Normal file
14
mealapi/utils/healthchecks.py
Normal file
@ -0,0 +1,14 @@
|
||||
#!/usr/bin/env python3
|
||||
|
||||
from model import db
|
||||
|
||||
|
||||
def health_database_status():
|
||||
is_database_working = True
|
||||
output = 'database is ok'
|
||||
try:
|
||||
db.session.execute('SELECT 1')
|
||||
except Exception as e:
|
||||
output = str(e)
|
||||
is_database_working = False
|
||||
return is_database_working, output
|
Reference in New Issue
Block a user