routly 0.1.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- routly/__init__.py +5 -0
- routly/conf/__init__.py +3 -0
- routly/conf/settings.py +49 -0
- routly/core/__init__.py +3 -0
- routly/core/app.py +86 -0
- routly/exceptions/__init__.py +24 -0
- routly/exceptions/exceptions.py +86 -0
- routly/exceptions/handler.py +80 -0
- routly/exceptions/templates/debug.html +250 -0
- routly/http/__init__.py +4 -0
- routly/http/request.py +13 -0
- routly/http/response.py +58 -0
- routly/middleware/__init__.py +19 -0
- routly/middleware/base.py +14 -0
- routly/middleware/cors.py +98 -0
- routly/middleware/csrf.py +99 -0
- routly/middleware/gzip.py +47 -0
- routly/middleware/manager.py +48 -0
- routly/middleware/security.py +94 -0
- routly/middleware/session.py +100 -0
- routly/middleware/timing.py +21 -0
- routly/routing/__init__.py +4 -0
- routly/routing/route.py +26 -0
- routly/routing/router.py +75 -0
- routly/templating/__init__.py +3 -0
- routly/templating/engine.py +34 -0
- routly/templating/helpers.py +38 -0
- routly-0.1.0.dist-info/METADATA +100 -0
- routly-0.1.0.dist-info/RECORD +31 -0
- routly-0.1.0.dist-info/WHEEL +5 -0
- routly-0.1.0.dist-info/top_level.txt +1 -0
routly/__init__.py
ADDED
routly/conf/__init__.py
ADDED
routly/conf/settings.py
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import importlib
|
|
2
|
+
import os
|
|
3
|
+
from pathlib import Path
|
|
4
|
+
from typing import Any, List, Optional, Union
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
class Settings:
|
|
8
|
+
"""Manages application-wide configuration settings with environment-based module discovery."""
|
|
9
|
+
|
|
10
|
+
def __init__(self) -> None:
|
|
11
|
+
"""Initializes default settings and loads user-defined overrides."""
|
|
12
|
+
self.DEBUG: bool = True
|
|
13
|
+
self.SECRET_KEY: str = "routly-secret-key-default"
|
|
14
|
+
self.TEMPLATES: List[dict[str, Any]] = []
|
|
15
|
+
self.MIDDLEWARE: List[str] = []
|
|
16
|
+
self.ALLOWED_HOSTS: List[str] = ["*"]
|
|
17
|
+
self.STATIC_URL: str = "/static/"
|
|
18
|
+
self.STATIC_ROOT: Optional[Union[str, Path]] = None
|
|
19
|
+
|
|
20
|
+
self.SESSION_COOKIE_NAME: str = "session"
|
|
21
|
+
self.SESSION_COOKIE_MAX_AGE: int = 1209600
|
|
22
|
+
self.SESSION_COOKIE_PATH: str = "/"
|
|
23
|
+
self.SESSION_COOKIE_HTTPONLY: bool = True
|
|
24
|
+
self.SESSION_COOKIE_SAMESITE: str = "Lax"
|
|
25
|
+
self.SESSION_COOKIE_SECURE: bool = False
|
|
26
|
+
|
|
27
|
+
self.CSRF_COOKIE_NAME: str = "csrftoken"
|
|
28
|
+
self.CSRF_COOKIE_PATH: str = "/"
|
|
29
|
+
self.CSRF_COOKIE_HTTPONLY: bool = False
|
|
30
|
+
self.CSRF_COOKIE_SAMESITE: str = "Lax"
|
|
31
|
+
self.CSRF_COOKIE_SECURE: bool = False
|
|
32
|
+
|
|
33
|
+
self._setup()
|
|
34
|
+
|
|
35
|
+
def _setup(self) -> None:
|
|
36
|
+
"""Discovers and imports the user settings module specified by ROUTLY_SETTINGS_MODULE."""
|
|
37
|
+
settings_module: str = os.environ.get("ROUTLY_SETTINGS_MODULE", "settings")
|
|
38
|
+
|
|
39
|
+
try:
|
|
40
|
+
user_settings = importlib.import_module(settings_module)
|
|
41
|
+
|
|
42
|
+
for key in dir(user_settings):
|
|
43
|
+
if key.isupper():
|
|
44
|
+
setattr(self, key, getattr(user_settings, key))
|
|
45
|
+
except ImportError:
|
|
46
|
+
pass
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
settings: Settings = Settings()
|
routly/core/__init__.py
ADDED
routly/core/app.py
ADDED
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
from typing import Any, Callable, Dict, Iterable, Optional, Type, Union
|
|
2
|
+
from whitenoise import WhiteNoise
|
|
3
|
+
|
|
4
|
+
from routly.conf import settings
|
|
5
|
+
from routly.exceptions import (
|
|
6
|
+
HttpException,
|
|
7
|
+
InternalServerError,
|
|
8
|
+
MethodNotAllowed,
|
|
9
|
+
NotFound,
|
|
10
|
+
render_exception,
|
|
11
|
+
)
|
|
12
|
+
from routly.http import Request, Response, build_response, current_request
|
|
13
|
+
from routly.middleware import BaseMiddleware, MiddlewareManager
|
|
14
|
+
from routly.routing import Router
|
|
15
|
+
from routly.templating import TemplateEngine
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
class RoutlyApp(Router):
|
|
19
|
+
"""Central WSGI application class for Routly microframework."""
|
|
20
|
+
|
|
21
|
+
def __init__(self) -> None:
|
|
22
|
+
"""Initializes the template engine, middleware pipeline, and WhiteNoise static serving."""
|
|
23
|
+
super().__init__()
|
|
24
|
+
self.templates: TemplateEngine = TemplateEngine()
|
|
25
|
+
self.middleware: MiddlewareManager = MiddlewareManager()
|
|
26
|
+
self.wsgi_app: Callable[..., Iterable[bytes]] = self._dispatch
|
|
27
|
+
|
|
28
|
+
static_root = getattr(settings, "STATIC_ROOT", None)
|
|
29
|
+
if static_root:
|
|
30
|
+
self.wsgi_app = WhiteNoise(
|
|
31
|
+
self.wsgi_app,
|
|
32
|
+
root=str(static_root),
|
|
33
|
+
prefix=getattr(settings, "STATIC_URL", "/static/"),
|
|
34
|
+
)
|
|
35
|
+
|
|
36
|
+
def __call__(
|
|
37
|
+
self, environ: Dict[str, Any], start_response: Callable[..., Any]
|
|
38
|
+
) -> Iterable[bytes]:
|
|
39
|
+
"""WSGI interface entry point, delegating to the wrapped WSGI pipeline."""
|
|
40
|
+
return self.wsgi_app(environ, start_response)
|
|
41
|
+
|
|
42
|
+
def _dispatch(
|
|
43
|
+
self, environ: Dict[str, Any], start_response: Callable[..., Any]
|
|
44
|
+
) -> Iterable[bytes]:
|
|
45
|
+
"""Dispatches an incoming WSGI request, executes handle_request, and starts response."""
|
|
46
|
+
request = Request(environ)
|
|
47
|
+
response = self.handle_request(request)
|
|
48
|
+
return response(environ, start_response)
|
|
49
|
+
|
|
50
|
+
def handle_request(self, request: Request) -> Response:
|
|
51
|
+
"""Processes an incoming HTTP request through middleware, route resolution, and handlers."""
|
|
52
|
+
current_request.set(request)
|
|
53
|
+
|
|
54
|
+
try:
|
|
55
|
+
response = self.middleware.process_request(request)
|
|
56
|
+
if response is not None:
|
|
57
|
+
return self.middleware.process_response(request, build_response(response))
|
|
58
|
+
|
|
59
|
+
handler, kwargs, error = self.resolve(request.path, request.method)
|
|
60
|
+
|
|
61
|
+
if error == 405:
|
|
62
|
+
raise MethodNotAllowed()
|
|
63
|
+
|
|
64
|
+
if error == 404 or handler is None:
|
|
65
|
+
raise NotFound()
|
|
66
|
+
|
|
67
|
+
response = build_response(handler(request, **kwargs))
|
|
68
|
+
except HttpException as exc:
|
|
69
|
+
response = render_exception(request, exc)
|
|
70
|
+
|
|
71
|
+
except Exception as exc:
|
|
72
|
+
response = render_exception(request, InternalServerError(exc=exc))
|
|
73
|
+
|
|
74
|
+
return self.middleware.process_response(request, response)
|
|
75
|
+
|
|
76
|
+
def template(
|
|
77
|
+
self, template_name: str, context: Optional[Dict[str, Any]] = None
|
|
78
|
+
) -> str:
|
|
79
|
+
"""Renders a Jinja2 template with context data."""
|
|
80
|
+
return self.templates.render(template_name, context)
|
|
81
|
+
|
|
82
|
+
def add_middleware(
|
|
83
|
+
self, middleware: Union[Type[BaseMiddleware], BaseMiddleware, str]
|
|
84
|
+
) -> None:
|
|
85
|
+
"""Registers a middleware class, instance, or dotted import string."""
|
|
86
|
+
self.middleware.add(middleware)
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
from routly.exceptions.exceptions import (
|
|
2
|
+
HttpException,
|
|
3
|
+
BadRequest,
|
|
4
|
+
Unauthorized,
|
|
5
|
+
Forbidden,
|
|
6
|
+
NotFound,
|
|
7
|
+
MethodNotAllowed,
|
|
8
|
+
InternalServerError,
|
|
9
|
+
abort,
|
|
10
|
+
)
|
|
11
|
+
from routly.exceptions.handler import render_exception, render_debug_page
|
|
12
|
+
|
|
13
|
+
__all__ = [
|
|
14
|
+
"HttpException",
|
|
15
|
+
"BadRequest",
|
|
16
|
+
"Unauthorized",
|
|
17
|
+
"Forbidden",
|
|
18
|
+
"NotFound",
|
|
19
|
+
"MethodNotAllowed",
|
|
20
|
+
"InternalServerError",
|
|
21
|
+
"abort",
|
|
22
|
+
"render_exception",
|
|
23
|
+
"render_debug_page",
|
|
24
|
+
]
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
from typing import Dict, NoReturn, Optional, Type
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
class HttpException(Exception):
|
|
5
|
+
"""Base class for all Routly HTTP exceptions."""
|
|
6
|
+
|
|
7
|
+
status_code: int = 500
|
|
8
|
+
default_message: str = "Internal Server Error"
|
|
9
|
+
default_title: str = "Server Error"
|
|
10
|
+
|
|
11
|
+
def __init__(
|
|
12
|
+
self,
|
|
13
|
+
message: Optional[str] = None,
|
|
14
|
+
title: Optional[str] = None,
|
|
15
|
+
exc: Optional[Exception] = None,
|
|
16
|
+
) -> None:
|
|
17
|
+
"""Initializes the HTTP exception with optional custom message, title, and underlying cause."""
|
|
18
|
+
self.message: str = message or self.default_message
|
|
19
|
+
self.title: str = title or self.default_title
|
|
20
|
+
self.exc: Optional[Exception] = exc
|
|
21
|
+
|
|
22
|
+
super().__init__(self.message)
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
class BadRequest(HttpException):
|
|
26
|
+
"""400 Bad Request exception."""
|
|
27
|
+
|
|
28
|
+
status_code: int = 400
|
|
29
|
+
default_message: str = "Bad Request"
|
|
30
|
+
default_title: str = "Bad Request"
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
class Unauthorized(HttpException):
|
|
34
|
+
"""401 Unauthorized exception."""
|
|
35
|
+
|
|
36
|
+
status_code: int = 401
|
|
37
|
+
default_message: str = "Unauthorized"
|
|
38
|
+
default_title: str = "Authentication Required"
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
class Forbidden(HttpException):
|
|
42
|
+
"""403 Forbidden exception."""
|
|
43
|
+
|
|
44
|
+
status_code: int = 403
|
|
45
|
+
default_message: str = "Forbidden"
|
|
46
|
+
default_title: str = "Access Forbidden"
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
class NotFound(HttpException):
|
|
50
|
+
"""404 Not Found exception."""
|
|
51
|
+
|
|
52
|
+
status_code: int = 404
|
|
53
|
+
default_message: str = "Not Found"
|
|
54
|
+
default_title: str = "Page Not Found"
|
|
55
|
+
|
|
56
|
+
|
|
57
|
+
class MethodNotAllowed(HttpException):
|
|
58
|
+
"""405 Method Not Allowed exception."""
|
|
59
|
+
|
|
60
|
+
status_code: int = 405
|
|
61
|
+
default_message: str = "Method Not Allowed"
|
|
62
|
+
default_title: str = "HTTP Method Not Allowed"
|
|
63
|
+
|
|
64
|
+
|
|
65
|
+
class InternalServerError(HttpException):
|
|
66
|
+
"""500 Internal Server Error exception."""
|
|
67
|
+
|
|
68
|
+
status_code: int = 500
|
|
69
|
+
default_message: str = "Internal Server Error"
|
|
70
|
+
default_title: str = "Internal Server Error"
|
|
71
|
+
|
|
72
|
+
|
|
73
|
+
STATUS_MAP: Dict[int, Type[HttpException]] = {
|
|
74
|
+
400: BadRequest,
|
|
75
|
+
401: Unauthorized,
|
|
76
|
+
403: Forbidden,
|
|
77
|
+
404: NotFound,
|
|
78
|
+
405: MethodNotAllowed,
|
|
79
|
+
500: InternalServerError,
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
|
|
83
|
+
def abort(code: int, message: Optional[str] = None) -> NoReturn:
|
|
84
|
+
"""Raises an appropriate HttpException corresponding to the given HTTP status code."""
|
|
85
|
+
exc_class = STATUS_MAP.get(code, HttpException)
|
|
86
|
+
raise exc_class(message)
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
import traceback
|
|
2
|
+
from pathlib import Path
|
|
3
|
+
from typing import Any, Optional
|
|
4
|
+
from jinja2 import Template
|
|
5
|
+
|
|
6
|
+
from routly.conf import settings
|
|
7
|
+
from routly.exceptions.exceptions import HttpException, InternalServerError
|
|
8
|
+
from routly.http import Response
|
|
9
|
+
|
|
10
|
+
TEMPLATE_FILE: Path = Path(__file__).resolve().parent / "templates" / "debug.html"
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
def render_debug_page(
|
|
14
|
+
request: Any,
|
|
15
|
+
exc: Optional[Exception] = None,
|
|
16
|
+
status_code: int = 500,
|
|
17
|
+
status_text: str = "Server Error",
|
|
18
|
+
error_title: str = "Unhandled Exception",
|
|
19
|
+
) -> Response:
|
|
20
|
+
"""Renders the Tailwind CSS dark-mode interactive debugger page with traceback details."""
|
|
21
|
+
template_content = TEMPLATE_FILE.read_text(encoding="utf-8")
|
|
22
|
+
template = Template(template_content)
|
|
23
|
+
|
|
24
|
+
if exc is not None:
|
|
25
|
+
exc_type = type(exc).__name__
|
|
26
|
+
exc_value = str(exc)
|
|
27
|
+
tb_text = traceback.format_exc()
|
|
28
|
+
else:
|
|
29
|
+
exc_type = f"HTTP {status_code}"
|
|
30
|
+
exc_value = status_text
|
|
31
|
+
tb_text = None
|
|
32
|
+
|
|
33
|
+
html = template.render(
|
|
34
|
+
exc_type=exc_type,
|
|
35
|
+
exc_value=exc_value,
|
|
36
|
+
tb_text=tb_text,
|
|
37
|
+
request=request,
|
|
38
|
+
status_code=status_code,
|
|
39
|
+
status_text=status_text,
|
|
40
|
+
error_title=error_title,
|
|
41
|
+
)
|
|
42
|
+
|
|
43
|
+
response = Response()
|
|
44
|
+
response.status_code = status_code
|
|
45
|
+
response.content_type = "text/html"
|
|
46
|
+
response.text = html
|
|
47
|
+
return response
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
def render_exception(request: Any, exc: Exception) -> Response:
|
|
51
|
+
"""Renders an HTTP response for an exception, handling content negotiation and debug mode."""
|
|
52
|
+
if not isinstance(exc, HttpException):
|
|
53
|
+
http_exc = InternalServerError(message=str(exc), exc=exc)
|
|
54
|
+
else:
|
|
55
|
+
http_exc = exc
|
|
56
|
+
|
|
57
|
+
code: int = http_exc.status_code
|
|
58
|
+
message: str = http_exc.message
|
|
59
|
+
title: str = http_exc.title
|
|
60
|
+
underlying_exc: Exception = http_exc.exc or http_exc
|
|
61
|
+
|
|
62
|
+
is_json = "application/json" in request.headers.get(
|
|
63
|
+
"Accept", ""
|
|
64
|
+
) or "application/json" in (request.content_type or "")
|
|
65
|
+
if is_json:
|
|
66
|
+
res = Response(status_code=code)
|
|
67
|
+
res.json = {"error": title, "message": message, "status_code": code}
|
|
68
|
+
res.content_type = "application/json"
|
|
69
|
+
return res
|
|
70
|
+
|
|
71
|
+
if getattr(settings, "DEBUG", False):
|
|
72
|
+
return render_debug_page(
|
|
73
|
+
request,
|
|
74
|
+
exc=underlying_exc,
|
|
75
|
+
status_code=code,
|
|
76
|
+
status_text=message,
|
|
77
|
+
error_title=title,
|
|
78
|
+
)
|
|
79
|
+
|
|
80
|
+
return Response(message, status_code=code)
|
|
@@ -0,0 +1,250 @@
|
|
|
1
|
+
<!DOCTYPE html>
|
|
2
|
+
<html lang="en" class="dark">
|
|
3
|
+
<head>
|
|
4
|
+
<meta charset="UTF-8">
|
|
5
|
+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
6
|
+
<title>{{ exc_type }}: {{ exc_value }} — Routly Debugger</title>
|
|
7
|
+
<link rel="icon" type="image/svg+xml" href="data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 32 32' width='32' height='32' fill='none'%3E%3Cdefs%3E%3ClinearGradient id='g' x1='0%25' y1='0%25' x2='100%25' y2='100%25'%3E%3Cstop offset='0%25' stop-color='%2338bdf8'/%3E%3Cstop offset='100%25' stop-color='%23a855f7'/%3E%3C/linearGradient%3E%3C/defs%3E%3Crect width='32' height='32' rx='8' fill='%23090d16'/%3E%3Cline x1='9' y1='7' x2='9' y2='25' stroke='url(%23g)' stroke-width='3.5' stroke-linecap='round'/%3E%3Cpath d='M 9 8.5 C 20 8.5 24 11 24 15 C 24 19 19 20.5 12 20.5' stroke='url(%23g)' stroke-width='3.5' stroke-linecap='round' fill='none'/%3E%3Cpath d='M 15 19 L 24 26' stroke='url(%23g)' stroke-width='3.5' stroke-linecap='round'/%3E%3C/svg%3E">
|
|
8
|
+
<!-- Tailwind CSS CDN -->
|
|
9
|
+
<script src="https://cdn.tailwindcss.com"></script>
|
|
10
|
+
<script>
|
|
11
|
+
tailwind.config = {
|
|
12
|
+
darkMode: 'class',
|
|
13
|
+
theme: {
|
|
14
|
+
extend: {
|
|
15
|
+
colors: {
|
|
16
|
+
dark: {
|
|
17
|
+
900: '#0b0f17',
|
|
18
|
+
800: '#111827',
|
|
19
|
+
700: '#1f2937',
|
|
20
|
+
600: '#374151'
|
|
21
|
+
},
|
|
22
|
+
routly: {
|
|
23
|
+
500: '#6366f1',
|
|
24
|
+
600: '#4f46e5'
|
|
25
|
+
}
|
|
26
|
+
},
|
|
27
|
+
fontFamily: {
|
|
28
|
+
mono: ['JetBrains Mono', 'Fira Code', 'SFMono-Regular', 'Menlo', 'Monaco', 'Consolas', 'monospace']
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
</script>
|
|
34
|
+
<style>
|
|
35
|
+
@import url('https://fonts.googleapis.com/css2?family=Plus+Jakarta+Sans:wght@400;500;600;700;800&family=JetBrains+Mono:wght@400;500;600&display=swap');
|
|
36
|
+
body { font-family: 'Plus Jakarta Sans', -apple-system, BlinkMacSystemFont, sans-serif; }
|
|
37
|
+
.font-code { font-family: 'JetBrains Mono', monospace; }
|
|
38
|
+
::-webkit-scrollbar { width: 8px; height: 8px; }
|
|
39
|
+
::-webkit-scrollbar-track { background: #0b0f17; }
|
|
40
|
+
::-webkit-scrollbar-thumb { background: #1f2937; border-radius: 4px; }
|
|
41
|
+
::-webkit-scrollbar-thumb:hover { background: #374151; }
|
|
42
|
+
</style>
|
|
43
|
+
</head>
|
|
44
|
+
<body class="bg-dark-900 text-slate-100 min-h-screen antialiased selection:bg-rose-500/30 selection:text-rose-200">
|
|
45
|
+
|
|
46
|
+
<!-- Top Navigation Bar -->
|
|
47
|
+
<header class="border-b border-slate-800/80 bg-dark-800/60 backdrop-blur-xl sticky top-0 z-50">
|
|
48
|
+
<div class="max-w-7xl mx-auto px-4 sm:px-6 lg:px-8 h-16 flex items-center justify-between">
|
|
49
|
+
<div class="flex items-center gap-3">
|
|
50
|
+
<svg class="w-9 h-9 rounded-xl shadow-lg shadow-indigo-500/25 ring-1 ring-white/20" viewBox="0 0 100 100" fill="none">
|
|
51
|
+
<defs>
|
|
52
|
+
<linearGradient id="debug-logo-grad" x1="0%" y1="0%" x2="100%" y2="100%">
|
|
53
|
+
<stop offset="0%" stop-color="#38bdf8"/>
|
|
54
|
+
<stop offset="50%" stop-color="#6366f1"/>
|
|
55
|
+
<stop offset="100%" stop-color="#a855f7"/>
|
|
56
|
+
</linearGradient>
|
|
57
|
+
</defs>
|
|
58
|
+
<rect width="100" height="100" rx="24" fill="#090d16"/>
|
|
59
|
+
<rect x="1.5" y="1.5" width="97" height="97" rx="22.5" stroke="url(#debug-logo-grad)" stroke-width="2" stroke-opacity="0.4" fill="none"/>
|
|
60
|
+
<line x1="28" y1="22" x2="28" y2="78" stroke="url(#debug-logo-grad)" stroke-width="10" stroke-linecap="round"/>
|
|
61
|
+
<path d="M 28 26 C 64 26 76 34 76 46 C 76 58 62 62 38 62" stroke="url(#debug-logo-grad)" stroke-width="10" stroke-linecap="round" stroke-linejoin="round" fill="none"/>
|
|
62
|
+
<path d="M 48 58 L 72 79" stroke="url(#debug-logo-grad)" stroke-width="10" stroke-linecap="round"/>
|
|
63
|
+
<polygon points="76,67 83,81 68,82" fill="#a855f7"/>
|
|
64
|
+
<circle cx="46" cy="44" r="4.5" fill="#38bdf8"/>
|
|
65
|
+
</svg>
|
|
66
|
+
<span class="font-extrabold text-xl tracking-tight bg-gradient-to-r from-sky-400 via-indigo-400 to-purple-400 bg-clip-text text-transparent">Routly</span>
|
|
67
|
+
</div>
|
|
68
|
+
|
|
69
|
+
<div class="flex items-center gap-3">
|
|
70
|
+
<span class="inline-flex items-center gap-1.5 px-3 py-1 rounded-full text-xs font-semibold bg-rose-500/10 text-rose-400 border border-rose-500/20 shadow-sm">
|
|
71
|
+
<span class="w-2 h-2 rounded-full bg-rose-500 animate-pulse"></span>
|
|
72
|
+
{{ status_code or 500 }} {{ status_text or "Server Error" }}
|
|
73
|
+
</span>
|
|
74
|
+
<span class="inline-flex items-center px-3 py-1 rounded-full text-xs font-semibold bg-amber-500/10 text-amber-300 border border-amber-500/20">
|
|
75
|
+
DEBUG MODE
|
|
76
|
+
</span>
|
|
77
|
+
</div>
|
|
78
|
+
</div>
|
|
79
|
+
</header>
|
|
80
|
+
|
|
81
|
+
<!-- Main Content Area -->
|
|
82
|
+
<main class="max-w-7xl mx-auto px-4 sm:px-6 lg:px-8 py-8 space-y-6">
|
|
83
|
+
|
|
84
|
+
<!-- Error Hero Banner -->
|
|
85
|
+
<div class="relative overflow-hidden rounded-2xl border border-rose-500/20 bg-gradient-to-b from-rose-950/40 via-dark-800/80 to-dark-800/40 p-6 sm:p-8 backdrop-blur-xl shadow-2xl">
|
|
86
|
+
<div class="absolute -top-24 -right-24 w-72 h-72 bg-rose-500/10 rounded-full blur-3xl pointer-events-none"></div>
|
|
87
|
+
|
|
88
|
+
<div class="flex flex-col md:flex-row md:items-start md:justify-between gap-6 relative z-10">
|
|
89
|
+
<div class="space-y-3 flex-1">
|
|
90
|
+
<div class="inline-flex items-center gap-2 px-3 py-1 rounded-lg bg-rose-500/15 text-rose-300 border border-rose-500/30 text-xs font-bold uppercase tracking-wider">
|
|
91
|
+
<svg class="w-4 h-4 text-rose-400" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2">
|
|
92
|
+
<path stroke-linecap="round" stroke-linejoin="round" d="M12 9v2m0 4h.01m-6.938 4h13.856c1.54 0 2.502-1.667 1.732-3L13.732 4c-.77-1.333-2.694-1.333-3.464 0L3.34 16c-.77 1.333.192 3 1.732 3z" />
|
|
93
|
+
</svg>
|
|
94
|
+
{{ error_title or "Unhandled Exception" }}
|
|
95
|
+
</div>
|
|
96
|
+
<h1 class="text-3xl sm:text-4xl font-extrabold text-white tracking-tight break-all font-code">
|
|
97
|
+
{{ exc_type }}
|
|
98
|
+
</h1>
|
|
99
|
+
<p class="text-lg sm:text-xl text-rose-200/90 font-medium break-all">
|
|
100
|
+
{{ exc_value or "No additional exception message provided." }}
|
|
101
|
+
</p>
|
|
102
|
+
</div>
|
|
103
|
+
|
|
104
|
+
{% if tb_text %}
|
|
105
|
+
<!-- Copy Traceback Button -->
|
|
106
|
+
<div class="shrink-0 flex items-center gap-3">
|
|
107
|
+
<button id="copyBtn" onclick="copyTraceback()" class="inline-flex items-center gap-2 px-4 py-2.5 rounded-xl bg-slate-800 hover:bg-slate-700 text-slate-200 text-xs font-semibold border border-slate-700 hover:border-slate-600 transition-all shadow-sm active:scale-95">
|
|
108
|
+
<svg id="copyIcon" class="w-4 h-4 text-slate-400" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2">
|
|
109
|
+
<path stroke-linecap="round" stroke-linejoin="round" d="M8 16H6a2 2 0 01-2-2V6a2 2 0 012-2h8a2 2 0 012 2v2m-6 12h8a2 2 0 002-2v-8a2 2 0 00-2-2h-8a2 2 0 00-2 2v8a2 2 0 002 2z" />
|
|
110
|
+
</svg>
|
|
111
|
+
<span id="copyText">Copy Traceback</span>
|
|
112
|
+
</button>
|
|
113
|
+
</div>
|
|
114
|
+
{% endif %}
|
|
115
|
+
</div>
|
|
116
|
+
</div>
|
|
117
|
+
|
|
118
|
+
{% if tb_text %}
|
|
119
|
+
<!-- Stack Trace Card -->
|
|
120
|
+
<div class="rounded-2xl border border-slate-800/80 bg-dark-800/60 overflow-hidden shadow-xl backdrop-blur-xl">
|
|
121
|
+
<div class="px-6 py-4 bg-dark-800/90 border-b border-slate-800/80 flex items-center justify-between">
|
|
122
|
+
<div class="flex items-center gap-3">
|
|
123
|
+
<div class="flex items-center gap-1.5">
|
|
124
|
+
<span class="w-3 h-3 rounded-full bg-rose-500/80 inline-block"></span>
|
|
125
|
+
<span class="w-3 h-3 rounded-full bg-amber-500/80 inline-block"></span>
|
|
126
|
+
<span class="w-3 h-3 rounded-full bg-emerald-500/80 inline-block"></span>
|
|
127
|
+
</div>
|
|
128
|
+
<span class="text-xs font-bold text-slate-400 uppercase tracking-wider ml-2 flex items-center gap-1.5">
|
|
129
|
+
<svg class="w-4 h-4 text-indigo-400" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2">
|
|
130
|
+
<path stroke-linecap="round" stroke-linejoin="round" d="M10 20l4-16m4 4l4 4-4 4M6 16l-4-4 4-4" />
|
|
131
|
+
</svg>
|
|
132
|
+
Python Call Stack Traceback
|
|
133
|
+
</span>
|
|
134
|
+
</div>
|
|
135
|
+
<span class="text-xs text-slate-500 font-mono">Most recent call last</span>
|
|
136
|
+
</div>
|
|
137
|
+
<div class="p-4 sm:p-6 bg-dark-900/80 overflow-x-auto">
|
|
138
|
+
<pre class="font-code text-sm text-rose-300/95 leading-relaxed whitespace-pre-wrap break-all">{{ tb_text }}</pre>
|
|
139
|
+
</div>
|
|
140
|
+
</div>
|
|
141
|
+
{% endif %}
|
|
142
|
+
|
|
143
|
+
<!-- Two Column Grid for Request & Environment -->
|
|
144
|
+
<div class="grid grid-cols-1 lg:grid-cols-2 gap-6">
|
|
145
|
+
|
|
146
|
+
<!-- Request Details Card -->
|
|
147
|
+
<div class="rounded-2xl border border-slate-800/80 bg-dark-800/60 overflow-hidden shadow-xl backdrop-blur-xl">
|
|
148
|
+
<div class="px-6 py-4 border-b border-slate-800/80 flex items-center gap-2">
|
|
149
|
+
<svg class="w-5 h-5 text-sky-400" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2">
|
|
150
|
+
<path stroke-linecap="round" stroke-linejoin="round" d="M21 12a9 9 0 01-9 9m9-9a9 9 0 00-9-9m9 9H3m9 9a9 9 0 01-9-9m9 9c1.657 0 3-4.03 3-9s-1.343-9-3-9m0 18c-1.657 0-3-4.03-3-9s1.343-9 3-9m-9 9a9 9 0 019-9" />
|
|
151
|
+
</svg>
|
|
152
|
+
<h2 class="text-sm font-bold text-slate-200 uppercase tracking-wider">HTTP Request Info</h2>
|
|
153
|
+
</div>
|
|
154
|
+
<div class="divide-y divide-slate-800/60">
|
|
155
|
+
<div class="px-6 py-3.5 flex items-center justify-between gap-4 text-sm">
|
|
156
|
+
<span class="text-slate-400 font-medium">Path</span>
|
|
157
|
+
<span class="font-code text-slate-200 font-semibold break-all">{{ request.path }}</span>
|
|
158
|
+
</div>
|
|
159
|
+
<div class="px-6 py-3.5 flex items-center justify-between gap-4 text-sm">
|
|
160
|
+
<span class="text-slate-400 font-medium">HTTP Method</span>
|
|
161
|
+
<span class="inline-flex items-center px-2.5 py-0.5 rounded-md text-xs font-bold font-code uppercase tracking-wider
|
|
162
|
+
{% if request.method == 'GET' %} bg-emerald-500/10 text-emerald-400 border border-emerald-500/20
|
|
163
|
+
{% elif request.method == 'POST' %} bg-blue-500/10 text-blue-400 border border-blue-500/20
|
|
164
|
+
{% elif request.method == 'PUT' %} bg-amber-500/10 text-amber-400 border border-amber-500/20
|
|
165
|
+
{% elif request.method == 'DELETE' %} bg-rose-500/10 text-rose-400 border border-rose-500/20
|
|
166
|
+
{% else %} bg-purple-500/10 text-purple-400 border border-purple-500/20 {% endif %}">
|
|
167
|
+
{{ request.method }}
|
|
168
|
+
</span>
|
|
169
|
+
</div>
|
|
170
|
+
<div class="px-6 py-3.5 flex items-center justify-between gap-4 text-sm">
|
|
171
|
+
<span class="text-slate-400 font-medium">Query String</span>
|
|
172
|
+
<span class="font-code text-slate-300 break-all">{{ request.query_string or "—" }}</span>
|
|
173
|
+
</div>
|
|
174
|
+
<div class="px-6 py-3.5 flex items-center justify-between gap-4 text-sm">
|
|
175
|
+
<span class="text-slate-400 font-medium">Client Address</span>
|
|
176
|
+
<span class="font-code text-slate-300">{{ request.client_addr or "127.0.0.1" }}</span>
|
|
177
|
+
</div>
|
|
178
|
+
<div class="px-6 py-3.5 flex items-center justify-between gap-4 text-sm">
|
|
179
|
+
<span class="text-slate-400 font-medium">Host</span>
|
|
180
|
+
<span class="font-code text-slate-300">{{ request.host or "localhost" }}</span>
|
|
181
|
+
</div>
|
|
182
|
+
</div>
|
|
183
|
+
</div>
|
|
184
|
+
|
|
185
|
+
<!-- HTTP Headers Card -->
|
|
186
|
+
<div class="rounded-2xl border border-slate-800/80 bg-dark-800/60 overflow-hidden shadow-xl backdrop-blur-xl">
|
|
187
|
+
<div class="px-6 py-4 border-b border-slate-800/80 flex items-center gap-2">
|
|
188
|
+
<svg class="w-5 h-5 text-indigo-400" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2">
|
|
189
|
+
<path stroke-linecap="round" stroke-linejoin="round" d="M4 6h16M4 10h16M4 14h16M4 18h16" />
|
|
190
|
+
</svg>
|
|
191
|
+
<h2 class="text-sm font-bold text-slate-200 uppercase tracking-wider">Headers & User Agent</h2>
|
|
192
|
+
</div>
|
|
193
|
+
<div class="divide-y divide-slate-800/60 max-h-[260px] overflow-y-auto">
|
|
194
|
+
<div class="px-6 py-3 text-sm flex flex-col gap-1">
|
|
195
|
+
<span class="text-xs font-semibold text-slate-400 uppercase tracking-wider">User-Agent</span>
|
|
196
|
+
<span class="font-code text-xs text-slate-300 break-all bg-dark-900/60 p-2 rounded-lg border border-slate-800/60">
|
|
197
|
+
{{ request.user_agent or "Unknown User Agent" }}
|
|
198
|
+
</span>
|
|
199
|
+
</div>
|
|
200
|
+
{% if request.headers %}
|
|
201
|
+
{% for header, value in request.headers.items() %}
|
|
202
|
+
{% if header.lower() != 'user-agent' %}
|
|
203
|
+
<div class="px-6 py-2.5 flex items-center justify-between gap-4 text-xs font-code">
|
|
204
|
+
<span class="text-slate-400 font-medium">{{ header }}</span>
|
|
205
|
+
<span class="text-slate-300 truncate max-w-[260px]">{{ value }}</span>
|
|
206
|
+
</div>
|
|
207
|
+
{% endif %}
|
|
208
|
+
{% endfor %}
|
|
209
|
+
{% endif %}
|
|
210
|
+
</div>
|
|
211
|
+
</div>
|
|
212
|
+
|
|
213
|
+
</div>
|
|
214
|
+
|
|
215
|
+
<!-- Footer -->
|
|
216
|
+
<footer class="pt-8 pb-4 text-center text-xs text-slate-500 flex items-center justify-center gap-2">
|
|
217
|
+
<span>Routly • Web & REST API Microframework</span>
|
|
218
|
+
<span>•</span>
|
|
219
|
+
<span class="text-emerald-400 font-semibold">Debug Mode Active</span>
|
|
220
|
+
<span>•</span>
|
|
221
|
+
<span>Never enable DEBUG in production environments</span>
|
|
222
|
+
</footer>
|
|
223
|
+
|
|
224
|
+
</main>
|
|
225
|
+
|
|
226
|
+
<!-- Hidden Raw Traceback for Copying -->
|
|
227
|
+
<textarea id="rawTraceback" class="hidden">{{ tb_text }}</textarea>
|
|
228
|
+
|
|
229
|
+
<!-- JavaScript for Interactive Copy Button -->
|
|
230
|
+
<script>
|
|
231
|
+
function copyTraceback() {
|
|
232
|
+
const raw = document.getElementById('rawTraceback').value;
|
|
233
|
+
navigator.clipboard.writeText(raw).then(() => {
|
|
234
|
+
const text = document.getElementById('copyText');
|
|
235
|
+
const btn = document.getElementById('copyBtn');
|
|
236
|
+
text.innerText = 'Copied to Clipboard!';
|
|
237
|
+
btn.classList.add('bg-emerald-600', 'text-white', 'border-emerald-500');
|
|
238
|
+
btn.classList.remove('bg-slate-800', 'text-slate-200', 'border-slate-700');
|
|
239
|
+
setTimeout(() => {
|
|
240
|
+
text.innerText = 'Copy Traceback';
|
|
241
|
+
btn.classList.remove('bg-emerald-600', 'text-white', 'border-emerald-500');
|
|
242
|
+
btn.classList.add('bg-slate-800', 'text-slate-200', 'border-slate-700');
|
|
243
|
+
}, 2000);
|
|
244
|
+
}).catch(err => {
|
|
245
|
+
console.error('Failed to copy traceback: ', err);
|
|
246
|
+
});
|
|
247
|
+
}
|
|
248
|
+
</script>
|
|
249
|
+
</body>
|
|
250
|
+
</html>
|
routly/http/__init__.py
ADDED
routly/http/request.py
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import contextvars
|
|
2
|
+
from typing import Optional
|
|
3
|
+
from webob import Request as BaseRequest
|
|
4
|
+
|
|
5
|
+
current_request: contextvars.ContextVar[Optional["Request"]] = contextvars.ContextVar(
|
|
6
|
+
"current_request", default=None
|
|
7
|
+
)
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
class Request(BaseRequest):
|
|
11
|
+
"""Represents an incoming HTTP request, extending WebOb's Request with Routly features."""
|
|
12
|
+
|
|
13
|
+
pass
|