Skip to content

Commit

Permalink
/-/tutor/start/
Browse files Browse the repository at this point in the history
  • Loading branch information
amitu committed Nov 5, 2023
1 parent 951122d commit 5fe48cb
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
1 change: 1 addition & 0 deletions fastn-core/src/commands/serve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
Expand Down
16 changes: 14 additions & 2 deletions fastn-core/src/tutor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ pub async fn main(package_name: String) -> fastn_core::Result<()> {
)
.await
}

pub async fn pwd() -> fastn_core::Result<fastn_core::http::Response> {
if !is_tutor() {
return Ok(fastn_core::not_found!("this only works in tutor mode"));
Expand All @@ -26,20 +27,31 @@ pub async fn js() -> fastn_core::Result<fastn_core::http::Response> {
Ok(actix_web::HttpResponse::Ok().body(include_bytes!("../tutor.js").to_vec()))
}

pub async fn start(t: Tutorial) -> fastn_core::Result<fastn_core::http::Response> {
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<fastn_core::http::Response> {
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")
}

static CURRENT_TUTORIAL: once_cell::sync::Lazy<async_lock::RwLock<Option<Tutorial>>> =
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,
}
Expand Down

0 comments on commit 5fe48cb

Please sign in to comment.