Skip to content

Commit b595de9

Browse files
committed
feat(examples): add AsyncDemoEndpoint for asynchronous handling
Introduces AsyncDemoEndpoint to demonstrate asynchronous endpoint capabilities within the mega example. This new endpoint simulates async work and provides a clear example of how to implement async functionality in LightAPI. Updates the main application to register the new endpoint and includes usage instructions in the output. These changes enhance the framework's demonstration of async performance features.
1 parent 1d1122a commit b595de9

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed

examples/mega_example.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -550,6 +550,15 @@ def post(self, request):
550550
return {"message": f"Hello, {name}!", "timestamp": time.time()}, 201
551551

552552

553+
# --- Async Demo Endpoint ---
554+
class AsyncDemoEndpoint(RestEndpoint):
555+
__abstract__ = True
556+
557+
async def get(self, request):
558+
await asyncio.sleep(0.2) # Simulate async work
559+
return {"message": "This is an async endpoint!", "timestamp": time.time()}, 200
560+
561+
553562
# --- Main App ---
554563
def init_database():
555564
engine = create_engine("sqlite:///mega_example.db")
@@ -625,13 +634,17 @@ class ConfigurableCacheEndpointCustom(ConfigurableCacheEndpoint):
625634
class HelloWorldEndpointCustom(HelloWorldEndpoint):
626635
route_patterns = ["/hello"]
627636

637+
class AsyncDemoEndpointCustom(AsyncDemoEndpoint):
638+
route_patterns = ["/async_demo"]
639+
628640
app.register(AuthEndpointCustom)
629641
app.register(PublicResourceCustom)
630642
app.register(SecretResourceCustom)
631643
app.register(UserProfileCustom)
632644
app.register(WeatherEndpointCustom)
633645
app.register(ConfigurableCacheEndpointCustom)
634646
app.register(HelloWorldEndpointCustom)
647+
app.register(AsyncDemoEndpointCustom)
635648
# Add middleware
636649
app.add_middleware([LoggingMiddleware, CORSMiddleware, RateLimitMiddleware, AuthenticationMiddleware])
637650
print("Server running at http://localhost:8000")
@@ -657,4 +670,6 @@ class HelloWorldEndpointCustom(HelloWorldEndpoint):
657670
)
658671
print("9. Access protected resource:")
659672
print(" curl -X GET http://localhost:8000/secret -H 'Authorization: Bearer YOUR_TOKEN'")
673+
print("10. Async demo endpoint:")
674+
print(" curl http://localhost:8000/async_demo")
660675
app.run(host="localhost", port=8000, debug=True)

0 commit comments

Comments
 (0)