Skip to content

Latest commit

 

History

History
187 lines (144 loc) · 5.26 KB

File metadata and controls

187 lines (144 loc) · 5.26 KB

Implementation Summary

This document summarizes the implementation of the new features requested.

Features Implemented

1. Cookie Support ✅

Implementation:

  • Added cookies field to PyRequest struct in Rust
  • Implemented cookie parsing from HTTP Cookie header
  • Added methods: get_cookie(), set_cookie(), cookies property
  • Added set_cookie() and delete_cookie() to PyResponse with full options (max_age, path, domain, secure, http_only, same_site)
  • Cookies are automatically parsed on each request and can be set in responses

Files Modified:

  • src/lib.rs - Added cookie parsing and methods

Example Usage:

@app.route("/login", methods=["POST"])
def login(request: Request) -> Response:
    resp = Response("Login successful")
    resp.set_cookie("session_id", "abc123", max_age=3600, http_only=True)
    return resp

@app.route("/profile", methods=["GET"])
def profile(request: Request) -> Response:
    session = request.get_cookie("session_id")
    return Response(f"Session: {session}")

2. Authentication Token Support ✅

Implementation:

  • Added auth_token property to PyRequest that extracts Bearer token from Authorization header
  • Property can be read and set: request.auth_token or request.auth_token = "token"
  • Automatically handles Bearer prefix

Files Modified:

  • src/lib.rs - Added auth_token property with getter/setter

Example Usage:

@app.route("/protected", methods=["GET"])
def protected(request: Request) -> Response:
    token = request.auth_token
    if not token:
        return Response("Unauthorized", status=401)
    return Response(f"Token: {token}")

@app.middleware
def auth_middleware(request: Request):
    if request.path.startswith("/internal"):
        request.auth_token = "internal-token"
    return request

3. Middleware Header Modification ✅

Implementation:

  • Middlewares already supported returning modified Request objects
  • Headers set via request.set_header() in middleware are propagated to handlers
  • Same applies to cookies and auth tokens

Files Modified:

  • No code changes needed - functionality already existed
  • Added test suite to verify

Example Usage:

@app.middleware
def header_middleware(request: Request):
    request.set_header("X-Request-ID", "req-123")
    return request

@app.route("/headers", methods=["GET"])
def show_headers(request: Request) -> Response:
    req_id = request.get_header("X-Request-ID")
    return Response(f"Request ID: {req_id}")

4. Static File Serving ✅

Implementation:

  • Added @app.static(url_path, directory) decorator in Python layer
  • Automatically registers route with wildcard pattern
  • Includes security: directory traversal protection
  • Automatic content-type detection using mimetypes module
  • Returns 404 for non-existent files, 403 for security violations

Files Modified:

  • python/rupy/__init__.py - Added _static_decorator() function

Example Usage:

@app.static("/static", "./public")
def static_files():
    pass

# Files in ./public are now accessible at /static/<filename>

5. Reverse Proxy ✅

Implementation:

  • Added @app.proxy(url_path, target_url) decorator in Python layer
  • Uses urllib.request to forward requests to backend
  • Forwards all HTTP methods: GET, POST, PUT, PATCH, DELETE
  • Preserves request headers and body
  • Returns backend response with headers

Files Modified:

  • python/rupy/__init__.py - Added _proxy_decorator() function

Example Usage:

@app.proxy("/api", "http://backend:8080")
def api_proxy():
    pass

# Requests to /api/* are forwarded to http://backend:8080/*

6. OpenAPI/Swagger Support ✅

Implementation:

  • Added enable_openapi() and disable_openapi() methods
  • Generates OpenAPI 3.0 specification in JSON format
  • Configurable: path, title, version, description
  • Automatically registers endpoint that returns the spec

Files Modified:

  • python/rupy/__init__.py - Added OpenAPI methods

Example Usage:

app = Rupy()

app.enable_openapi(
    path="/openapi.json",
    title="My API",
    version="1.0.0",
    description="API documentation"
)

# OpenAPI spec available at /openapi.json

Test Coverage

All features have comprehensive test coverage:

  1. tests/test_cookies_auth.py - Cookie and auth token tests
  2. tests/test_middleware_headers.py - Middleware header modification tests
  3. tests/test_new_features.py - Integration tests for all features

Examples

Created 4 new example files:

  1. examples/cookies_auth_example.py - Cookie and authentication
  2. examples/static_files_example.py - Static file serving
  3. examples/reverse_proxy_example.py - Reverse proxy
  4. examples/openapi_example.py - OpenAPI endpoint

Documentation

Updated README.md with:

  • Feature list updated with new capabilities
  • New sections for:
    • Cookies and Authentication
    • Static File Serving
    • Reverse Proxy
    • OpenAPI/Swagger Support
  • Code examples for each feature
  • Links to example files

Summary

All requested features have been successfully implemented with:

  • ✅ Full functionality as specified
  • ✅ Comprehensive test coverage
  • ✅ Working examples
  • ✅ Complete documentation
  • ✅ Minimal code changes (surgical modifications)
  • ✅ Backward compatibility maintained