Skip to content

Commit

Permalink
Update config env
Browse files Browse the repository at this point in the history
  • Loading branch information
vectornguyen76 committed Mar 15, 2024
1 parent 39092eb commit fb6a552
Show file tree
Hide file tree
Showing 6 changed files with 74 additions and 52 deletions.
24 changes: 14 additions & 10 deletions .env
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
# APP configuration
APP_NAME="Flask API Rest Template"
APP_ENV="develop"
APP_NAME=Flask API Rest Template
APP_ENV=develop

# Flask Configuration
FLASK_APP= "app:app"
FLASK_DEBUG="true"
APP_SETTINGS_MODULE="config.DevelopConfig"
APP_TEST_SETTINGS_MODULE="config.TestingConfig"
FLASK_RUN_HOST="0.0.0.0"
FLASK_RUN_PORT="5000"
FLASK_APP=app:app
FLASK_DEBUG=true
APP_SETTINGS_MODULE=config.DevelopConfig
APP_TEST_SETTINGS_MODULE=config.TestingConfig
FLASK_RUN_HOST=0.0.0.0
FLASK_RUN_PORT=5000

# Secret key
SECRET_KEY=<your-secret-key>
JWT_SECRET_KEY=<your-jwt-secret-key>

# Database service configuration
DATABASE_URL="postgresql://db_user:db_password@localhost/db_dev"
DATABASE_TEST_URL="postgresql://db_user:db_password@localhost/db_test"
DATABASE_URL=postgresql://db_user:db_password@localhost/db_dev
DATABASE_TEST_URL=postgresql://db_user:db_password@localhost/db_test
4 changes: 4 additions & 0 deletions .env.api.local
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,9 @@ POSTGRES_DB=db_dev
POSTGRES_USER=db_user
PGPASSWORD=db_password

# Secret key
SECRET_KEY=<your-secret-key>
JWT_SECRET_KEY=<your-jwt-secret-key>

DATABASE_TEST_URL=postgresql+psycopg2://db_user:db_password@db_service:5432/db_test
DATABASE_URL=postgresql+psycopg2://db_user:db_password@db_service:5432/db_dev
4 changes: 4 additions & 0 deletions .env.pro
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ APP_SETTINGS_MODULE=config.ProductionConfig
API_HOST=0.0.0.0
API_PORT=5000

# Secret key
SECRET_KEY=<your-secret-key>
JWT_SECRET_KEY=<your-jwt-secret-key>

# Database service configuration
DATABASE_URL=sqlite:///production.db

Expand Down
70 changes: 43 additions & 27 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -100,19 +100,23 @@ Development environment that uses PostgreSQL in local and uses the server flask
3. Create or update **.env** file and enter the environment variables of the environment. Example:

```shell
# APP configuration
APP_NAME="Flask API Rest Template"
APP_ENV="develop"

# Flask Configuration
FLASK_APP= "app:app"
FLASK_DEBUG="true"
APP_SETTINGS_MODULE="config.DevelopConfig"
FLASK_RUN_HOST="0.0.0.0"
FLASK_RUN_PORT="5000"

# Database service configuration
DATABASE_URL="postgresql://db_user:db_password@localhost/db_dev"
# APP configuration
APP_NAME="Flask API Rest Template"
APP_ENV="develop"

# Flask Configuration
FLASK_APP= "app:app"
FLASK_DEBUG="true"
APP_SETTINGS_MODULE="config.DevelopConfig"
FLASK_RUN_HOST="0.0.0.0"
FLASK_RUN_PORT="5000"

# Secret key
SECRET_KEY=<your-secret-key>
JWT_SECRET_KEY=<your-jwt-secret-key>

# Database service configuration
DATABASE_URL="postgresql://db_user:db_password@localhost/db_dev"
```

4. Run:
Expand Down Expand Up @@ -182,19 +186,23 @@ Testing environment that uses PostgreSQL as database (db_test) and performs unit
3. Create or update **.env** file and enter the environment variables of the environment. Example:

```shell
# APP configuration
APP_NAME="Flask API Rest Template"
APP_ENV="testing"

# Flask Configuration
FLASK_APP= "app:app"
FLASK_DEBUG="true"
APP_SETTINGS_MODULE="config.TestingConfig"
FLASK_RUN_HOST="0.0.0.0"
FLASK_RUN_PORT="3000"

# Database service configuration
DATABASE_TEST_URL="postgresql://db_user:db_password@localhost/db_test"
# APP configuration
APP_NAME="Flask API Rest Template"
APP_ENV="testing"

# Flask Configuration
FLASK_APP= "app:app"
FLASK_DEBUG="true"
APP_SETTINGS_MODULE="config.TestingConfig"
FLASK_RUN_HOST="0.0.0.0"
FLASK_RUN_PORT="3000"

# Secret key
SECRET_KEY=<your-secret-key>
JWT_SECRET_KEY=<your-jwt-secret-key>

# Database service configuration
DATABASE_TEST_URL="postgresql://db_user:db_password@localhost/db_test"
```

4. Init database:
Expand Down Expand Up @@ -276,6 +284,10 @@ Containerized services separately with PostgreSQL databases (db), API (api) and
POSTGRES_USER=<name_user> # For example db_user
PGPASSWORD=<password_user> # For example db_password
# Secret key
SECRET_KEY=<your-secret-key>
JWT_SECRET_KEY=<your-jwt-secret-key>
DATABASE_TEST_URL=<url database test> # For example postgresql+psycopg2://db_user:db_password@db_service:5432/db_test
DATABASE_URL=<url database> # For example postgresql+psycopg2://db_user:db_password@db_service:5432/db_dev
```
Expand Down Expand Up @@ -319,6 +331,10 @@ Apply CI/CD with Github Actions to automatically deployed to AWS platform use EC
# API service configuration
API_HOST=<api_host> # For example 0.0.0.0
# Secret key
SECRET_KEY=<your-secret-key>
JWT_SECRET_KEY=<your-jwt-secret-key>
# Database service configuration
DATABASE_URL=<url_database> # For example sqlite:///production.db
Expand Down Expand Up @@ -400,7 +416,7 @@ Apply CI/CD with Github Actions to automatically deployed to AWS platform use EC
## Swagger

```
http://localhost:port/swagger-ui
http://localhost:<port>/swagger-ui
```

## Reference
Expand Down
15 changes: 7 additions & 8 deletions app/utils/decorators.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,21 @@


def permission_required(permission_name):
def decorator_function(original_function):
@wraps(original_function)
def wrapper_function(*arg, **kwargs):
def decorator(func):
def wrapper(*arg, **kwargs):
jwt_data = get_jwt()
user_id = jwt_data["sub"]
user = UserModel.query.filter_by(id=user_id).first()
for role in user.roles:
for permission in role.permissions:
if permission.name == permission_name:
return original_function(*arg, **kwargs)
logger.error("User does not have permission to access this api!")
abort(403, message="User does not have permission to access this api!")
return func(*arg, **kwargs)
logger.error("User does not have permission to access this API!")
abort(403, message="User does not have permission to access this API!")

return wrapper_function
return wrapper

return decorator_function
return decorator


def time_profiling(func):
Expand Down
9 changes: 2 additions & 7 deletions config.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,13 @@ class DefaultConfig:

# Flask Configuration
APP_NAME = os.environ.get("APP_NAME")
SECRET_KEY = os.environ.get(
"SECRET_KEY", "e42ebf32a22c7cef7f4a33c71f90f0d8ea65e63144f952e57e1b39b26cc26a6f"
)
SECRET_KEY = os.environ.get("SECRET_KEY")
PROPAGATE_EXCEPTIONS = True
DEBUG = False
TESTING = False

# Configuration of Flask-JWT-Extended
JWT_SECRET_KEY = os.environ.get(
"JWT_SECRET_KEY",
"d7da6e940725de5a15b7e48f5a71f535a315c72a5372c1d3bb8691b38b5f29a1",
)
JWT_SECRET_KEY = os.environ.get("JWT_SECRET_KEY")
# Determines the minutes that the access token remains active
JWT_ACCESS_TOKEN_EXPIRES = datetime.timedelta(minutes=30)
# Determines the days that the refresh token remains active
Expand Down

0 comments on commit fb6a552

Please sign in to comment.