EasyAPI is a minimalist Python web framework designed for simplicity and ease of use. It allows you to define routes, handle HTTP requests, and manage middleware with a clean, visually appealing structure. Completely open-source, and contributions are welcome.
- Visually Appealing Route Registration: Define routes in a clear and structured manner without using decorators.
- Middleware Support: Easily integrate middleware for pre- and post-processing of requests and responses.
- Blueprints: Modularize your application into separate components, making it easy to manage larger projects.
- Customizeable Error Handling: Customize error pages and manage HTTP status codes effectively.
- Logging: Track application activity with built-in logging support.
- Static Files: Serve static assets like CSS, JavaScript, and images directly from your application.
- OAuth Integration: Support for OAuth 2.0 authentication using providers like Google, GitHub, and Facebook via Authlib.
- Payment Processing: Comprehensive Stripe integration for handling payments, subscriptions, webhooks, and customer management.
- Caching: Enhanced support for Redis and Memcached with TTL and cache invalidation.
- Task Queue: Background job processing using Celery with Redis as the broker.
Make sure you have Python installed on your system. You can check your Python version with:
python --version
Clone the repository to your local machine:
git clone https://github.com/yourusername/easyapi.git
cd easyapi
To create a basic web application using EasyAPI, follow these steps:
def home(request):
return Response("Welcome to EasyAPI! This is the home page.")
def about(request):
return Response("This is the About page.")
def greet(request):
name = request.body or 'Guest'
return Response(f"Hello, {name}!")
routes = [
('/', home, ['GET']),
('/about', about, ['GET']),
('/greet', greet, ['POST']),
]
app.add_routes(routes)
app.use_middleware(log_request)
app.use_middleware(add_custom_header)
app.register_error_handler(404, handle_404)
app.register_error_handler(500, handle_500)
app.set_static_folder('static')
if __name__ == '__main__':
app.run()
See a complete example usage including advanced features in the example.py or see an example of the base features here:
from EasyAPI.app import EasyAPI
from EasyAPI.response import Response
from EasyAPI.middleware import log_request, add_custom_header
from EasyAPI.error_handlers import handle_404, handle_500
app = EasyAPI()
def home(request):
return Response("Welcome to EasyAPI! This is the home page.")
def about(request):
return Response("This is the About page.")
def greet(request):
name = request.body or 'Guest'
return Response(f"Hello, {name}!")
routes = [
('/', home, ['GET']),
('/about', about, ['GET']),
('/greet', greet, ['POST']),
]
app.add_routes(routes)
app.use_middleware(log_request)
app.use_middleware(add_custom_header)
app.register_error_handler(404, handle_404)
app.register_error_handler(500, handle_500)
app.set_static_folder('static')
if __name__ == '__main__':
app.run()
Middleware functions allow you to add custom logic before and after request handling. Here’s an example:
def log_request(request):
print(f"Received {request.method} request for {request.path}")
def add_custom_header(request, response):
response.headers.append(('X-Custom-Header', 'This is a custom header'))
return response
Blueprints help you organize your application by grouping related routes together. Here’s an example of using a blueprint:
from EasyAPI.blueprint import Blueprint
api = Blueprint()
@api.route('/greet', methods=['POST'])
def greet(request):
name = request.body or 'Guest'
return Response(f"Hello, {name}!")
app.register_blueprint(api, url_prefix='/api')
You can define custom error handlers to provide more user-friendly error pages:
def handle_404(request):
return Response("Custom 404 Not Found", status='404 NOT FOUND')
def handle_500(request):
return Response("Custom 500 Internal Server Error", status='500 INTERNAL SERVER ERROR')
Create a New Project
easyapi new my_project
This command generates a new EasyAPI project with a basic directory structure, including folders for routes, middlewares, and static files.
Run the Server:
easyapi run
This command starts the EasyAPI development server. It automatically imports the run.py file to start the application.
List All Routes:
easyapi routes
This command lists all registered routes along with their corresponding handler functions and HTTP methods.
Generate a New Route:
easyapi generate-route my_route
This command scaffolds a new route handler in the routes directory. For example, it will create a my_route.py file in the routes directory with a pre-defined handler function.
Add a Middleware:
easyapi add-middleware my_middleware
This command scaffolds a new middleware function in the middlewares directory. It creates a file with a basic middleware template.
Set Static Folder:
easyapi set-static static
This command sets the static files directory for the application.
Contributions are welcome! If you have ideas, features, or bug fixes, feel free to submit a pull request or open an issue.
This project is licensed under the MIT License - see the LICENSE file for details.