Research
Security News
Malicious npm Packages Inject SSH Backdoors via Typosquatted Libraries
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.
flask-context-manager
Advanced tools
A lightweight dependency injection and route management system for Flask, inspired by Spring Boot.
The Flask Context Manager is a project that provides an inversion of control (IoC) container for Flask applications. It offers features reminiscent of the Spring Boot framework, including dependency injection, route management, configuration reading, and more.
pip install flask-context-manager
In the terminal, run the following command to initialize folder structure:
flask_context_manager start
This will create a folder structure so you can add inside your current package.
Dependency Injection: Enjoy automatic dependency injection. Classes with @Service
, @Controller
, or @Component
are managed automatically, and their dependencies are resolved via constructors.
Route Management: Define routes at the method level using @get_mapping
, @post_mapping
, @put_mapping
, @delete_mapping
. The @rest_mapping
adds a prefix to all routes in a controller.
Dynamic URL Handling: With dynamic URL routing, methods can easily fetch parameters from URLs.
POST Method Parameters: Design POST methods effortlessly by specifying parameters directly in the method.
Structure your application with directories for services (/service
), controllers (/controller
), and components (/component
).
service/hello_service.py
@Service
class HelloService:
def get_hello(self):
return "Hello, World!"
controller/hello_controller.py
@Controller
@rest_mapping('/api/v1')
class HelloController:
def __init__(self, hello_service: HelloService):
self.hello_service = hello_service
@get_mapping('hello')
def hello(self):
return self.hello_service.get_hello()
Similar to Spring Boot, we can add the @Configuration
annotation so that any annotated @Bean
methods are automatically registered in the context as soon as it starts.
config/app_config.py
@Configuration
class AppConfig:
@Bean
def my_jackson_copy(self):
return MyJacksonCopy()
@Bean
def i_just_run_and_return_nothing_and_its_ok(self):
print("First!!!")
The Flask Context Manager supports dynamic parameters in routes just like native Flask. To capture a portion of the URL as a variable, you can use the <variable_name>
syntax in your mapping.
controller/user_controller.py
@Controller
@rest_mapping('/api/v1')
class UserController:
@get_mapping('user/<user_id>')
def get_user_by_id(self, user_id):
return f"Fetching info for user with ID: {user_id}"
controller/test_controller.py
@Controller
class TestController:
@post_mapping("/test")
def my_post(body):
return "This is a cool body:" + str(body)
In the main application file, initiate the Context Manager:
app.py
from flask import Flask
from flask_context_manager.src.main.core.context_manager import ContextManager
app = Flask(__name__)
ContextManager.append(app)
if __name__ == "__main__":
ContextManager.start()
Note: This project is mainly for educational purposes. Ensure thorough testing and code review before deploying in a production setting.
FAQs
A lightweight dependency injection and route management system for Flask, inspired by Spring Boot.
We found that flask-context-manager demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Research
Security News
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.
Security News
MITRE's 2024 CWE Top 25 highlights critical software vulnerabilities like XSS, SQL Injection, and CSRF, reflecting shifts due to a refined ranking methodology.
Security News
In this segment of the Risky Business podcast, Feross Aboukhadijeh and Patrick Gray discuss the challenges of tracking malware discovered in open source softare.