Skip to content

Commit

Permalink
V3 (#277)
Browse files Browse the repository at this point in the history
* Merge v3 into main and support for Lilya.
  • Loading branch information
tarsil committed Mar 25, 2024
1 parent 44ca694 commit d3b2568
Show file tree
Hide file tree
Showing 207 changed files with 1,060 additions and 2,034 deletions.
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ repos:
- id: ruff
args: ["--fix", "--line-length=99"]
- repo: https://github.com/psf/black
rev: 24.2.0
rev: 24.3.0
hooks:
- id: black
args: ["--line-length=99"]
Expand Down
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@
Esmerald is a modern, powerful, flexible, high performant, web framework designed to build not only APIs
but also full scalable applications from the smallest to enterprise level.

Esmerald is designed to build with python 3.8+ and based on standard python type hints and on the top of
the heavily known [Starlette](https://github.com/encode/starlette) and [Pydantic](https://github.com/samuelcolvin/pydantic)/[msgspec](https://jcristharif.com/msgspec/).
Esmerald is designed to build with python 3.8+ and based on standard python type hints. Initially
built on the top of [Starlette](https://github.com/encode/starlette) and later on moved to [Lilya](https://lilya.dev) and [Pydantic](https://github.com/samuelcolvin/pydantic)/[msgspec](https://jcristharif.com/msgspec/).

Check out the [Esmerald documentation 📚](https://esmerald.dev)

Expand All @@ -62,7 +62,7 @@ For a job to be done properly, usually it is never done alone and there is alway

Esmerald wouldn't be possible without two colossals:

* <a href="https://www.starlette.io/" class="external-link" target="_blank">Starlette</a>
* <a href="https://www.lilya.dev/" class="external-link" target="_blank">Starlette</a>
* <a href="https://pydantic-docs.helpmanual.io/" class="external-link" target="_blank">Pydantic</a>

## Installation
Expand Down
4 changes: 2 additions & 2 deletions docs/application/applications.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Applications

Esmerald runs Starlette under the hood and therefore includes an application class **Esmerald** that ties
Esmerald runs Lilya under the hood and therefore includes an application class **Esmerald** that ties
of its functionality.

## The Esmerald class
Expand Down Expand Up @@ -79,7 +79,7 @@ requests (HTTP and Websockets).
* **middleware** - A list of middleware to run for every request. A Esmerald application will always include the
middlewares from the configurations passed (CSRF, CORS, JWT...) and the custom user middleware. The middlewares
can be subclasses of the [MiddlewareProtocol](../protocols.md).
or <a href='https://www.starlette.io/middleware/' target='_blank'>Starlette Middleware</a> as they are both converted
or <a href='https://www.lilya.dev/middleware/' target='_blank'>Lilya Middleware</a> as they are both converted
internally. Read more about [Python Protocols](https://peps.python.org/pep-0544/).
* **dependencies** - A dictionary of string and [Inject](.././dependencies.md) instances enable application level dependency
injection.
Expand Down
13 changes: 7 additions & 6 deletions docs/application/settings.md
Original file line number Diff line number Diff line change
Expand Up @@ -293,25 +293,26 @@ Passing parameters in the object will always override the values from the defaul
```python
from esmerald import EsmeraldAPISettings
from esmerald.conf.enums import EnvironmentType
from esmerald.middleware.basic import BasicHTTPMiddleware
from esmerald.middleware.https import HTTPSRedirectMiddleware
from esmerald.types import Middleware
from lilya.middleware import DefineMiddleware


class AppSettings(EsmeraldAPISettings):
debug: bool = False

@property
def middleware(self) -> List[Middleware]:
return [BasicHTTPMiddleware]
return [DefineMiddleware(HTTPSRedirectMiddleware)]

```

The application will:

1. Start with `debug` as `False`.
2. Will start with a middleware `BasicHTTPMiddleware`.
2. Will start with a middleware `HTTPSRedirectMiddleware`.

Starting the application with the above settings will make sure that has an initial `BasicHTTPMiddleware` and `debug`
Starting the application with the above settings will make sure that has an initial `HTTPSRedirectMiddleware` and `debug`
set with values **but** what happens if you use the settings + parameters on instantiation?

```python
Expand All @@ -323,9 +324,9 @@ app = Esmerald(debug=True, middleware=[])
The application will:

1. Start with `debug` as `True`.
2. Will start without custom middlewares it the `BasicHTTPMiddleware` it was overridden by `[]`.
2. Will start without custom middlewares it the `HTTPSRedirectMiddleware` it was overridden by `[]`.

Although it was set in the settings to start with `BasicHTTPMiddleware` and debug as `False`, once you pass different
Although it was set in the settings to start with `HTTPSRedirectMiddleware` and debug as `False`, once you pass different
values in the moment of instantiating an `Esmerald` object, those will become the values to be used.

**Declaring parameters in the instance will always precede the values from your settings**.
Expand Down
13 changes: 6 additions & 7 deletions docs/background-tasks.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
# Background Tasks

Like Starlette and any other Starlette based frameworks, in Esmerald you can define background
tasks to run **after** the returning response.
Like Lilya, in Esmerald you can define background tasks to run **after** the returning response.

This can be useful for those operations that need to happen after the request without blocking the
client (the client doesn't have to wait to complete) from receiving that same response.
Expand All @@ -15,7 +14,7 @@ background.

## How to use

As mentioned before, Esmerald uses the background tasks from Starlette and you can pass them in
As mentioned before, Esmerald uses the background tasks from Lilya and you can pass them in
different ways:

* Via [handlers](#via-handlers)
Expand Down Expand Up @@ -102,8 +101,8 @@ The `.add_task()` receives as arguments:

## Technical information

The class `BackgroundTask` and `BackgroundTasks` come directly from `starlette.background`. This
means you can import directly from Starlette if you want.
The class `BackgroundTask` and `BackgroundTasks` come directly from `lilya.background`. This
means you can import directly from Lilya if you want.

Esmerald imports those classes and adds some extra typing information but without affecting the
overall functionality of the core.
Expand All @@ -112,8 +111,8 @@ You can use `def` or `async def` functions when declaring those functionalities
the `BackgroundTask` and Esmerald will know how to handle those for you.

For more details about these objects, you can see in
[Starlette official docs for Background Tasks](https://www.starlette.io/background/).
[Lilya official docs for Background Tasks](https://www.lilya.dev/tasks/).

## API Reference

Learn more about the `BackgroundTask` by checking the [API Reference](http://localhost:8000/references/background/).
Learn more about the `BackgroundTask` by checking the [API Reference](./references/background.md).
2 changes: 1 addition & 1 deletion docs/configurations/staticfiles.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ StaticFilesConfig is simple set of configurations that when passed enables the b
When a StaticFilesConfig object is passed to an application instance, it will enable the static files serving.

!!! Check
StaticFiles are considereed an `app` and they are pure Starlette app so using Starlette StaticFiles
StaticFiles are considereed an `app` and they are pure Lilya app so using Lilya StaticFiles
will also work with Esmerald.

## StaticFilesConfig and application
Expand Down
8 changes: 4 additions & 4 deletions docs/datastructures.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Datastructures

Esmerald comes equipped with some datastructures. If you want to use the same as Starlette, feel free as they are
Esmerald comes equipped with some datastructures. If you want to use the same as Lilya, feel free as they are
compatible and work out of the box.

## How to access them
Expand All @@ -13,13 +13,13 @@ from esmerald.datastructures import Headers

The available datastructures to be imported from `Esmerald` are as follow.

* **Starlette**:
* **Lilya**:
* `URL`
* `Address`
* `FormData`
* `Headers`
* `MutableHeaders`
* `QueryParams`
* `QueryParam`
* [UploadFile](./extras/upload-files.md)
* `URLPath`
* **Esmerald**:
Expand All @@ -36,5 +36,5 @@ and see how to use them.
All the datastructures can be and are used across the codebase and can be applied by you anywhere.

!!! Note
As mentioned before, all the Starlette datastructures are 100% compatible with `Esmerald`. You can use whatever
As mentioned before, all the Lilya datastructures are 100% compatible with `Esmerald`. You can use whatever
it suits you.
12 changes: 1 addition & 11 deletions docs/external.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,24 +10,14 @@ working solutions built by great minds.

### <a href="https://ariadnegraphql.org/docs/asgi" target="_blank">Ariadne</a>

Great framework with great documentation and great examples how to use it. Currently they also have an integration
with Starlette and that also means **Esmerald**. Their example is with Starlette but here we also provide an example
for Esmerald.
Great framework with great documentation and great examples how to use it.

```python
{!> ../docs_src/external_packages/ariadne.py !}
```

The documentation can be found <a href="https://ariadnegraphql.org/docs/intro" target="_blank">here</a>.

### Ariadne version 0.16.1

!!! Warning
By the time of this writing, `Starlette` was in the version `0.21.0`.
Due to the nature of `Esmerald`, when installing `Ariadne` it will install a lower version and this will
conflict with **Esmerald**. After you install `ariadne` make sure you reinstall `Starlette>=0.21.0`.
Until a new version of the package comes out supporting the latest from Starlette, this is the workaround.

## [Esmerald Sessions](https://esmerald-sessions.dymmond.com/)

Manages sessions for Esmerald using Redis, Memcache or any custom backend.
Expand Down
29 changes: 6 additions & 23 deletions docs/lifespan-events.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,34 +14,17 @@ These cycles cover the **whole** application.

Currently Esmerald supports **on_startup**, **on_shutdown** and **lifespan**.

Esmerald being built on the top of Starlette, it inherited those behaviours, which means, it can be
Esmerald being built on the top of Lilya, it inherited those behaviours, which means, it can be
easily assembled to work directly with these events 🤖.

!!! Deprecation Warning
After the release of Starlette 0.26.0 it was announced the deprecation of **on_startup** and
**on_shutdown** in favour of the newly **lifespan**. Esmerald supports lifespan since its
inception so this won't affect directly the framework.

### Esmerald on_startup and on_shutdown

Since the annoucement of Starlette that `on_startup` and `on_shutdown` would be removed after the
the release 1.0 in favour of `lifespan`, Esmerald decided to follow through since it is 100%
compatible with it.

Despite the deprecation of those fields, Esmerald has an underlying implementation that
**will allow** the `on_startup` and `on_shutdown` to still exist even after this deprecation from
Starlette.

**Does this mean that Esmerald will have to maintain its own events?** No, despite allowing
`on_startup` and `on_shutdown` to exist in the same way it does now, the underlying implementation
changed.

If you pass an `on_startup` and an `on_shutdown` parameters intead of the `lifespan`, Esmerald
will **automatically generate the async context manager** for you and pass it to the `lifespan`
internally for you.

This way Esmerald assures 100% compatibility with Starlette and still maintains the same
"look and feel" as before.
This way Esmerald assures 100% compatibility with Lilya and still maintains the same
"look and feel" as before.

**You can use on_startup/on_shutdown and lifespan but not both at the same time**.

Expand Down Expand Up @@ -105,7 +88,7 @@ Let us see what does it mean in practical examples by changing the previous one
This is quite something to unwrap here. What is actually happening?

So, before you need to explicitly declare the `on_startup` and `on_shutdown` events in the
corresponding parameters in the Esmerald application but with the `lifespan` you do that in
corresponding parameters in the Esmerald application but with the `lifespan` you do that in
**one place only**.

The first part before the `yield` will be executed **before the application starts** and
Expand Down Expand Up @@ -142,9 +125,9 @@ new `lifespan` async context manager directly to it.

## Curiosity about async context managers

This section is out of the scope of the lifespan and events of Esmerald and it is
This section is out of the scope of the lifespan and events of Esmerald and it is
**for curiosity only**. Please see the [lifespan](#lifespan) section as in the case of Esmerald,
the way of **declaring is different** and an `app: Esmerald` parameter is **always required**.
the way of **declaring is different** and an `app: Esmerald` parameter is **always required**.

### General approach to async context managers

Expand Down

0 comments on commit d3b2568

Please sign in to comment.