Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add automatic imports of controllers and routes #334

Open
RobertoPrevato opened this issue Apr 24, 2023 · 0 comments
Open

Add automatic imports of controllers and routes #334

RobertoPrevato opened this issue Apr 24, 2023 · 0 comments
Assignees
Labels
enhancement New feature or request fixed in branch Already fixed in a branch, waiting to be merged needs docs

Comments

@RobertoPrevato
Copy link
Member

RobertoPrevato commented Apr 24, 2023

Explicit is better than implicit. However, having to import controllers and routes explicitly is boring and annoying!

Consider the following example:

├── app 📁 
│   ├── controllers 📁 
│   │   ├── __init__.py
│   │   ├── comments.py
│   │   ├── favorites.py
│   │   ├── home.py
│   │   ├── reactions.py
│   │   ├── reviews.py
│   │   ├── topics.py
│   │   └── users.py
│   ├── __init__.py
│   ├── program.py <-- the app is instantiated here

The user currently needs to import Controller types defined in each module inside the controllers package explicitly.
Example:

# app/controllers/__init__.py

from .home import HomeController as HomeController
from .comments import CommentsController as CommentsController
from .favorites import FavoritesController as FavoritesController
from .reactions import ReactionsController as ReactionsController
from .reviews import ReviewsController as ReviewsController
from .topics import TopicsController as TopicsController
from .users import UsersController as UsersController

Note: as added to make linters happy and remove unused imports lints.

from . import controllers  # NoQA

...

This has downsides:

  • it makes for a poor development experience
  • imports look unused even if they are used, making linters unhappy
  • increases code verbosity adding little value

In this case, adopting convention over configuration can help improving the situation:

  • all modules inside a controllers package under the same namespace where the Application class is instantiated might be automatically imported

It would be nice to support the same for regular routes defined using request handlers. But to achieve this, it is also necessary to support importing get, post, put, etc. methods to register request handlers in a similar fashion as already supported for controllers.

Therefore the blacksheep.server.routing module should export a default router that is used by default, when no router is explicitly configured for the application.

In the final form, routes would also be automatically imported:

.
├── main.py
└── routes  ⭐
    ├── example.py
    ├── home.py
    └── __init__.py
# routes/home.py

from blacksheep import get

@get("/")
async def home():
    return "Hello, World!"

In such scenario, routes would be automatically imported when instantiating an Application class that uses the default router.

# main.py
from blacksheep import Application

app = Application()

Users who want to maintain things explicit has still the option of doing so:

  • disabling the default router using a dedicated environment variable
  • adding explicit imports
  • configuring the Application instance to use a specific Router
@RobertoPrevato RobertoPrevato added the enhancement New feature or request label Apr 24, 2023
@RobertoPrevato RobertoPrevato self-assigned this Apr 24, 2023
RobertoPrevato added a commit that referenced this issue Apr 25, 2023
* Apply minor corrections to GzipMiddleware
* Fix ensure_response type annotation
* Add features to describe app environment
* Rename gzip.py to compression.py
* Add auto-import of controllers and routes #334 
* Update CHANGELOG.md
* Adopt pyproject.toml
@RobertoPrevato RobertoPrevato added fixed in branch Already fixed in a branch, waiting to be merged needs docs labels Apr 25, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request fixed in branch Already fixed in a branch, waiting to be merged needs docs
Projects
None yet
Development

No branches or pull requests

1 participant