-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d5cb833
commit 76dfa96
Showing
5 changed files
with
53 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import os | ||
from fastapi import FastAPI | ||
import importlib.util | ||
import logging | ||
|
||
logger = logging.getLogger("uvicorn") | ||
|
||
modules = [] | ||
|
||
|
||
def load_extensions(app: FastAPI): | ||
global modules | ||
# Extension Loading | ||
extensions_dir = "/src/app/extensions/" | ||
if not os.path.exists(extensions_dir): | ||
return | ||
for item in os.listdir(extensions_dir): | ||
item_path = os.path.join(extensions_dir, item) | ||
init_file = os.path.join(item_path, "__init__.py") | ||
try: | ||
readme_file = open(os.path.join(item_path, "README.md"), "r").read() | ||
except: | ||
logger.error(f"Extension {item} is missing README.md") | ||
continue | ||
if os.path.isdir(item_path) and os.path.isfile(init_file): | ||
spec = importlib.util.spec_from_file_location(item, init_file) | ||
module = importlib.util.module_from_spec(spec) | ||
try: | ||
spec.loader.exec_module(module) | ||
except Exception as e: | ||
logger.error(f"Failed to load extension {item}: {e}") | ||
if logger.level == logging.INFO: | ||
raise e | ||
continue | ||
modules.append([spec, module, readme_file]) | ||
|
||
for spec, module, _ in modules: | ||
app.include_router(module.router, prefix=f"/ext/{module.__name__}") | ||
|
||
logger.info( | ||
"\u001b[32m-> Loaded Extensions: " | ||
+ ", ".join([module.__name__ for spec, module, _ in modules]) | ||
+ "\u001b[0m" | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters