Skip to content

Commit

Permalink
feat: add get base_dir support in tman designer (#540)
Browse files Browse the repository at this point in the history
  • Loading branch information
halajohn authored Jan 10, 2025
1 parent c5654c3 commit e73dcfb
Show file tree
Hide file tree
Showing 7 changed files with 489 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import type {
IFileContentResponse,
ISetBaseDirResponse,
IBaseDirResponse,
IGetBaseDirResponse,
} from "@/types/fileSystem";

export const ENDPOINT_FILE_SYSTEM = {
Expand Down Expand Up @@ -49,6 +50,15 @@ export const ENDPOINT_FILE_SYSTEM = {
})
),
},
[ENDPOINT_METHOD.GET]: {
url: `${API_DESIGNER_V1}/base-dir`,
method: ENDPOINT_METHOD.GET,
responseSchema: genResSchema<IGetBaseDirResponse>(
z.object({
base_dir: z.string().nullable(),
})
),
},
},
dirList: {
[ENDPOINT_METHOD.GET]: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
} from "@/api/services/utils";
import { ENDPOINT_FILE_SYSTEM } from "@/api/endpoints";
import { ENDPOINT_METHOD } from "@/api/endpoints/constant";
import { IGetBaseDirResponse, ISetBaseDirResponse } from "@/types/fileSystem";

// request functions -------------------------------

Expand Down Expand Up @@ -44,7 +45,9 @@ export const putFileContent = async (
return res;
};

export const putBaseDir = async (baseDir: string) => {
export const putBaseDir = async (
baseDir: string
): Promise<ISetBaseDirResponse> => {
const template = ENDPOINT_FILE_SYSTEM.baseDir[ENDPOINT_METHOD.PUT];
const req = makeAPIRequest(template, {
body: { base_dir: baseDir },
Expand All @@ -53,6 +56,13 @@ export const putBaseDir = async (baseDir: string) => {
return template.responseSchema.parse(res).data;
};

export const getBaseDir = async (): Promise<IGetBaseDirResponse> => {
const template = ENDPOINT_FILE_SYSTEM.baseDir[ENDPOINT_METHOD.GET];
const req = makeAPIRequest(template, {});
const res = await req;
return template.responseSchema.parse(res).data;
};

export const getDirList = async (path: string) => {
const encodedPath = encodeURIComponent(path);
const template = ENDPOINT_FILE_SYSTEM.dirList[ENDPOINT_METHOD.GET];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,7 @@ export type TBaseDirEntry = {
export interface IBaseDirResponse {
entries: TBaseDirEntry[];
}

export interface IGetBaseDirResponse {
base_dir: string | null;
}
82 changes: 82 additions & 0 deletions core/src/ten_manager/src/designer/base_dir/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ pub struct SetBaseDirResponse {
pub success: bool,
}

#[derive(Serialize, Deserialize, Debug, PartialEq)]
pub struct GetBaseDirResponse {
pub base_dir: Option<String>,
}

pub async fn set_base_dir(
req: web::Json<SetBaseDirRequest>,
state: web::Data<Arc<RwLock<DesignerState>>>,
Expand Down Expand Up @@ -60,6 +65,20 @@ pub async fn set_base_dir(
}
}

pub async fn get_base_dir(
state: web::Data<Arc<RwLock<DesignerState>>>,
) -> impl Responder {
let state = state.read().unwrap();
let response = ApiResponse {
status: Status::Ok,
data: GetBaseDirResponse {
base_dir: state.base_dir.clone(),
},
meta: None,
};
HttpResponse::Ok().json(response)
}

#[cfg(test)]
mod tests {
use actix_web::{test, App};
Expand Down Expand Up @@ -101,4 +120,67 @@ mod tests {

assert!(resp.is_err());
}

#[actix_web::test]
async fn test_get_base_dir_some() {
let designer_state = DesignerState {
base_dir: Some("/initial/path".to_string()),
all_pkgs: Some(vec![]),
tman_config: TmanConfig::default(),
};
let designer_state = Arc::new(RwLock::new(designer_state));

let app = test::init_service(
App::new()
.app_data(web::Data::new(designer_state.clone()))
.route(
"/api/designer/v1/base-dir",
web::get().to(get_base_dir),
),
)
.await;

let req = test::TestRequest::get()
.uri("/api/designer/v1/base-dir")
.to_request();
let resp: ApiResponse<GetBaseDirResponse> =
test::call_and_read_body_json(&app, req).await;

assert_eq!(resp.status, Status::Ok);
assert_eq!(
resp.data,
GetBaseDirResponse {
base_dir: Some("/initial/path".to_string())
}
);
}

#[actix_web::test]
async fn test_get_base_dir_none() {
let designer_state = DesignerState {
base_dir: None,
all_pkgs: Some(vec![]),
tman_config: TmanConfig::default(),
};
let designer_state = Arc::new(RwLock::new(designer_state));

let app = test::init_service(
App::new()
.app_data(web::Data::new(designer_state.clone()))
.route(
"/api/designer/v1/base-dir",
web::get().to(get_base_dir),
),
)
.await;

let req = test::TestRequest::get()
.uri("/api/designer/v1/base-dir")
.to_request();
let resp: ApiResponse<GetBaseDirResponse> =
test::call_and_read_body_json(&app, req).await;

assert_eq!(resp.status, Status::Ok);
assert_eq!(resp.data, GetBaseDirResponse { base_dir: None });
}
}
9 changes: 5 additions & 4 deletions core/src/ten_manager/src/designer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ mod mock;
mod packages;
mod property;
pub mod response;
mod run_app;
mod terminal;
mod version;

Expand All @@ -28,8 +29,6 @@ use actix_web::web;
use ten_rust::pkg_info::PkgInfo;

use super::config::TmanConfig;
use terminal::ws_terminal;
use version::get_version;

pub struct DesignerState {
pub base_dir: Option<String>,
Expand All @@ -44,7 +43,7 @@ pub fn configure_routes(
cfg.service(
web::scope("/api/designer/v1")
.app_data(state.clone())
.route("/version", web::get().to(get_version))
.route("/version", web::get().to(version::get_version))
.route(
"/addons/extensions",
web::get().to(addons::extensions::get_extension_addons),
Expand Down Expand Up @@ -93,7 +92,9 @@ pub fn configure_routes(
web::put().to(file_content::save_file_content),
)
.route("/base-dir", web::put().to(base_dir::set_base_dir))
.route("/base-dir", web::get().to(base_dir::get_base_dir))
.route("/dir-list/{path}", web::get().to(dir_list::list_dir))
.route("/ws/terminal", web::get().to(ws_terminal)),
.route("/ws/run-app", web::get().to(run_app::run_app))
.route("/ws/terminal", web::get().to(terminal::ws_terminal)),
);
}
Loading

0 comments on commit e73dcfb

Please sign in to comment.