Skip to content

Commit

Permalink
feat: add CORS support
Browse files Browse the repository at this point in the history
  • Loading branch information
gazev committed Nov 14, 2024
1 parent 23c6c90 commit 378f96c
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 16 deletions.
15 changes: 9 additions & 6 deletions .env.example
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
# Please provided relative paths with ./ or things can break :)

SESSION_DIR="./data/flask_sessions/"
SESSION_LIFETIME="3600"
SESSION_DIR="data/flask_sessions/"
SESSION_LIFETIME="10800" # 3 hours

DATABASE_PATH="./data/hackerschool.sqlite3"
ROLES_PATH="./data/roles.json"
PHOTOS_DIR="./data/photos/"
DATABASE_PATH="data/hackerschool.sqlite3"
ROLES_PATH="data/roles.json"
PHOTOS_DIR="data/photos/"
MAX_FILE_UPLOAD_LENGTH="16777216" # 16 MiB

LOG_LEVEL="INFO"
LOGS_PATH="./data/logs/app.log"
LOGS_PATH="data/logs/app.log"

ADMIN_USERNAME="admin"
ADMIN_PASSWORD="admin"

FRONTEND_URI="http://localhost:3000"
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ source .venv/bin/activate
pip install -r requirements.txt
```
4. **Setup environment variables**: Create a `.env` file and add any necessary environment variables (see [Environment Variables](#environment-variables) section).

5. **Setup the database and create an admin user**:
```bash
flask init-db
Expand All @@ -64,7 +65,9 @@ flask run --debug


### Docker Setup (Optional)
1. **Create all application-data folders**: `docker compose` will mount the database and required folders into the container. For this you need to initialize the database, please refer to the [installation steps 1 to 5](#installation) to set this up.
1. **Create all application-data folders**:
`docker compose` will mount the database and required folders into the container. For this you need to initialize the database, please refer to the [installation steps 1 to 5](#installation) to set this up.

1. **Build the docker image**:
```bash
docker compose build
Expand Down
4 changes: 4 additions & 0 deletions app/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import logging

from flask import Flask
from flask_cors import CORS

from app.config import Config
from app.extensions import session
Expand All @@ -23,6 +24,9 @@ def create_app(config_class=Config):
register_error_handlers(flask_app)
register_commands(flask_app)

if (frontend_uri := flask_app.config.get("FRONTEND_URI", "")) != "":
CORS(flask_app, origins=[frontend_uri], supports_credentials=True)

setup_logger(flask_app)

return flask_app
Expand Down
17 changes: 10 additions & 7 deletions app/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,20 +27,23 @@ class Config:
SESSION_TYPE = "filesystem"
PERMANENT_SESSION = True
SESSION_REFRESH_EACH_REQUEST = False
PERMANENT_SESSION_LIFETIME = _get_int_env_or_default("SESSION_LIFETIME", 12 * 60 * 60)
SESSION_KEY_PREFIX = _get_env_or_default("SESSION_KEY_PREFIX", "hs_session_")
SESSION_FILE_DIR = os.path.join(basedir, _get_env_or_default("SESSION_DIR", "data/flask_sessions/")).rstrip("/")
SESSION_KEY_PREFIX = "hs_session_"

ROLES_PATH = os.path.join(basedir, _get_env_or_default("ROLES_PATH", "data/roles.json"))
PERMANENT_SESSION_LIFETIME = _get_int_env_or_default("SESSION_LIFETIME", 3 * 60 * 60)
SESSION_FILE_DIR = os.path.join(basedir, _get_env_or_default("SESSION_DIR", "data/flask_sessions/")).rstrip("/")

DATABASE_PATH = os.path.join(basedir, _get_env_or_default("DATABASE_PATH", "data/hackerschool.sqlite3"))
SQLALCHEMY_DATABASE_URI = 'sqlite:///' + DATABASE_PATH

ROLES_PATH = os.path.join(basedir, _get_env_or_default("ROLES_PATH", "data/roles.json"))

PHOTOS_DIR = os.path.join(basedir, _get_env_or_default("PHOTOS_DIR", "data/photos/")).rstrip("/")
MAX_CONTENT_LENGTH = _get_int_env_or_default("MAX_PHOTO_SIZE", 16 * 1000 * 1000) ## max pohto size
MAX_CONTENT_LENGTH = _get_int_env_or_default("MAX_FILE_UPLOAD_LENGTH", 16 * 1024 * 1024)

LOGS_PATH = os.path.join(basedir, _get_env_or_default("LOGS_PATH", ""))
LOG_LEVEL = _get_env_or_default("LOG_LEVEL", "INFO")

ADMIN_USERNAME = _get_env_or_default("ADMIN_USERNAME", "")
ADMIN_PASSWORD = _get_env_or_default("ADMIN_PASSWORD", "")
FRONTEND_URI = _get_env_or_default("FRONTEND_URI", "http://localhost:3000")

ADMIN_USERNAME = _get_env_or_default("ADMIN_USERNAME", "admin")
ADMIN_PASSWORD = _get_env_or_default("ADMIN_PASSWORD", "admin")
6 changes: 4 additions & 2 deletions docker-compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ services:
- SESSION_DIR=/hs-api/data/flask_sessions/
- PHOTOS_DIR=/hs-api/data/photos/
- LOGS_PATH=/hs-api/data/logs/app.log
- LOG_LEVEL=INFO
- SESSION_LIFETIME=10800
- LOG_LEVEL=${LOG_LEVEL}
- SESSION_LIFETIME=${SESSION_LIFETIME}
- MAX_FILE_UPLOAD_LENGTH=${MAX_FILE_UPLOAD_LENGTH}
- FRONTEND_URI=${FRONTEND_URI}

0 comments on commit 378f96c

Please sign in to comment.