This document summarizes the implementation of the new features requested.
Implementation:
- Added
cookiesfield toPyRequeststruct in Rust - Implemented cookie parsing from HTTP
Cookieheader - Added methods:
get_cookie(),set_cookie(),cookiesproperty - Added
set_cookie()anddelete_cookie()toPyResponsewith 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}")Implementation:
- Added
auth_tokenproperty toPyRequestthat extracts Bearer token fromAuthorizationheader - Property can be read and set:
request.auth_tokenorrequest.auth_token = "token" - Automatically handles
Bearerprefix
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 requestImplementation:
- Middlewares already supported returning modified
Requestobjects - 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}")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>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/*Implementation:
- Added
enable_openapi()anddisable_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.jsonAll features have comprehensive test coverage:
tests/test_cookies_auth.py- Cookie and auth token teststests/test_middleware_headers.py- Middleware header modification teststests/test_new_features.py- Integration tests for all features
Created 4 new example files:
examples/cookies_auth_example.py- Cookie and authenticationexamples/static_files_example.py- Static file servingexamples/reverse_proxy_example.py- Reverse proxyexamples/openapi_example.py- OpenAPI endpoint
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
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