From 42dbf4686496b329de17b9295962067d7028dd56 Mon Sep 17 00:00:00 2001 From: Hu Yueh-Wei Date: Wed, 25 Dec 2024 16:15:48 +0800 Subject: [PATCH] fix: split the handling of / and /api/ of tman designer --- .vscode/launch.json | 2 - core/src/ten_manager/src/cmd/cmd_designer.rs | 34 ++--- core/src/ten_manager/src/designer/mod.rs | 142 ++++++++++--------- 3 files changed, 88 insertions(+), 90 deletions(-) diff --git a/.vscode/launch.json b/.vscode/launch.json index d45d566b8..dd93cb081 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -418,9 +418,7 @@ "program": "${workspaceFolder}/core/src/ten_manager/target/x86_64-unknown-linux-gnu/debug/tman", "cwd": "${workspaceFolder}/core/src/ten_manager/target/x86_64-unknown-linux-gnu/debug/", "args": [ - "--verbose", "designer", - "--base-dir=/home/wei/MyData/MyProject/ten_framework_internal_base/ten_framework/out/linux/x64/tests/ten_runtime/integration/python/aio_http_server_python/aio_http_server_python_app/", ], "preRunCommands": [ "script import pathlib;import subprocess;import lldb;rustc_sysroot = subprocess.getoutput(\"rustc --print sysroot\");rustlib_etc = pathlib.Path(rustc_sysroot) / \"lib\" / \"rustlib\" / \"etc\";lldb.debugger.HandleCommand(f'command script import \"{rustlib_etc / \"lldb_lookup.py\"}\"');lldb.debugger.HandleCommand(f'command source -s 0 \"{rustlib_etc / \"lldb_commands\"}\"')" diff --git a/core/src/ten_manager/src/cmd/cmd_designer.rs b/core/src/ten_manager/src/cmd/cmd_designer.rs index 46864cfc5..e3d7b4b29 100644 --- a/core/src/ten_manager/src/cmd/cmd_designer.rs +++ b/core/src/ten_manager/src/cmd/cmd_designer.rs @@ -10,7 +10,6 @@ use std::{ }; use actix_cors::Cors; -use actix_files::Files; use actix_web::{http::header, web, App, HttpServer}; use anyhow::{Ok, Result}; use clap::{value_parser, Arg, ArgMatches, Command}; @@ -19,12 +18,12 @@ use console::Emoji; use crate::{ config::TmanConfig, - designer::{configure_routes, frontend::get_frontend_asset, DesignerState}, + designer::{configure_routes, DesignerState}, log::tman_verbose_println, utils::{check_is_app_folder, get_cwd}, }; -#[derive(Debug)] +#[derive(Clone, Debug)] pub struct DesignerCommand { pub ip_address: String, pub port: u16, @@ -106,10 +105,10 @@ pub async fn execute_cmd( tman_verbose_println!(tman_config, "{:?}", command_data); tman_verbose_println!(tman_config, "{:?}", tman_config); - let base_dir = match command_data.base_dir { + let base_dir = match &command_data.base_dir { Some(base_dir) => { println!("Base directory: {}", base_dir); - base_dir + base_dir.clone() } None => { let cwd = get_cwd()?.to_str().unwrap_or_default().to_string(); @@ -120,7 +119,7 @@ pub async fn execute_cmd( &cwd ); - cwd + cwd.clone() } }; @@ -144,6 +143,8 @@ pub async fn execute_cmd( tman_config: TmanConfig::default(), })); + let command_data_cloned = command_data.clone(); + let server = HttpServer::new(move || { let state = web::Data::new(state.clone()); @@ -154,25 +155,12 @@ pub async fn execute_cmd( .allowed_header(header::CONTENT_TYPE) .max_age(3600); - let mut app = App::new() + App::new() .app_data(state.clone()) .wrap(cors) - .configure(|cfg| configure_routes(cfg, state.clone())); - - if let Some(external_frontend_asset_path) = - &command_data.external_frontend_asset_path - { - let static_files = Files::new("/", external_frontend_asset_path) - .index_file("index.html") - .use_last_modified(true) - .use_etag(true); - - app = app.service(static_files); - } else { - app = app.default_service(web::route().to(get_frontend_asset)); - } - - app + .configure(|cfg| { + configure_routes(cfg, &command_data_cloned, state.clone()) + }) }); let bind_address = diff --git a/core/src/ten_manager/src/designer/mod.rs b/core/src/ten_manager/src/designer/mod.rs index 02b3954f7..5b6d16783 100644 --- a/core/src/ten_manager/src/designer/mod.rs +++ b/core/src/ten_manager/src/designer/mod.rs @@ -23,12 +23,15 @@ mod version; use std::sync::{Arc, RwLock}; +use actix_files::Files; use actix_web::web; +use frontend::get_frontend_asset; use ten_rust::pkg_info::PkgInfo; -use terminal::ws_terminal; use super::config::TmanConfig; +use crate::cmd::cmd_designer::DesignerCommand; +use terminal::ws_terminal; use version::get_version; pub struct DesignerState { @@ -39,70 +42,79 @@ pub struct DesignerState { pub fn configure_routes( cfg: &mut web::ServiceConfig, + command_data: &DesignerCommand, state: web::Data>>, ) { - cfg.app_data(state.clone()) - .route("/api/designer/v1/version", web::get().to(get_version)) - .route( - "/api/designer/v1/addons/extensions", - web::get().to(addons::extensions::get_extension_addons), - ) - .route( - "/api/designer/v1/addons/extensions/{name}", - web::get().to(addons::extensions::get_extension_addon_by_name), - ) - .route( - "/api/designer/v1/packages/reload", - web::post().to(packages::reload::clear_and_reload_pkgs), - ) - .route("/api/designer/v1/graphs", web::get().to(graphs::get_graphs)) - .route( - "/api/designer/v1/graphs/{graph_name}/nodes", - web::get().to(graphs::nodes::get_graph_nodes), - ) - .route( - "/api/designer/v1/graphs/{graph_name}/connections", - web::get().to(graphs::connections::get_graph_connections), - ) - .route( - "/api/designer/v1/graphs/{graph_name}", - web::put().to(graphs::update::update_graph), - ) - .route( - "/api/designer/v1/manifest", - web::put().to(manifest::dump::dump_manifest), - ) - .route( - "/api/designer/v1/manifest/check", - web::get().to(manifest::check::check_manifest), - ) - .route( - "/api/designer/v1/property", - web::put().to(property::dump::dump_property), - ) - .route( - "/api/designer/v1/property/check", - web::get().to(property::check::check_property), - ) - .route( - "/api/designer/v1/messages/compatible", - web::post().to(messages::compatible::get_compatible_messages), - ) - .route( - "/api/designer/v1/file-content/{path}", - web::get().to(file_content::get_file_content), - ) - .route( - "/api/designer/v1/file-content/{path}", - web::put().to(file_content::save_file_content), - ) - .route( - "/api/designer/v1/base-dir", - web::put().to(base_dir::set_base_dir), - ) - .route( - "/api/designer/v1/dir-list/{path}", - web::get().to(dir_list::list_dir), - ) - .route("/ws/terminal", web::get().to(ws_terminal)); + cfg.service( + web::scope("/api/designer/v1") + .app_data(state.clone()) + .route("/version", web::get().to(get_version)) + .route( + "/addons/extensions", + web::get().to(addons::extensions::get_extension_addons), + ) + .route( + "/addons/extensions/{name}", + web::get().to(addons::extensions::get_extension_addon_by_name), + ) + .route( + "/packages/reload", + web::post().to(packages::reload::clear_and_reload_pkgs), + ) + .route("/graphs", web::get().to(graphs::get_graphs)) + .route( + "/graphs/{graph_name}/nodes", + web::get().to(graphs::nodes::get_graph_nodes), + ) + .route( + "/graphs/{graph_name}/connections", + web::get().to(graphs::connections::get_graph_connections), + ) + .route( + "/graphs/{graph_name}", + web::put().to(graphs::update::update_graph), + ) + .route("/manifest", web::put().to(manifest::dump::dump_manifest)) + .route( + "/manifest/check", + web::get().to(manifest::check::check_manifest), + ) + .route("/property", web::put().to(property::dump::dump_property)) + .route( + "/property/check", + web::get().to(property::check::check_property), + ) + .route( + "/messages/compatible", + web::post().to(messages::compatible::get_compatible_messages), + ) + .route( + "/file-content/{path}", + web::get().to(file_content::get_file_content), + ) + .route( + "/file-content/{path}", + web::put().to(file_content::save_file_content), + ) + .route("/base-dir", web::put().to(base_dir::set_base_dir)) + .route("/dir-list/{path}", web::get().to(dir_list::list_dir)) + .route("/ws/terminal", web::get().to(ws_terminal)), + ); + + if let Some(external_frontend_asset_path) = + &command_data.external_frontend_asset_path + { + cfg.service( + Files::new("/", external_frontend_asset_path) + .index_file("index.html") + .use_last_modified(true) + .use_etag(true), + ); + } else { + cfg.service( + web::scope("/") + .app_data(state.clone()) + .default_service(web::route().to(get_frontend_asset)), + ); + } }