Skip to content

Commit

Permalink
fixed db.session issue
Browse files Browse the repository at this point in the history
  • Loading branch information
dinesh-aot committed Jul 30, 2024
1 parent a3efce2 commit 0de9984
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 43 deletions.
21 changes: 14 additions & 7 deletions compliance-api/src/compliance_api/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,6 @@ def create_app(run_mode=os.getenv("FLASK_ENV", "development")):

CORS(app, resources={r"/*": {"origins": allowedorigins()}}, supports_credentials=True)

# Register blueprints
app.register_blueprint(API_BLUEPRINT) # Create the database (run once)
app.register_blueprint(OPS_BLUEPRINT)

# Setup jwt for keycloak
if os.getenv("FLASK_ENV", "production") != "testing":
setup_jwt_manager(app, jwt)
Expand All @@ -65,11 +61,13 @@ def create_app(run_mode=os.getenv("FLASK_ENV", "development")):

# # Database migrate initialize
migrate.init_app(app, db)
with app.app_context():
db.create_all()

# Marshmallow initialize
ma.init_app(app)
# Register blueprints
app.register_blueprint(API_BLUEPRINT) # Create the database (run once)
app.register_blueprint(OPS_BLUEPRINT)
register_shellcontext(app)

@app.before_request
def set_origin():
Expand All @@ -93,7 +91,6 @@ def handle_error(err):
raise err
current_app.logger.error(str(err))
return "Internal server error", HTTPStatus.INTERNAL_SERVER_ERROR

# Return App for run in run.py file
return app

Expand All @@ -111,3 +108,13 @@ def get_roles(a_dict):

app_context.config["JWT_ROLE_CALLBACK"] = get_roles
jwt_manager.init_app(app_context)

def register_shellcontext(app):
"""Register shell context objects."""
from api import models # pylint: disable=import-outside-toplevel

def shell_context():
"""Shell context objects."""
return {'app': app, 'jwt': jwt, 'db': db, 'models': models} # pragma: no cover

app.shell_context_processor(shell_context)
1 change: 1 addition & 0 deletions compliance-api/src/compliance_api/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ class _Config(): # pylint: disable=too-few-public-methods
SQLALCHEMY_DATABASE_URI = f'postgresql://{DB_USER}:{DB_PASSWORD}@{DB_HOST}:{int(DB_PORT)}/{DB_NAME}'
SQLALCHEMY_ECHO = True
SQLALCHEMY_TRACK_MODIFICATIONS = False
SQLALCHEMY_RECORD_QUERIES=True

# JWT_OIDC Settings
JWT_OIDC_WELL_KNOWN_CONFIG = os.getenv('JWT_OIDC_WELL_KNOWN_CONFIG')
Expand Down
10 changes: 4 additions & 6 deletions compliance-api/src/compliance_api/resources/ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,9 @@
# limitations under the License.
"""Endpoints to check and manage the health of the service."""
from flask_restx import Namespace, Resource
from flask import current_app
from sqlalchemy import exc, text

from submit_api.models import db


API = Namespace('ops', description='Service - OPS checks')

SQL = text('select 1')
Expand All @@ -34,9 +32,9 @@ class Healthz(Resource):
def get():
"""Return a JSON object stating the health of the Service and dependencies."""
try:
db.session.execute(SQL)
current_app.extensions['sqlalchemy'].session.execute(SQL)
except exc.SQLAlchemyError:
return {'message': 'api is down'}, 500
return {"message": "api is down"}, 500

# made it here, so all checks passed
return {'message': 'api is healthy'}, 200
Expand All @@ -50,4 +48,4 @@ class Readyz(Resource):
def get():
"""Return a JSON object that identifies if the service is setupAnd ready to work."""
# TODO: add a poll to the DB when called
return {'message': 'api is ready'}, 200
return {'message': 'api is ready'}, 200
1 change: 0 additions & 1 deletion compliance-api/src/compliance_api/resources/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
from compliance_api.exceptions import ResourceNotFoundError
from compliance_api.auth import auth
from .apihelper import Api as ApiHelper

API = Namespace("users", description="Endpoints for User Management")
"""Custom exception messages
"""
Expand Down
16 changes: 0 additions & 16 deletions deployment/charts/compliance-api/templates/patroni-secret.yaml

This file was deleted.

14 changes: 1 addition & 13 deletions deployment/charts/compliance-api/values.dev.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,8 @@ resources:
database:
secret: compliance-patroni
service:
name: submit-patroni
port: 5432
patroni:
name: compliance-patroni
appDbName: YXBw
appDbPassword: WjFCZUZHNlVrdzlzUG1BMkh2NFFvUnQ4THkzTnhEY0s=
appDbUsername: Y29tcGxpYW5jZV91c2Vy
replicationPassword: c3lXTTVBa1Y2ak1pZzJLRVJuR3NDc1RJZGw0N1dpT2M=
replicationUsername: cmVwbGljYXRpb24=
superuserPassword: bGtmdG5RT1loRlJwQXZrSDltaW1NTWVsUW5Qb2VWdzM=
superuserUsername: cG9zdGdyZXM=



port: 5432

service:
type: ClusterIP
Expand Down

0 comments on commit 0de9984

Please sign in to comment.