BustAPI Python Web Framework

repository·main·Indexed 41 days ago

https://github.com/grandpaej/bustapi

A high-performance Python web framework powered by Rust and Actix-Web, designed for building fast REST APIs with a Flask-like syntax. Features include turbo routes for zero-overhead static paths, async/await support, dependency injection via Depends, and modular organization using Blueprints. Compatible with Python 3.10–3.14 on Linux, macOS, and Windows.

Tokens
46.8K
Snippets
198
Records
231
Agent score
51%

What's inside BustAPI

  1. Overview of BustAPI features

    main

    BustAPI is a high-performance Python web framework with the following core features:

    • Flask-like API: Uses familiar decorators such as @app.route(), @app.get(), and @app.post().
    • Rust Performance: Fast HTTP parsing powered by Actix-web.
    • Turbo Routes: Specialized @app.turbo_route() for high-throughput endpoints.
    • Multiprocessing: Support for parallel request handling on Linux using the workers parameter.
    • JWT Authentication: Built-in support for token-based authentication.
    • Templates: Integration with Jinja2 for template rendering.
    • Blueprints: Support for modular application organization.
  2. Security features for static files

    main

    The BustAPI static file handler is implemented in Rust and provides the following security protections:

    • Path Traversal Protection: Prevents attackers from accessing files outside the designated static folder (e.g., using ../../etc/passwd).
    • Hidden File Blocking: Automatically blocks requests for files starting with a dot . (e.g., .env, .git).
    WARNING

    Never place sensitive files (such as database credentials or source code) inside your static folder.

  3. BustAPI Platform Support and Performance

    main

    BustAPI supports multiple operating systems and architectures. While it works on Windows and macOS, Linux is recommended for production due to superior performance features.

    Requirements

    • Python: 3.10, 3.11, 3.12, 3.13, 3.14
    • Architecture: x86_64, arm64 (Apple Silicon)

    Platform Comparison

    OSBest Use CasePerformance Notes
    LinuxProductionBest performance with native multiprocessing, SO_REUSEPORT kernel load balancing, and 100,000+ RPS with 4 workers.
    macOSDevelopmentFully supported; ~35,000 RPS (single-process); Apple Silicon native.
    WindowsDevelopmentFully supported; ~17,000 RPS (single-process); x64 pre-built wheels.
  4. Best practices for Async vs Sync in BustAPI

    main

    BustAPI runs on an asynchronous event loop powered by Rust. To maintain high performance, follow these guidelines:

    Use Async for:

    • Database queries (using async drivers like motor or asyncpg).
    • Calling external APIs (using httpx or aiohttp).
    • File I/O.

    Avoid blocking calls in Async handlers:

    Do not use blocking calls like time.sleep() or standard requests.get() inside an async def route. Doing so will block the entire event loop and freeze your server.

  5. Performance benefits of using Cached routes

    main
    BustAPI provides a significant performance boost when using cached responses compared to standard (no-cache) routes. Benchmarks show that enabling caching can increase Requests Per Second (RPS) by over 100% and reduce average latency to near 0.00ms for common endpoints like /, /json, and parameterized routes like /user/10.
  6. Understand platform behavior for multiprocessing

    main

    BustAPI's multiprocessing capabilities depend heavily on the operating system:

    • Linux: Uses SO_REUSEPORT for true multiprocessing and kernel-level load balancing. This provides the highest performance (100,000+ RPS).
    • macOS: Falls back to a single-process mode using Actix-web's internal threading (~35,000 RPS).
    • Windows: Falls back to a single-process mode using Actix-web's internal threading (~17,000 RPS).

    For production environments requiring maximum scale, Linux is the recommended platform.

  7. What are Blueprints and how do they work

    main

    A Blueprint is a modular component that acts like a mini-application. It allows you to group related routes, templates, and static files into reusable modules to prevent a single application file from becoming unmanageable.

    Key characteristics:

    • Blueprints cannot run independently; they must be registered with a BustAPI application instance.
    • They support url_prefix to namespace all routes within the blueprint.
    • They are ideal for organizing code by domain (e.g., auth, admin, api).
    from bustapi import Blueprint
    
    # Create the blueprint with a namespace
    auth_bp = Blueprint("auth", url_prefix="/auth")
    
    @auth_bp.route("/login")
    def login():
        return "Login Page"
  8. Use template inheritance with Jinja2

    main

    BustAPI leverages Jinja2's inheritance model to allow you to define a base layout and extend it in child templates. This is achieved using {% extends "base.html" %} and {% block name %} tags.

    Example base.html:

    <!doctype html>
    <html>
    <head>
        <title>{% block title %}{% endblock %}</title>
    </head>
    <body>
        {% block content %}{% endblock %}
    </body>
    </html>

    Example index.html (extending base):

    {% extends "base.html" %}
    
    {% block title %}Home{% endblock %}
    
    {% block content %}
        <h1>Welcome to my site</h1>
    {% endblock %}
  9. Use Turbo Routes for maximum performance

    main

    For high-performance requirements, use the @app.turbo_route() decorator. These routes are parsed entirely in Rust to minimize overhead.

    Key Features:

    • Zero-overhead static routes: For fixed paths.
    • Typed dynamic parameters: Supports int, float, str, and path types.
    • Caching: Use the cache_ttl argument to cache responses for a specified number of seconds.

    ⚠️ CRITICAL LIMITATION: Turbo routes skip middleware, sessions, and request context. If your logic depends on these features, use the standard @app.route() instead.

    # Zero-overhead static route
    @app.turbo_route("/health")
    def health():
        return {"status": "ok"}
    
    # Dynamic route with typed params (parsed in Rust)
    @app.turbo_route("/users/<int:id")
    def get_user(id: int):
        return {"id": id, "name": f"User {id}"}
    
    # Cached response (140k+ RPS!)
    @app.turbo_route("/config", cache_ttl=60)
    def get_config():
        return {"version": "1.0", "env": "production"}
  10. Security considerations for BustAPI sessions

    main

    Signed Cookies

    Data is stored in cookies on the client side. While the data is base64 encoded and visible to the user, it is signed with your secret_key to prevent tampering.

    Warning: Do not store sensitive secrets (like plain-text passwords) directly in the session, as the data is visible to the client.

    Automatic Header Management

    BustAPI automatically tracks modifications to the session dictionary (including pop, clear, etc.). It only sends a new Set-Cookie header in the response when changes have actually occurred, optimizing network overhead.