# Autodiscovery of Blueprints, Middleware, and Listeners

How do I autodiscover the components I am using to build my application?

One of the first problems someone faces when building an application, is how to structure the project. Sanic makes heavy use of decorators to register route handlers, middleware, and listeners. And, after creating blueprints, they need to be mounted to the application.

A possible solution is a single file in which everything is imported and applied to the Sanic instance. Another is passing around the Sanic instance as a global variable. Both of these solutions have their drawbacks.

An alternative is autodiscovery. You point your application at modules (already imported, or strings), and let it wire everything up.

    here is the dir tree
    generate with 'find . -type d -name "__pycache__" -exec rm -rf {} +; tree'
    . # run 'sanic sever -d' here
    ├── blueprints
    │   ├── __init__.py # you need add this file, just empty
    │   ├── level1.py
    │   └── one
    │       └── two
    │           └── level3.py
    ├── listeners
    │   └── something.py
    ├── parent
    │   └── child
    │       ├── __init__.py
    │       └── nested.py
    ├── server.py
    └── utility.py
    
    . ./.venv/bin/activate # activate the python venv which sanic is installed in
    sanic sever -d # run this in the directory containing server.py
    
    you will see "something ***" like this:
    [2023-07-12 11:23:36 +0000] [113704] [DEBUG] something
    [2023-07-12 11:23:36 +0000] [113704] [DEBUG] something inside __init__.py
    [2023-07-12 11:23:36 +0000] [113704] [DEBUG] something @ level3
    [2023-07-12 11:23:36 +0000] [113704] [DEBUG] something @ level1
    [2023-07-12 11:23:36 +0000] [113704] [DEBUG] something @ nested
    
    MIT Licensed
    Copyright © 2018-present Sanic Community Organization

    ~ Made with ❤️ and ☕️ ~