You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/index.md
+1-1Lines changed: 1 addition & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -23,7 +23,7 @@ Ellar was deeply influenced by [NestJS](https://docs.nestjs.com/){target="_blank
23
23
Also, Ellar took some concepts from [FastAPI](https://fastapi.tiangolo.com/){target="_blank"} in terms of request parameter handling and data serialization with Pydantic.
24
24
25
25
The objective of Ellar is to provide a high level of abstracted interface to your python web app, along with a well-structured project setup, give room for object-oriented approach to web application design,
26
-
allow you chose your desired application architecture, and ultimately, deliver speedy handling to requests using any ASGI server.
26
+
allow you to choose your desired application architecture, and ultimately, deliver speedy handling to requests using any ASGI server.
Copy file name to clipboardExpand all lines: docs/overview/controllers.md
+28-23Lines changed: 28 additions & 23 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,10 +1,11 @@
1
1
# **Controllers**
2
-
The Controller is responsible for handling incoming requests and returning responses to the client.
3
-
The purpose of a controller is to receive specific requests for an application `ApplicationRouter`. `ApplicationRouter` on the other hand, decides which `controller` should handle an incoming request.
2
+
The Controller plays a crucial role in managing incoming requests and providing responses to clients.
3
+
Its primary function is to handle specific requests directed to an application's `ApplicationRouter`.
4
+
In turn, the `ApplicationRouter` determines the appropriate `controller` to manage the incoming request.
Controllers can be said to be a router with many routes registered in them.
8
+
Conceptually, controllers can be likened to routers with multiple registered routes within them.
8
9
9
10
### **Creating a Controller**
10
11
To create a controller, we use classes and decorators. The `Controller` decorator associates classes with a required
@@ -19,13 +20,13 @@ class UserController(ControllerBase):
19
20
```
20
21
21
22
## **Routing**
22
-
In this section, we are going to highlight key features of the `@Controller()`, a `class decorator`
23
-
for defining a controller. By default, `@Controller()` will create a path prefix `/car` gotten from the class name in `Car`Controller.
24
-
This will be used to group related routes and minimize duplicate route definitions.
25
-
26
-
For example, we may choose to group a set of routes that manage interactions with a customer entity under the route `/user`.
27
-
In that case, we could specify the path prefix `/user` in the `@Controller()` decorator so we don't have to repeat that portion of the path for each route in the controller.
23
+
In this section, we will outline the key features of `@Controller()`, a `class decorator` designed for defining a controller.
24
+
By default, when applied, `@Controller()` generates a path prefix based on the class name, such as `/car`
25
+
for a controller named `CarController`. This feature aims to organize and group related routes, reducing redundancy in route definitions.
28
26
27
+
For instance, if we want to group a collection of routes managing interactions with a customer entity under the route `/user`,
28
+
we can specify the path prefix `/user` in the `@Controller()` decorator.
29
+
This ensures that we don't need to repeat this portion of the path for each route within the controller.
29
30
```python
30
31
# project_name/apps/car/controllers.py
31
32
@@ -43,17 +44,20 @@ class CarController(ControllerBase):
43
44
```
44
45
45
46
!!! hint
46
-
Class Decorators name are capitalized while function/method decorator name are in lower case
47
+
Class decorators are conventionally named with capital letters, while function/method decorator names typically use lowercase letters.
48
+
47
49
48
-
The `@get()` HTTP method decorator before the `get_all(self)` method marks `get_all(self)` as the HTTP request handler that will handle a specific endpoint matching the route path and HTTP method of `GET`.
50
+
The `@get()` HTTP method decorator preceding the `get_all(self)` method designates `get_all(self)` as the HTTP request
51
+
handler responsible for handling a specific endpoint matching the route path and HTTP method of `GET`.
49
52
50
-
But what then is the route path of `get_all(self)`? The route path is determined by concatenating the controller `path prefix` and the path specified in the HTTP method function decorator `@get()`.
53
+
But what exactly is the route path for `get_all(self)`? The route path is determined by combining the controller's
54
+
`path prefix` and the path specified in the HTTP method function decorator `@get()`.
51
55
52
-
For example, we've declared a prefix for every route `(car)`, and haven't added any path information in the decorator, which means the path will default to `/`. In that case,
53
-
Ellar will map`GET /car/` requests to the `get_all(self)` handler.
56
+
For instance, if we've set a prefix for every route `(car)` and haven't added any path information in the
57
+
decorator, it means the path defaults to `/`. In this case, Ellar will associate`GET /car/` requests with the `get_all(self)` handler.
54
58
55
-
Another example to help make things clear, a path prefix of `/users`combined with the decorator `@get('/profile')` would produce a route mapping for requests like
56
-
`GET /users/profile`.
59
+
To illustrate further, if we have a path prefix of `/users`and include the decorator `@get('/profile')`,
60
+
it would result in a route mapping for requests like `GET /users/profile`.
57
61
58
62
59
63
### **Overview of HTTP function decorator parameters:**
Copy file name to clipboardExpand all lines: docs/overview/module-router.md
+18-14Lines changed: 18 additions & 14 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,15 +1,14 @@
1
1
# **Module Router**
2
2
3
-
ModuleRouter allows you to define your route handlers as standalone functions, providing an alternative to using classes.
4
-
This can be beneficial for python developers who prefer using functions.
5
-
It is important to note that using ModuleRouter does not limit your access to other features provided by Ellar.
3
+
`ModuleRouter` allows you to define your route handlers as standalone functions, offering an alternative to using classes.
4
+
This can be advantageous for Python developers who prefer using functions.
5
+
Importantly, using `ModuleRouter` does not restrict your access to other features provided by Ellar.
6
6
7
7
## **Usage**
8
-
The Ellar CLI tool generates a `routers.py` file in every `create-module` scaffold command.
9
-
This file contains a quick guide on how to use the `ModuleRouter` class.
10
-
11
-
Let's use the **routers.py** created in our previous project. And create **two** route functions, **addition** and **subtraction**
8
+
The Ellar CLI tool automatically generates a `routers.py` file with every `create-module` scaffold command.
9
+
This file serves as a concise guide on utilizing the `ModuleRouter` class.
12
10
11
+
Now, let's leverage the **routers.py** file generated in our prior project to implement **two** route functions, namely **addition** and **subtraction**.
13
12
```python
14
13
# project_name/apps/car/routers.py
15
14
"""
@@ -23,25 +22,30 @@ def index(request: Request):
23
22
return {'detail': 'Welcome to Cats Resource'}
24
23
"""
25
24
from ellar.common import ModuleRouter
25
+
from ellar.openapi import ApiTags
26
26
27
-
math_router = ModuleRouter('/math', tag='Math')
27
+
math_router = ModuleRouter('/math')
28
+
open_api_tag = ApiTags(name='Math')
29
+
open_api_tag(math_router.get_control_type())
28
30
29
31
@math_router.get('/add')
30
-
defaddition(a:int, b:int):
32
+
defaddition(a:int, b:int):
31
33
return a + b
32
34
33
35
34
36
@math_router.get('/subtract')
35
-
defsubtraction(a:int, b:int):
37
+
defsubtraction(a:int, b:int):
36
38
return a - b
39
+
37
40
```
38
-
In the example above, we created `math_router` with a prefix `/math` and a OPENAPI tag 'math'. Then we added two routes `addition(a:int, b:int)` and `subtraction(a:int, b:int)`.
39
-
Each route takes two query parameters, 'a' and 'b' which are declared as int type. These functions handle the query parameters and return the result of the mathematical operation.
40
41
41
-
Next, we have to make the `math_router` visible to the application
42
+
In the provided example, the `math_router` is created with a prefix `/math` and an OPENAPI tag 'Math'.
43
+
Two routes, `addition(a:int, b:int)` and `subtraction(a:int, b:int)`, are added to the router, each handling two query parameters ('a' and 'b') of integer type. These functions perform the specified mathematical operations and return the results.
44
+
45
+
To make the `math_router` visible to the application, it is registered with the current injector using `current_injector.register(ModuleRouter, math_router)`. This step ensures that the router is recognized and accessible within the application.
42
46
43
47
## **Registering Module Router**
44
-
Like controllers, ModuleRouters also need to be registered to their root module in order to be used in a web application.
48
+
Like controllers, ModuleRouters also need to be registered to their root module to be used in a web application.
45
49
In the example provided above, the `math_router` would be registered under the `project_name/apps/car/module.py` file.
46
50
47
51
This registration process typically involves importing the `math_router` and then adding it to the list of `routers` in the `module.py` file.
The best way to organize your components is to build your projects as `Modules`.
8
+
Organizing components within projects as `Modules` is considered a best practice.
9
+
The `ApplicationModule` serves as the starting point or root module for constructing the application module tree.
10
+
This internal data structure facilitates the resolution of relationships, dependencies, and interactions between modules and providers.
9
11
10
-
The `ApplicationModule` is the entry-point/root module to building the application module tree -
11
-
the internal data structure used to resolve `module` and `provider` relationships and dependencies.
12
-
13
-
Thus, the architecture resulting from most applications will include multiple modules with closely related **functionality**.
12
+
Consequently, the typical architecture of applications involves multiple modules, each encapsulating closely related functionality.
14
13
15
14
## **Feature modules**
16
-
Building an application as a group of feature modules bundled together helps to manage complexity, have a maintainable, extendable, and testable code base, and encourage development using SOLID principles.
17
-
18
-
A typical example of a feature module is the **car** project. The `CarModule` wraps all the services and controller that manages the `car` resource which makes it easy to maintain, extend, and testable.
0 commit comments