From a0b007bc008144312b6b93b0c185380aa4cd5f16 Mon Sep 17 00:00:00 2001 From: Tiago Silva Date: Wed, 17 Jul 2024 17:11:03 +0100 Subject: [PATCH] Documentation for decorators (#361) Documentation for decorators --- README.md | 34 ++++++++++++++++++++++++++++++++++ docs/en/docs/index.md | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 68 insertions(+) diff --git a/README.md b/README.md index f4230de5..2082158d 100644 --- a/README.md +++ b/README.md @@ -226,6 +226,40 @@ if __name__ == "__main__": Then you can access the endpoints. +### Using Esmerald as a decorator + +To quickly start with Esmerald you can also use it as decorator, you can just do this. Using `uvicorn` as example. + +```python +#!/usr/bin/env python +import uvicorn + +from esmerald import Esmerald, Gateway, JSONResponse, Request, get + + +app = Esmerald() + + +@app.get("/esmerald") +def welcome() -> JSONResponse: + return JSONResponse({"message": "Welcome to Esmerald"}) + + +@app.get("/esmerald/{user}") +def user(user: str) -> JSONResponse: + return JSONResponse({"message": f"Welcome to Esmerald, {user}"}) + + +@app.get("/esmerald/in-request/{user}") +def user_in_request(request: Request) -> JSONResponse: + user = request.path_params["user"] + return JSONResponse({"message": f"Welcome to Esmerald, {user}"}) + + +if __name__ == "__main__": + uvicorn.run(app, port=8000) +``` + ## Settings Like every other framework, when starting an application, a lot of [settings](./application/settings.md) can/need to be diff --git a/docs/en/docs/index.md b/docs/en/docs/index.md index 4aef6ae6..49ae433f 100644 --- a/docs/en/docs/index.md +++ b/docs/en/docs/index.md @@ -232,6 +232,40 @@ if __name__ == "__main__": Then you can access the endpoints. +### Using Esmerald as a decorator + +To quickly start with Esmerald you can also use it as decorator, you can just do this. Using `uvicorn` as example. + +```python +#!/usr/bin/env python +import uvicorn + +from esmerald import Esmerald, Gateway, JSONResponse, Request, get + + +app = Esmerald() + + +@app.get("/esmerald") +def welcome() -> JSONResponse: + return JSONResponse({"message": "Welcome to Esmerald"}) + + +@app.get("/esmerald/{user}") +def user(user: str) -> JSONResponse: + return JSONResponse({"message": f"Welcome to Esmerald, {user}"}) + + +@app.get("/esmerald/in-request/{user}") +def user_in_request(request: Request) -> JSONResponse: + user = request.path_params["user"] + return JSONResponse({"message": f"Welcome to Esmerald, {user}"}) + + +if __name__ == "__main__": + uvicorn.run(app, port=8000) +``` + ## Settings Like every other framework, when starting an application, a lot of [settings](./application/settings.md) can/need to be