Skip to content

Commit

Permalink
Documentation for decorators (#361)
Browse files Browse the repository at this point in the history
Documentation for decorators
  • Loading branch information
tarsil authored Jul 17, 2024
1 parent 0051e5c commit a0b007b
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 0 deletions.
34 changes: 34 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
34 changes: 34 additions & 0 deletions docs/en/docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit a0b007b

Please sign in to comment.