mirror of
https://github.com/YunoHost/yunohost.git
synced 2024-09-03 20:06:10 +02:00
separate app factories
This commit is contained in:
parent
fee2ee741c
commit
de7f2adb4c
5 changed files with 51 additions and 8 deletions
|
@ -18,7 +18,7 @@ def main(
|
||||||
Run the YunoHost API to manage your server.
|
Run the YunoHost API to manage your server.
|
||||||
"""
|
"""
|
||||||
uvicorn.run(
|
uvicorn.run(
|
||||||
"yunohost.__init_:create_interface",
|
"yunohost.__init_:create_api_interface",
|
||||||
factory=True,
|
factory=True,
|
||||||
host=host,
|
host=host,
|
||||||
port=port,
|
port=port,
|
||||||
|
|
|
@ -13,7 +13,7 @@ if os.environ["PATH"] != default_path:
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
|
|
||||||
from yunohost.__init_ import create_interface
|
from yunohost.__init_ import create_cli_interface
|
||||||
|
|
||||||
if os.geteuid() != 0:
|
if os.geteuid() != 0:
|
||||||
sys.stderr.write(
|
sys.stderr.write(
|
||||||
|
@ -22,5 +22,5 @@ if __name__ == "__main__":
|
||||||
)
|
)
|
||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
|
|
||||||
app = create_interface()
|
app = create_cli_interface()
|
||||||
app()
|
app()
|
||||||
|
|
|
@ -1,10 +1,46 @@
|
||||||
|
import logging
|
||||||
|
from rich.logging import RichHandler
|
||||||
from yunohost.interface import Interface
|
from yunohost.interface import Interface
|
||||||
|
from yunohost.user import user, group, permission
|
||||||
|
|
||||||
from yunohost.user import app as user_app
|
from moulinette import m18n
|
||||||
|
from moulinette.interfaces.cli import get_locale
|
||||||
|
|
||||||
|
|
||||||
def create_interface():
|
def create_cli_interface():
|
||||||
|
init_i18n()
|
||||||
|
init_logging(interface="cli")
|
||||||
|
|
||||||
app = Interface(root=True)
|
app = Interface(root=True)
|
||||||
app.add(user_app)
|
|
||||||
|
user.add(group)
|
||||||
|
user.add(permission)
|
||||||
|
app.add(user)
|
||||||
|
|
||||||
return app.instance
|
return app.instance
|
||||||
|
|
||||||
|
|
||||||
|
def create_api_interface():
|
||||||
|
init_i18n()
|
||||||
|
init_logging(interface="cli")
|
||||||
|
|
||||||
|
app = Interface(root=True)
|
||||||
|
# Intermediate router to have distincts categories in swagger
|
||||||
|
user_sub = Interface(prefix="/users")
|
||||||
|
user_sub.add(user)
|
||||||
|
user_sub.add(group)
|
||||||
|
user_sub.add(permission)
|
||||||
|
app.add(user_sub)
|
||||||
|
|
||||||
|
return app.instance
|
||||||
|
|
||||||
|
|
||||||
|
def init_i18n():
|
||||||
|
m18n.set_locales_dir("/usr/share/yunohost/locales/")
|
||||||
|
m18n.set_locale(get_locale())
|
||||||
|
|
||||||
|
|
||||||
|
def init_logging(interface="cli", debug=False, quiet=False, logdir="/var/log/yunohost"):
|
||||||
|
logging.basicConfig(
|
||||||
|
level="NOTSET", format="%(message)s", handlers=[RichHandler(show_time=False)]
|
||||||
|
)
|
||||||
|
|
|
@ -67,7 +67,7 @@ class Interface(BaseInterface):
|
||||||
def add(self, interface: Interface):
|
def add(self, interface: Interface):
|
||||||
assert isinstance(interface.instance, fastapi.APIRouter)
|
assert isinstance(interface.instance, fastapi.APIRouter)
|
||||||
self.instance.include_router(
|
self.instance.include_router(
|
||||||
interface.instance, prefix=f"/{interface.name}", tags=[interface.name]
|
interface.instance, prefix=interface.prefix, tags=[interface.name] if interface.name else []
|
||||||
)
|
)
|
||||||
|
|
||||||
def prepare_params(
|
def prepare_params(
|
||||||
|
|
|
@ -68,9 +68,16 @@ class BaseInterface:
|
||||||
kind: InterfaceKind
|
kind: InterfaceKind
|
||||||
local_data: dict[str, Any] = {}
|
local_data: dict[str, Any] = {}
|
||||||
|
|
||||||
def __init__(self, root: bool = False, name: str = "", help: str = ""):
|
def __init__(
|
||||||
|
self,
|
||||||
|
root: bool = False,
|
||||||
|
name: str = "",
|
||||||
|
help: str = "",
|
||||||
|
prefix: str = "",
|
||||||
|
):
|
||||||
self.name = "root" if root else name or ""
|
self.name = "root" if root else name or ""
|
||||||
self.help = help
|
self.help = help
|
||||||
|
self.prefix = prefix
|
||||||
|
|
||||||
def __call__(self, *args, **kwargs):
|
def __call__(self, *args, **kwargs):
|
||||||
self.local_data = kwargs
|
self.local_data = kwargs
|
||||||
|
|
Loading…
Add table
Reference in a new issue