From 5fe48cb7aa00b7c084f09e2afd59dae356abf647 Mon Sep 17 00:00:00 2001 From: Amit Upadhyay Date: Sun, 5 Nov 2023 19:57:12 +0530 Subject: [PATCH] /-/tutor/start/ --- fastn-core/src/commands/serve.rs | 1 + fastn-core/src/tutor.rs | 16 ++++++++++++++-- 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/fastn-core/src/commands/serve.rs b/fastn-core/src/commands/serve.rs index 4e2a771453..41a67dc94a 100644 --- a/fastn-core/src/commands/serve.rs +++ b/fastn-core/src/commands/serve.rs @@ -648,6 +648,7 @@ async fn actual_route( ("get", "/test/") => test().await, ("get", "/-/pwd/") => fastn_core::tutor::pwd().await, ("get", "/-/tutor.js") => fastn_core::tutor::js().await, + ("post", "/-/tutor/start/") => fastn_core::tutor::start(req.json()?).await, ("get", "/-/tutor/stop/") => fastn_core::tutor::stop().await, (_, _) => serve(config, req).await, } diff --git a/fastn-core/src/tutor.rs b/fastn-core/src/tutor.rs index ebf8354e73..df71ee00c5 100644 --- a/fastn-core/src/tutor.rs +++ b/fastn-core/src/tutor.rs @@ -14,6 +14,7 @@ pub async fn main(package_name: String) -> fastn_core::Result<()> { ) .await } + pub async fn pwd() -> fastn_core::Result { if !is_tutor() { return Ok(fastn_core::not_found!("this only works in tutor mode")); @@ -26,12 +27,22 @@ pub async fn js() -> fastn_core::Result { Ok(actix_web::HttpResponse::Ok().body(include_bytes!("../tutor.js").to_vec())) } +pub async fn start(t: Tutorial) -> fastn_core::Result { + if !is_tutor() { + return Ok(fastn_core::not_found!("this only works in tutor mode")); + } + + println!("/-/start/ called"); + *CURRENT_TUTORIAL.write().await = Some(t); + fastn_core::http::api_ok("done") +} + pub async fn stop() -> fastn_core::Result { if !is_tutor() { return Ok(fastn_core::not_found!("this only works in tutor mode")); } - println!("/-/shutdown/ called, shutting down"); + println!("/-/stop/ called, shutting down"); *CURRENT_TUTORIAL.write().await = None; fastn_core::http::api_ok("done") } @@ -39,7 +50,8 @@ pub async fn stop() -> fastn_core::Result { static CURRENT_TUTORIAL: once_cell::sync::Lazy>> = once_cell::sync::Lazy::new(|| async_lock::RwLock::new(None)); -struct Tutorial { +#[derive(serde::Deserialize)] +pub struct Tutorial { path: String, data: fastn_core::commands::serve::AppData, }