Flask framework with out of the box Logging, MongoDB, JWT, CORs, Sentry and Docker support
Project description
Flongo Framework
A framework for rapid application development on Flask. Includes:
- Built-in easy HTTP request/response management and error handling
- Built-in query string and response body parsing / normalization
- Built-in request and response payload validation using JSONSchema
- Built-in CORS handling and request origin configuration
- Built-in endpoint permissions handling with cookie-based JWT
- Built-in bindings for MongoDB including fixtures and index management
- Built-in automatic CRUD handling for endpoints given a MongoDB collection name
- Built-in detailed logging and fine-tuneable configurations
- Built-in Sentry integration with detailed spans for request tracing
- Dockerfile/Docker Compose to easily build and run the application
Sample App
# app
from src.application import Application
# routing
from src.api.routing import App_Routes, Route, Route_Schema, \
Route_Handler, Default_Route_Handler, Route_Permissions
# responses
from src.api.responses import API_JSON_Response, API_Message_Response
from src.api.responses.errors import API_Error_Message, API_Error_Response
# database
from src.database.mongodb.index import MongoDB_Indices, MongoDB_Index
from src.database.mongodb.fixture import MongoDB_Fixtures, MongoDB_Fixture
# settings
from src.config.settings import App_Settings, Flask_Settings, MongoDB_Settings
# enums
from src.config.enums.logs.log_levels import LOG_LEVELS
# typing
from typing import Any
from bson import ObjectId
from datetime import datetime
from src.utils.jwt.jwt_manager import App_JWT_Manager
# Method that throws a sample error
def throw(exception_type:type, msg:Any):
raise exception_type(msg)
# Simple sample application
routes = App_Routes(
Route(
# Route that demonstrates built-in basic request handling
url='/request',
handler=Route_Handler(
GET=lambda request, payload, collection: API_Message_Response("Sample GET request"),
POST=lambda request, payload, collection: API_JSON_Response({'sample_record': f'{payload["_id"]}', 'created': True}, 201),
PUT=lambda request, payload, collection: API_JSON_Response({'payload': payload}),
DELETE=lambda request, payload, collection: API_JSON_Response({'date': datetime.now(), 'deleted': True}),
),
# Demonstrates HTTP method based schema validation
request_schema=Route_Schema(
POST={
'type': 'object',
'additionalProperties': False,
'properties': {
'_id': {'type': 'integer'}
},
'required': ['_id']
}
),
response_schema=Route_Schema(
PUT={
'type': 'object',
'additionalProperties': False,
'properties': {
'payload': {'type': 'object'}
},
'required': ['payload']
}
),
log_level=LOG_LEVELS.DEBUG
),
Route(
# Route that demonstrates built-in basic error handling
url='/error',
handler=Route_Handler(
GET=lambda request, payload, collection: throw(ValueError, "Oh no! A value error!"),
POST=lambda request, payload, collection: throw(API_Error_Message, "Oh no! An API error!"),
PUT=lambda request, payload, collection: throw(API_Error_Response, {'data': payload, 'error': 'Oh no!'}),
),
log_level=LOG_LEVELS.DEBUG
),
Route(
# Route that demonstrates built-in database handling
url='/database',
handler=Route_Handler(
# Custom handlers allow a POST request or a GET request to create different errors
POST=lambda request, payload, collection: API_Message_Response(collection.insert_one(payload) if collection != None else 'No collection!'),
GET=lambda request, payload, collection: API_Message_Response(collection.find_one(payload) if collection != None else 'No collection!')
),
log_level=LOG_LEVELS.DEBUG,
collection_name='sample'
),
Route(
# Route that demonstrates built-in default CRUD handling
url='/default',
handler=Default_Route_Handler(),
log_level=LOG_LEVELS.DEBUG,
collection_name='default'
),
Route(
# Route that demonstrates built-in permissions handling
url='/permissions',
handler=Default_Route_Handler(
# Authentication route that sets the JWT in response cookies
GET=lambda request, payload, collection: App_JWT_Manager.add_response_jwt(
response=API_Message_Response("Authenticated!"),
_id="test",
roles="user"
),
# De-authentication route that removes the JWT in response cookies
DELETE=lambda request, payload, collection: App_JWT_Manager.remove_response_jwt(
response=API_Message_Response("Logged out!"),
)
),
log_level=LOG_LEVELS.DEBUG,
collection_name='permissions',
permissions=Route_Permissions(POST='user', PUT='admin')
),
)
# Application settings
settings = App_Settings(
flask=Flask_Settings(
env="local",
debug_mode=True,
log_level=LOG_LEVELS.DEBUG,
config_log_level=LOG_LEVELS.DEBUG
),
mongodb=MongoDB_Settings(
log_level=LOG_LEVELS.DEBUG
)
)
# Application Database Indices
indices = MongoDB_Indices(
MongoDB_Index("sample", "name")
)
# Application Database Fixtures
fixtures = MongoDB_Fixtures(
MongoDB_Fixture("sample", {"_id": ObjectId("652790328c73b750984aee34"), "name": "Peter"})
)
# Create application
app = Application(routes=routes, settings=settings, indices=indices, fixtures=fixtures)
# Binding directly to Flask for Gunicorn or VSCode
def get_app():
return app.app
if __name__ == '__main__':
# Run application
app.run()
Running With Docker
Building
From the root directory run:
docker build -t <your_image_name> -f docker/Dockerfile .
Running
Run your image from the Docker GUI or with
docker run <your_image_name>
Note: that MongoDB must be configured on the same Docker network as the app (Use Docker Compose to do this automatically)
Running With Docker Compose
Since the server might depend on MongoDB, you can use Docker Compose to start the Dockerized application in conjuction with a Dockerized MongoDB instance
Building the Server Container
From the docker/ directory containing docker-compose.yml
, run:
docker-compose build
Running the Server + MongoDB
From the docker/ directory containing docker-compose.yml
, run:
docker-compose up --force-recreate
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
flongo_framework-0.2.6.tar.gz
(16.4 kB
view hashes)
Built Distribution
Close
Hashes for flongo_framework-0.2.6-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 4a191714bb7051c072a7b92b734c09350a9ad93626890979462bc4351b41441f |
|
MD5 | 172f4efc06a3f78569c882ac68aa3255 |
|
BLAKE2b-256 | 84006749923f366adc80f56622e5c7da761dd275dfbb4fb139260f687c338c5f |