diff --git a/memos/cmds/library.py b/memos/cmds/library.py index 51c93b9..d2ef2b8 100644 --- a/memos/cmds/library.py +++ b/memos/cmds/library.py @@ -41,7 +41,7 @@ file_detector = Magika() IS_THUMBNAIL = "is_thumbnail" -BASE_URL = f"http://{settings.server_host}:{settings.server_port}" +BASE_URL = settings.server_endpoint include_files = [".jpg", ".jpeg", ".png", ".webp"] diff --git a/memos/cmds/plugin.py b/memos/cmds/plugin.py index ec8fe5b..3a3f9c9 100644 --- a/memos/cmds/plugin.py +++ b/memos/cmds/plugin.py @@ -3,7 +3,7 @@ import httpx from tabulate import tabulate from memos.config import settings -BASE_URL = f"http://{settings.server_host}:{settings.server_port}" +BASE_URL = settings.server_endpoint plugin_app = typer.Typer() diff --git a/memos/commands.py b/memos/commands.py index 23b8735..ca3646e 100644 --- a/memos/commands.py +++ b/memos/commands.py @@ -26,9 +26,6 @@ from tabulate import tabulate app = typer.Typer(context_settings={"help_option_names": ["-h", "--help"]}) -app.add_typer(plugin_app, name="plugin") -app.add_typer(lib_app, name="lib") - BASE_URL = settings.server_endpoint # Configure logging @@ -42,6 +39,44 @@ logging.getLogger("httpx").setLevel(logging.ERROR) logging.getLogger("typer").setLevel(logging.ERROR) +def check_server_health(): + """Check if the server is running and healthy.""" + try: + response = httpx.get(f"{BASE_URL}/health", timeout=5) + return response.status_code == 200 + except httpx.RequestError: + return False + + +def callback(ctx: typer.Context): + """Callback to check server health before running any command.""" + # List of commands that require the server to be running + server_dependent_commands = [ + "scan", + "typesense-index", + "reindex", + "watch", + + "ls", + "create", + "add-folder", + "show", + "sync", + + "bind", + "unbind", + ] + + if ctx.invoked_subcommand in server_dependent_commands: + if not check_server_health(): + typer.echo("Error: Server is not running. Please start the server first.") + raise typer.Exit(code=1) + + +app.add_typer(plugin_app, name="plugin") +app.add_typer(lib_app, name="lib", callback=callback) + + @app.command() def serve(): """Run the server after initializing if necessary.""" @@ -207,7 +242,9 @@ def record( """ Record screenshots of the screen. """ - base_dir = os.path.expanduser(base_dir) if base_dir else settings.resolved_screenshots_dir + base_dir = ( + os.path.expanduser(base_dir) if base_dir else settings.resolved_screenshots_dir + ) previous_hashes = load_previous_hashes(base_dir) if once: diff --git a/memos/server.py b/memos/server.py index 02e29a7..2ebf4e2 100644 --- a/memos/server.py +++ b/memos/server.py @@ -90,6 +90,10 @@ app.mount( "/_app", StaticFiles(directory=os.path.join(current_dir, "static/_app"), html=True) ) +@app.get("/health") +async def health(): + return {"status": "ok"} + @app.get("/favicon.png", response_class=FileResponse) async def favicon_png():