Skip to content

Commit

Permalink
Add flask-session
Browse files Browse the repository at this point in the history
  • Loading branch information
mvalik committed Nov 25, 2023
1 parent 532fc7a commit 341ca36
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 5 deletions.
28 changes: 27 additions & 1 deletion poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ flask-oidc = "^2.1.1"
Flask-SQLAlchemy = "^3.1.1"
Flask-Cors = "^4.0.0"
Flask-Migrate = "^4.0.5"
Flask-Session = "^0.5.0"
itsdangerous = {version = "==2.0.1", optional = true}
# causes a problem with OIDC callback (returns 500)
Werkzeug = "^3.0.1"
Expand Down
24 changes: 20 additions & 4 deletions waiverdb/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from flask import Flask, current_app, send_from_directory
from flask_cors import CORS
from flask_migrate import Migrate
from flask_session import Session
from sqlalchemy import event, text
from sqlalchemy.exc import ProgrammingError
import requests
Expand Down Expand Up @@ -85,7 +86,7 @@ def populate_db_config(app):


# applicaiton factory http://flask.pocoo.org/docs/0.12/patterns/appfactories/
def create_app(config_obj=None):
def create_app(config_obj=None) -> Flask:
app = Flask(__name__)

if config_obj:
Expand All @@ -102,15 +103,17 @@ def create_app(config_obj=None):
app.register_error_handler(requests.Timeout, json_error)

populate_db_config(app)
if 'OIDC' in auth_methods(app):
oidc.init_app(app)
app.oidc = oidc
# initialize logging
init_logging(app)
# initialize tracing
init_tracing(app)
# initialize db
db.init_app(app)
init_session(app)
if 'OIDC' in auth_methods(app):
oidc.init_app(app)
app.oidc = oidc

# initialize db migrations
migrations_dir = os.path.join(os.path.abspath(os.path.dirname(__file__)),
'migrations')
Expand All @@ -131,6 +134,19 @@ def create_app(config_obj=None):
return app


def init_session(app: Flask) -> None:
app.config["SESSION_SQLALCHEMY"] = db
app.server_session = Session(app)
if app.config["SESSION_TYPE"] == "sqlalchemy":
import sqlalchemy

with app.app_context():
inspect = sqlalchemy.inspect(db.engine)
table = app.config["SESSION_SQLALCHEMY_TABLE"]
if not inspect.has_table(table):
db.create_all()


def healthcheck():
"""
Request handler for performing an application-level health check. This is
Expand Down
7 changes: 7 additions & 0 deletions waiverdb/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,13 @@ class Config(object):
OTEL_EXPORTER_OTLP_METRICS_ENDPOINT = None
OTEL_EXPORTER_SERVICE_NAME = "waiverdb"

SESSION_TYPE = "sqlalchemy"
SESSION_SQLALCHEMY_TABLE = "sessions"
SESSION_PERMANENT = True
SESSION_USE_SIGNER = True
SESSION_COOKIE_SECURE = True
SESSION_COOKIE_SAMESITE = "Lax"


class ProductionConfig(Config):
DEBUG = False
Expand Down

0 comments on commit 341ca36

Please sign in to comment.