From b39209d404f9c34dde81ef892e5ea96d3467b9f0 Mon Sep 17 00:00:00 2001 From: Amit Upadhyay Date: Fri, 27 Oct 2023 21:57:32 +0530 Subject: [PATCH 01/43] attaching tutor.js --- fastn-core/src/commands/serve.rs | 1 + fastn-core/src/tutor.rs | 10 ++++++++++ fastn-core/src/utils.rs | 10 ++++++++++ fastn-core/tutor.js | 19 +++++++++++++++++++ fastn/src/main.rs | 2 +- ftd/ftd-js.html | 2 ++ 6 files changed, 43 insertions(+), 1 deletion(-) create mode 100644 fastn-core/tutor.js diff --git a/fastn-core/src/commands/serve.rs b/fastn-core/src/commands/serve.rs index 0bbf40fc35..5b1f966f29 100644 --- a/fastn-core/src/commands/serve.rs +++ b/fastn-core/src/commands/serve.rs @@ -657,6 +657,7 @@ async fn route( ("get", "/favicon.ico") => favicon().await, ("get", "/test/") => test().await, ("get", "/-/pwd/") => fastn_core::tutor::pwd().await, + ("get", "/-/tutor.js") => fastn_core::tutor::js().await, ("get", "/-/shutdown/") => fastn_core::tutor::shutdown().await, (_, _) => { serve( diff --git a/fastn-core/src/tutor.rs b/fastn-core/src/tutor.rs index 65c3229a01..7fffe1ab06 100644 --- a/fastn-core/src/tutor.rs +++ b/fastn-core/src/tutor.rs @@ -6,6 +6,10 @@ pub async fn pwd() -> fastn_core::Result { fastn_core::http::api_ok(std::env::current_dir()?.to_string_lossy()) } +pub async fn js() -> fastn_core::Result { + Ok(actix_web::HttpResponse::Ok().body(include_bytes!("../tutor.js").to_vec())) +} + pub async fn shutdown() -> fastn_core::Result { if !is_tutor() { return Ok(fastn_core::not_found!("this only works in tutor mode")); @@ -148,7 +152,10 @@ struct Tutorial { url: String, title: String, done: bool, + /// current means this is the tutorial the user is currently working on current: bool, + /// active means this is the page the user is seeing right now + active: bool, } impl Tutorial { @@ -165,6 +172,7 @@ impl Tutorial { current: state.current == id, url: format!("/{id}/"), id, + active: false, }) } } @@ -194,6 +202,7 @@ mod test { title: "Install and start using `fastn`".to_string(), done: false, current: false, + active: false, }], }, super::Workshop { @@ -207,6 +216,7 @@ mod test { title: "Install and start using `fastn`".to_string(), done: false, current: false, + active: false, }], }, ], diff --git a/fastn-core/src/utils.rs b/fastn-core/src/utils.rs index 2a237d35dc..e47792bb17 100644 --- a/fastn-core/src/utils.rs +++ b/fastn-core/src/utils.rs @@ -793,6 +793,16 @@ pub fn replace_markers_2023( ) .as_str(), ) + .replace( + "__extra_js__", + get_extra_js( + config.ftd_external_js.as_slice(), + config.ftd_inline_js.as_slice(), + "", + "", + ) + .as_str(), + ) .replace("__default_css__", default_css) .replace("__base_url__", base_url) .as_str(), diff --git a/fastn-core/tutor.js b/fastn-core/tutor.js new file mode 100644 index 0000000000..e6134faecb --- /dev/null +++ b/fastn-core/tutor.js @@ -0,0 +1,19 @@ +(function() { + document.addEventListener("DOMContentLoaded", function() { + console.log("onload 1"); + + let iframe = document.createElement('iframe'); + // iframe.src = "/-/tutor/"; + iframe.src = "https://fastn.com/"; + iframe.style.position = "fixed"; + iframe.style.top = "0"; + iframe.style.left = "0"; + iframe.style.width = "100px"; + iframe.style.height = "100vh"; + + document.body.style.paddingLeft = "100px"; + document.body.insertBefore(iframe, document.body.firstChild); + console.log("onload"); + }); + console.log("registered"); +})(); \ No newline at end of file diff --git a/fastn/src/main.rs b/fastn/src/main.rs index ffc73481d8..32eabbda00 100644 --- a/fastn/src/main.rs +++ b/fastn/src/main.rs @@ -94,7 +94,7 @@ async fn fastn_core_commands(matches: &clap::ArgMatches) -> fastn_core::Result<( Some(2000), None, Some("2023".to_string()), - vec![], + vec!["/-/tutor.js".to_string()], vec![], vec![], vec![], diff --git a/ftd/ftd-js.html b/ftd/ftd-js.html index 8113699d14..62934ad4c0 100644 --- a/ftd/ftd-js.html +++ b/ftd/ftd-js.html @@ -1,3 +1,4 @@ + @@ -10,6 +11,7 @@ __script_file__ + __extra_js__ + + +
Mobile text
+ + From 14f53c2427e59dc48799518e4ce779e9a1f09de6 Mon Sep 17 00:00:00 2001 From: Amit Upadhyay Date: Fri, 3 Nov 2023 19:24:16 +0530 Subject: [PATCH 21/43] implementing Default --- fastn-core/src/http.rs | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/fastn-core/src/http.rs b/fastn-core/src/http.rs index 5364bfeb79..40af4beb04 100644 --- a/fastn-core/src/http.rs +++ b/fastn-core/src/http.rs @@ -73,7 +73,7 @@ pub fn ok_with_content_type( .body(data) } -#[derive(Debug, Clone)] +#[derive(Debug, Clone, Default)] pub struct Request { method: String, uri: String, @@ -90,12 +90,6 @@ pub struct Request { // path_params: Vec<(String, )> } -impl Default for Request { - fn default() -> Self { - todo!() - } -} - impl Request { //pub fn get_named_params() -> {} pub fn from_actix(req: actix_web::HttpRequest, body: actix_web::web::Bytes) -> Self { From 53050006277cc223080b7686504fc3cc71f215db Mon Sep 17 00:00:00 2001 From: Amit Upadhyay Date: Fri, 3 Nov 2023 19:47:37 +0530 Subject: [PATCH 22/43] removed extra print so fbt test passes --- fastn-core/src/library2022/mod.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/fastn-core/src/library2022/mod.rs b/fastn-core/src/library2022/mod.rs index 162a72abfb..fdcc74a302 100644 --- a/fastn-core/src/library2022/mod.rs +++ b/fastn-core/src/library2022/mod.rs @@ -207,7 +207,6 @@ impl Library2022 { processor: String, doc: &'a mut ftd::interpreter::TDoc<'a>, ) -> ftd::interpreter::Result { - println!("process: {}", &processor); tracing::info!( msg = "stuck-on-processor", doc = doc.name, From 38d07fece325dfab3f2cffdec6075852c01ceab4 Mon Sep 17 00:00:00 2001 From: Amit Upadhyay Date: Sat, 4 Nov 2023 11:43:01 +0530 Subject: [PATCH 23/43] test fixes and removed some un-needed lifetimes --- fastn-core/src/commands/serve.rs | 8 ++++---- fastn-core/src/package/package_doc.rs | 16 ++++++++-------- ftd/ftd-js.html | 3 +-- ftd/src/ftd2021/p1/parser.rs | 4 +++- ftd/src/js/ftd_test_helpers.rs | 1 + 5 files changed, 17 insertions(+), 15 deletions(-) diff --git a/fastn-core/src/commands/serve.rs b/fastn-core/src/commands/serve.rs index 8dc4aeb36a..be55481be9 100644 --- a/fastn-core/src/commands/serve.rs +++ b/fastn-core/src/commands/serve.rs @@ -18,8 +18,8 @@ fn handle_redirect( /// path: // /// #[tracing::instrument(skip_all)] -async fn serve_file<'m>( - config: &'m mut fastn_core::RequestConfig, +async fn serve_file( + config: &mut fastn_core::RequestConfig, path: &camino::Utf8Path, ) -> fastn_core::http::Response { if let Some(r) = handle_redirect(&config.config, path) { @@ -123,8 +123,8 @@ async fn serve_file<'m>( } } -async fn serve_cr_file<'m>( - req_config: &'m mut fastn_core::RequestConfig, +async fn serve_cr_file( + req_config: &mut fastn_core::RequestConfig, path: &camino::Utf8Path, cr_number: usize, ) -> fastn_core::http::Response { diff --git a/fastn-core/src/package/package_doc.rs b/fastn-core/src/package/package_doc.rs index 38d054ccaa..89996d6319 100644 --- a/fastn-core/src/package/package_doc.rs +++ b/fastn-core/src/package/package_doc.rs @@ -363,8 +363,8 @@ impl From for fastn_core::http::Response { } #[tracing::instrument(skip_all)] -pub(crate) async fn read_ftd<'m>( - config: &'m mut fastn_core::RequestConfig, +pub(crate) async fn read_ftd( + config: &mut fastn_core::RequestConfig, main: &fastn_core::Document, base_url: &str, download_assets: bool, @@ -385,8 +385,8 @@ pub(crate) async fn read_ftd<'m>( } #[tracing::instrument(name = "read_ftd_2022", skip_all)] -pub(crate) async fn read_ftd_2022<'m>( - config: &'m mut fastn_core::RequestConfig, +pub(crate) async fn read_ftd_2022( + config: &mut fastn_core::RequestConfig, main: &fastn_core::Document, base_url: &str, download_assets: bool, @@ -454,8 +454,8 @@ pub(crate) async fn read_ftd_2022<'m>( #[allow(clippy::await_holding_refcell_ref)] #[tracing::instrument(name = "read_ftd_2023", skip_all)] -pub(crate) async fn read_ftd_2023<'m>( - config: &'m mut fastn_core::RequestConfig, +pub(crate) async fn read_ftd_2023( + config: &mut fastn_core::RequestConfig, main: &fastn_core::Document, base_url: &str, download_assets: bool, @@ -529,8 +529,8 @@ pub(crate) async fn read_ftd_2023<'m>( Ok(FTDResult::Html(file_content.into())) } -pub(crate) async fn process_ftd<'m>( - config: &'m mut fastn_core::RequestConfig, +pub(crate) async fn process_ftd( + config: &mut fastn_core::RequestConfig, main: &fastn_core::Document, base_url: &str, build_static_files: bool, diff --git a/ftd/ftd-js.html b/ftd/ftd-js.html index 62934ad4c0..d9cad7d961 100644 --- a/ftd/ftd-js.html +++ b/ftd/ftd-js.html @@ -10,8 +10,7 @@ __fastn_package__ - __script_file__ - __extra_js__ + __script_file____extra_js__ -
undefined Tuesday
undefined Wednesday
undefined Thursday
undefined Friday
undefined Saturday
@@ -33,10 +33,10 @@ return (fastn_utils.getter(index) >= 2); }, function (root) { let rooti0 = fastn_dom.createKernel(root, fastn_dom.ElementKind.Text); - rooti0.setProperty(fastn_dom.PropertyKind.StringValue, fastn.formula([global.foo__key, + rooti0.setProperty(fastn_dom.PropertyKind.StringValue, fastn.formula([index, item], function () { return foo__join({ - a: global.foo__key, + a: index, b: item, }, rooti0); }), inherited); diff --git a/ftd/t/js/32-ftd-len.html b/ftd/t/js/32-ftd-len.html index 0c377f2531..fbb2611ba3 100644 --- a/ftd/t/js/32-ftd-len.html +++ b/ftd/t/js/32-ftd-len.html @@ -68,8 +68,8 @@ fastn_utils.getter(global.foo__months).forLoop(root, function (root, item, index) { let rooti0 = fastn_dom.createKernel(root, fastn_dom.ElementKind.Text); rooti0.setProperty(fastn_dom.PropertyKind.StringValue, item, inherited); - rooti0.setProperty(fastn_dom.PropertyKind.Color, fastn.formula([global.foo__index, - global.foo__index], function () { + rooti0.setProperty(fastn_dom.PropertyKind.Color, fastn.formula([index, + index], function () { if (function () { return (fastn_utils.getter(index) % 3 == 1); }()) { From c7cb58283257f25c178d164f5c4657f9e3b78d54 Mon Sep 17 00:00:00 2001 From: Arpita-Jaiswal Date: Tue, 7 Nov 2023 16:36:55 +0530 Subject: [PATCH 36/43] Fix fonts for fastn build --- fastn-core/src/commands/build.rs | 40 +++++++++++++++++++++----------- 1 file changed, 26 insertions(+), 14 deletions(-) diff --git a/fastn-core/src/commands/build.rs b/fastn-core/src/commands/build.rs index dc5b380fc7..237f98a393 100644 --- a/fastn-core/src/commands/build.rs +++ b/fastn-core/src/commands/build.rs @@ -593,20 +593,32 @@ async fn handle_file_( return Ok(()); } - let req = fastn_core::http::Request::default(); - let mut req_config = - fastn_core::RequestConfig::new(config, &req, doc.id.as_str(), base_url); - req_config.current_document = Some(document.get_id().to_string()); - - let resp = fastn_core::package::package_doc::process_ftd( - &mut req_config, - doc, - base_url, - build_static_files, - test, - file_path.as_str(), - ) - .await; + let resp = { + let req = fastn_core::http::Request::default(); + let mut req_config = + fastn_core::RequestConfig::new(config, &req, doc.id.as_str(), base_url); + req_config.current_document = Some(document.get_id().to_string()); + + let resp = fastn_core::package::package_doc::process_ftd( + &mut req_config, + doc, + base_url, + build_static_files, + test, + file_path.as_str(), + ) + .await; + + config.all_packages.borrow_mut().extend( + req_config + .config + .all_packages + .borrow() + .iter() + .map(|(k, v)| (k.clone(), v.clone())), + ); + resp + }; match (resp, ignore_failed) { (Ok(r), _) => { From 3ce144d06b8d0d52eb3d8ab2e838e0caeb662c90 Mon Sep 17 00:00:00 2001 From: Amit Upadhyay Date: Tue, 7 Nov 2023 17:53:57 +0530 Subject: [PATCH 37/43] server error should show a message --- Cargo.lock | 103 +-------------------- Cargo.toml | 2 + fastn-core/Cargo.toml | 3 +- fastn-core/src/catch_panic.rs | 151 +++++++++++++++++++++++++++++++ fastn-core/src/commands/serve.rs | 2 +- fastn-core/src/lib.rs | 1 + fastn-core/src/tutor.rs | 4 +- 7 files changed, 161 insertions(+), 105 deletions(-) create mode 100644 fastn-core/src/catch_panic.rs diff --git a/Cargo.lock b/Cargo.lock index 02a4720afb..0bcc593a3b 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -187,54 +187,6 @@ dependencies = [ "syn 2.0.38", ] -[[package]] -name = "actix-web-lab" -version = "0.19.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f9b7c50a90657ef1868db9dd85f74e82c4d9858ce583d4639ae3583f0bb97775" -dependencies = [ - "actix-http", - "actix-router", - "actix-service", - "actix-utils", - "actix-web", - "actix-web-lab-derive", - "ahash 0.8.6", - "arc-swap", - "async-trait", - "bytes", - "bytestring", - "csv", - "derive_more", - "futures-core", - "futures-util", - "http", - "impl-more", - "itertools 0.11.0", - "local-channel", - "mediatype", - "mime", - "once_cell", - "pin-project-lite", - "regex", - "serde", - "serde_html_form", - "serde_json", - "tokio", - "tracing", -] - -[[package]] -name = "actix-web-lab-derive" -version = "0.19.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "16294584c7794939b1e5711f28e7cae84ef30e62a520db3f9af425f85269bcd2" -dependencies = [ - "proc-macro2", - "quote", - "syn 1.0.109", -] - [[package]] name = "addr2line" version = "0.21.0" @@ -417,12 +369,6 @@ version = "1.0.0-beta.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8f1f8f5a6f3d50d89e3797d7593a50f96bb2aaa20ca0cc7be1fb673232c91d72" -[[package]] -name = "arc-swap" -version = "1.6.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "bddcadddf5e9015d310179a59bb28c4d4b9920ad0f11e8e14dbadf654890c9a6" - [[package]] name = "arrayvec" version = "0.5.2" @@ -1095,27 +1041,6 @@ dependencies = [ "syn 2.0.38", ] -[[package]] -name = "csv" -version = "1.3.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ac574ff4d437a7b5ad237ef331c17ccca63c46479e5b5453eb8e10bb99a759fe" -dependencies = [ - "csv-core", - "itoa", - "ryu", - "serde", -] - -[[package]] -name = "csv-core" -version = "0.1.11" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5efa2b3d7902f4b634a20cae3c9c4e6209dc4779feb6863329607560143efa70" -dependencies = [ - "memchr", -] - [[package]] name = "darling" version = "0.14.4" @@ -1670,7 +1595,6 @@ name = "fastn-core" version = "0.1.0" dependencies = [ "actix-web", - "actix-web-lab", "antidote", "async-lock", "async-recursion", @@ -1689,6 +1613,8 @@ dependencies = [ "fluent", "ftd 0.3.0", "futures", + "futures-core", + "futures-util", "hyper", "ignore", "indoc 2.0.4", @@ -2375,12 +2301,6 @@ dependencies = [ "winapi-util", ] -[[package]] -name = "impl-more" -version = "0.1.6" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "206ca75c9c03ba3d4ace2460e57b189f39f43de612c2f85836e65c929701bb2d" - [[package]] name = "include_dir" version = "0.7.3" @@ -2764,12 +2684,6 @@ dependencies = [ "digest 0.10.7", ] -[[package]] -name = "mediatype" -version = "0.19.15" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8c408dc227d302f1496c84d9dc68c00fec6f56f9228a18f3023f976f3ca7c945" - [[package]] name = "memchr" version = "2.6.4" @@ -4053,19 +3967,6 @@ dependencies = [ "syn 2.0.38", ] -[[package]] -name = "serde_html_form" -version = "0.2.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "cde65b75f2603066b78d6fa239b2c07b43e06ead09435f60554d3912962b4a3c" -dependencies = [ - "form_urlencoded", - "indexmap 2.1.0", - "itoa", - "ryu", - "serde", -] - [[package]] name = "serde_json" version = "1.0.108" diff --git a/Cargo.toml b/Cargo.toml index ff2b38b589..77f9159d31 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -90,6 +90,8 @@ format_num = "0.1" ftd = { path = "ftd" } fastn-js = { path = "fastn-js" } futures = "0.3" +futures-util = { version = "0.3", default-features = false, features = ["std"] } +futures-core = "0.3" home = "0.5" ignore = "0.4" include_dir = "0.7" diff --git a/fastn-core/Cargo.toml b/fastn-core/Cargo.toml index 509bf5e86b..c60572d680 100644 --- a/fastn-core/Cargo.toml +++ b/fastn-core/Cargo.toml @@ -31,7 +31,6 @@ github-auth = ["dep:oauth2"] [dependencies] actix-web.workspace = true -actix-web-lab.workspace = true antidote.workspace = true async-lock.workspace = true dirs.workspace = true @@ -41,6 +40,8 @@ clap.workspace = true colored.workspace = true native-tls.workspace = true deadpool-postgres.workspace = true +futures-util.workspace = true +futures-core.workspace = true postgres-types.workspace = true postgres-native-tls.workspace = true tokio-postgres.workspace = true diff --git a/fastn-core/src/catch_panic.rs b/fastn-core/src/catch_panic.rs new file mode 100644 index 0000000000..21985d23f6 --- /dev/null +++ b/fastn-core/src/catch_panic.rs @@ -0,0 +1,151 @@ +// borrowed from https://github.com/robjtede/actix-web-lab/ (MIT) +use std::{ + future::{ready, Ready}, + panic::AssertUnwindSafe, + rc::Rc, +}; + +use actix_web::{ + dev::{forward_ready, Service, ServiceRequest, ServiceResponse, Transform}, + error, +}; +use futures_core::future::LocalBoxFuture; +use futures_util::FutureExt as _; + +/// A middleware to catch panics in wrapped handlers and middleware, returning empty 500 responses. +/// +/// **This middleware should never be used as replacement for proper error handling.** See [this +/// thread](https://github.com/actix/actix-web/issues/1501#issuecomment-627517783) for historical +/// discussion on why Actix Web does not do this by default. +/// +/// It is recommended that this middleware be registered last. That is, `wrap`ed after everything +/// else except `Logger`. +/// +/// # Examples +/// +/// ``` +/// # use actix_web::App; +/// use actix_web_lab::middleware::CatchPanic; +/// +/// App::new().wrap(CatchPanic::default()) +/// # ; +/// ``` +/// +/// ```no_run +/// # use actix_web::App; +/// use actix_web::middleware::{Logger, NormalizePath}; +/// use actix_web_lab::middleware::CatchPanic; +/// +/// // recommended wrap order +/// App::new() +/// .wrap(NormalizePath::default()) +/// .wrap(CatchPanic::default()) // <- after everything except logger +/// .wrap(Logger::default()) +/// # ; +/// ``` +#[derive(Debug, Clone, Default)] +#[non_exhaustive] +pub struct CatchPanic; + +impl Transform for CatchPanic +where + S: Service, Error = actix_web::Error> + 'static, +{ + type Response = ServiceResponse; + type Error = actix_web::Error; + type Transform = CatchPanicMiddleware; + type InitError = (); + type Future = Ready>; + + fn new_transform(&self, service: S) -> Self::Future { + ready(Ok(CatchPanicMiddleware { + service: Rc::new(service), + })) + } +} + +pub struct CatchPanicMiddleware { + service: Rc, +} + +impl Service for CatchPanicMiddleware +where + S: Service, Error = actix_web::Error> + 'static, +{ + type Response = ServiceResponse; + type Error = actix_web::Error; + type Future = LocalBoxFuture<'static, Result>; + + forward_ready!(service); + + fn call(&self, req: ServiceRequest) -> Self::Future { + AssertUnwindSafe(self.service.call(req)) + .catch_unwind() + .map(move |res| match res { + Ok(Ok(res)) => Ok(res), + Ok(Err(svc_err)) => Err(svc_err), + Err(_panic_err) => Err(error::ErrorInternalServerError("500 Server Error")), + }) + .boxed_local() + } +} + +#[cfg(test)] +mod tests { + use actix_web::{ + body::{to_bytes, MessageBody}, + dev::{Service as _, ServiceFactory}, + http::StatusCode, + test, web, App, Error, + }; + + use super::*; + + fn test_app() -> App< + impl ServiceFactory< + ServiceRequest, + Response = ServiceResponse, + Config = (), + InitError = (), + Error = Error, + >, + > { + App::new() + .wrap(CatchPanic::default()) + .route("/", web::get().to(|| async { "content" })) + .route( + "/disco", + #[allow(unreachable_code)] + web::get().to(|| async { + panic!("the disco"); + "" + }), + ) + } + + #[actix_web::test] + async fn pass_through_no_panic() { + let app = test::init_service(test_app()).await; + + let req = test::TestRequest::default().to_request(); + let res = test::call_service(&app, req).await; + assert_eq!(res.status(), StatusCode::OK); + let body = test::read_body(res).await; + assert_eq!(body, "content"); + } + + #[actix_web::test] + async fn catch_panic_return_internal_server_error_response() { + let app = test::init_service(test_app()).await; + + let req = test::TestRequest::with_uri("/disco").to_request(); + let err = match app.call(req).await { + Ok(_) => panic!("unexpected Ok response"), + Err(err) => err, + }; + let res = err.error_response(); + assert_eq!(res.status(), StatusCode::INTERNAL_SERVER_ERROR); + let body = to_bytes(res.into_body()).await.unwrap(); + assert!(body.is_empty()); + } +} diff --git a/fastn-core/src/commands/serve.rs b/fastn-core/src/commands/serve.rs index 41a67dc94a..b3cac66e35 100644 --- a/fastn-core/src/commands/serve.rs +++ b/fastn-core/src/commands/serve.rs @@ -728,7 +728,7 @@ You can try without providing port, it will automatically pick unused port."#, inline_css: inline_css.clone(), package_name: package_name.clone(), })) - .wrap(actix_web_lab::middleware::CatchPanic::default()) + .wrap(fastn_core::catch_panic::CatchPanic::default()) .wrap( actix_web::middleware::Logger::new( r#""%r" %Ts %s %b %a "%{Referer}i" "%{User-Agent}i""#, diff --git a/fastn-core/src/lib.rs b/fastn-core/src/lib.rs index 91360f3e27..51bdd263bc 100644 --- a/fastn-core/src/lib.rs +++ b/fastn-core/src/lib.rs @@ -32,6 +32,7 @@ mod tracker; mod translation; mod version; // mod wasm; +pub(crate) mod catch_panic; mod library2022; mod workspace; diff --git a/fastn-core/src/tutor.rs b/fastn-core/src/tutor.rs index d4b5cd635c..2501cf07aa 100644 --- a/fastn-core/src/tutor.rs +++ b/fastn-core/src/tutor.rs @@ -101,7 +101,7 @@ pub async fn process( }; let state = TutorState { - fs_state, + done: fs_state.done, current: CURRENT_TUTORIAL.read().await.as_ref().map(|t| t.id.clone()), }; @@ -110,7 +110,7 @@ pub async fn process( #[derive(Debug, Default, serde::Serialize)] struct TutorState { - fs_state: TutorStateFS, + done: Vec, current: Option, } From d35198a63f0dbed1b7d9fe413f1aef6bda33b399 Mon Sep 17 00:00:00 2001 From: Amit Upadhyay Date: Tue, 7 Nov 2023 18:46:44 +0530 Subject: [PATCH 38/43] removing controller stuff and .d files from release (#1461) Signed-off-by: Amit Upadhyay --- .github/workflows/release.yml | 20 ++------------------ 1 file changed, 2 insertions(+), 18 deletions(-) diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index b1af5035d6..17843fd0a4 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -36,16 +36,7 @@ jobs: - uses: actions/upload-artifact@v2 with: name: linux_musl_x86_64 - path: target/x86_64-unknown-linux-musl/release/${{ env.BINARY_NAME }}* - - name: Build-musl - uses: gmiam/rust-musl-action@9e6a37bf27ecfffb6b92240ea276bea5487fa15d - continue-on-error: false - with: - args: cargo build --target $BUILD_TARGET --release --features controller - - uses: actions/upload-artifact@v2 - with: - name: fastn_controller_linux_musl_x86_64 - path: target/x86_64-unknown-linux-musl/release/${{ env.BINARY_NAME }}* + path: target/x86_64-unknown-linux-musl/release/${{ env.BINARY_NAME }} release-windows: name: Build for Windows runs-on: windows-latest @@ -131,10 +122,6 @@ jobs: with: name: linux_musl_x86_64 path: ~/download/linux_musl - - uses: actions/download-artifact@v2 - with: - name: fastn_controller_linux_musl_x86_64 - path: ~/download/fastn_controller_linux_musl - uses: actions/download-artifact@v2 with: name: windows_x64_latest @@ -149,16 +136,13 @@ jobs: mv ~/download/windows/windows_x64_installer.exe ~/download/windows/fastn_setup.exe mv ~/download/macos/fastn ~/download/macos/fastn_macos_x86_64 mv ~/download/linux_musl/fastn ~/download/linux_musl/fastn_linux_musl_x86_64 - mv ~/download/linux_musl/fastn.d ~/download/linux_musl/fastn_linux_musl_x86_64.d - mv ~/download/fastn_controller_linux_musl/fastn ~/download/fastn_controller_linux_musl/fastn_controller_linux_musl_x86_64 - mv ~/download/fastn_controller_linux_musl/fastn.d ~/download/fastn_controller_linux_musl/fastn_controller_linux_musl_x86_64.d - name: Update .github/RELEASE_TEMPLATE.md run: | sed -i "s/GITHUB_SHA/${GITHUB_SHA}/g" .github/RELEASE_TEMPLATE.md sed -i "s/DATE/$(date)/g" .github/RELEASE_TEMPLATE.md - uses: ncipollo/release-action@v1 with: - artifacts: "~/download/windows/fastn_windows_x86_64.exe,~/download/windows/fastn_setup.exe,~/download/macos/fastn_macos_x86_64,~/download/linux_musl/fastn_linux_musl_x86_64,~/download/linux_musl/fastn_linux_musl_x86_64.d,~/download/fastn_controller_linux_musl/fastn_controller_linux_musl_x86_64,~/download/fastn_controller_linux_musl/fastn_controller_linux_musl_x86_64.d" + artifacts: "~/download/windows/fastn_windows_x86_64.exe,~/download/windows/fastn_setup.exe,~/download/macos/fastn_macos_x86_64,~/download/linux_musl/fastn_linux_musl_x86_64" generateReleaseNotes: true token: ${{ secrets.GITHUB_TOKEN }} tag: ${{ github.event.inputs.releaseTag }} From 12ad954bac67999d9df3b88cce9e9d10f8336a1d Mon Sep 17 00:00:00 2001 From: Arpita-Jaiswal Date: Tue, 7 Nov 2023 20:46:10 +0530 Subject: [PATCH 39/43] Fix catch_panic_return_internal_server_error_response test --- fastn-core/src/catch_panic.rs | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/fastn-core/src/catch_panic.rs b/fastn-core/src/catch_panic.rs index 21985d23f6..e5fd83e6c3 100644 --- a/fastn-core/src/catch_panic.rs +++ b/fastn-core/src/catch_panic.rs @@ -84,12 +84,16 @@ where .map(move |res| match res { Ok(Ok(res)) => Ok(res), Ok(Err(svc_err)) => Err(svc_err), - Err(_panic_err) => Err(error::ErrorInternalServerError("500 Server Error")), + Err(_panic_err) => Err(error::ErrorInternalServerError( + INTERNAL_SERVER_ERROR_MESSAGE, + )), }) .boxed_local() } } +const INTERNAL_SERVER_ERROR_MESSAGE: &str = "500 Server Error"; + #[cfg(test)] mod tests { use actix_web::{ @@ -146,6 +150,6 @@ mod tests { let res = err.error_response(); assert_eq!(res.status(), StatusCode::INTERNAL_SERVER_ERROR); let body = to_bytes(res.into_body()).await.unwrap(); - assert!(body.is_empty()); + assert_eq!(body, INTERNAL_SERVER_ERROR_MESSAGE) } } From 891c5f5774823a292187788740d56e5cd786b014 Mon Sep 17 00:00:00 2001 From: Arpita-Jaiswal Date: Tue, 7 Nov 2023 21:11:57 +0530 Subject: [PATCH 40/43] Added no_run for doc test --- fastn-core/src/catch_panic.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/fastn-core/src/catch_panic.rs b/fastn-core/src/catch_panic.rs index e5fd83e6c3..f04d3b3594 100644 --- a/fastn-core/src/catch_panic.rs +++ b/fastn-core/src/catch_panic.rs @@ -23,7 +23,7 @@ use futures_util::FutureExt as _; /// /// # Examples /// -/// ``` +/// ```no_run /// # use actix_web::App; /// use actix_web_lab::middleware::CatchPanic; /// From cde85e836a88ef73062daf2ccc8a306123ca0753 Mon Sep 17 00:00:00 2001 From: Arpita-Jaiswal Date: Tue, 7 Nov 2023 21:12:49 +0530 Subject: [PATCH 41/43] Added ignore for doc test --- fastn-core/src/catch_panic.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/fastn-core/src/catch_panic.rs b/fastn-core/src/catch_panic.rs index f04d3b3594..b1c175e142 100644 --- a/fastn-core/src/catch_panic.rs +++ b/fastn-core/src/catch_panic.rs @@ -23,7 +23,7 @@ use futures_util::FutureExt as _; /// /// # Examples /// -/// ```no_run +/// ```ignore /// # use actix_web::App; /// use actix_web_lab::middleware::CatchPanic; /// @@ -31,7 +31,7 @@ use futures_util::FutureExt as _; /// # ; /// ``` /// -/// ```no_run +/// ```ignore /// # use actix_web::App; /// use actix_web::middleware::{Logger, NormalizePath}; /// use actix_web_lab::middleware::CatchPanic; From 3eb6f6abefbb3b1db3d175ca5dc2435bf2726c9e Mon Sep 17 00:00:00 2001 From: Harsh Singh <64768386+harshdoesdev@users.noreply.github.com> Date: Tue, 7 Nov 2023 21:52:11 +0530 Subject: [PATCH 42/43] backwards compatibility for package-query (#1460) * backwards compatibility for package-query * refactored code and added deprecation message * removed comments * minor changes * applied recommended changes --- fastn-core/src/library2022/mod.rs | 11 +--- fastn-core/src/library2022/processor/mod.rs | 3 +- .../library2022/processor/package_query.rs | 61 +++++++++++++++++++ .../src/library2022/processor/sqlite.rs | 16 +++-- fastn-core/src/library2022/utils.rs | 6 ++ 5 files changed, 81 insertions(+), 16 deletions(-) create mode 100644 fastn-core/src/library2022/processor/package_query.rs diff --git a/fastn-core/src/library2022/mod.rs b/fastn-core/src/library2022/mod.rs index 4107921f2f..d866c69f71 100644 --- a/fastn-core/src/library2022/mod.rs +++ b/fastn-core/src/library2022/mod.rs @@ -255,16 +255,7 @@ impl Library2022 { "fastn-apps" => processor::apps::process(value, kind, doc, self), "is-reader" => processor::user_group::is_reader(value, kind, doc, self).await, "sql" => processor::sql::process(value, kind, doc, self).await, - "package-query" => { - processor::sqlite::process( - value, - kind, - doc, - self, - &fastn_core::library2022::processor::sql::get_db_config()?, - ) - .await - } + "package-query" => processor::package_query::process(value, kind, doc, self).await, "pg" => processor::pg::process(value, kind, doc).await, "package-tree" => { processor::package_tree::process(value, kind, doc, &self.config).await diff --git a/fastn-core/src/library2022/processor/mod.rs b/fastn-core/src/library2022/processor/mod.rs index d9e21caeed..2f4fdd08bc 100644 --- a/fastn-core/src/library2022/processor/mod.rs +++ b/fastn-core/src/library2022/processor/mod.rs @@ -5,12 +5,13 @@ pub(crate) mod figma_tokens; pub(crate) mod figma_typography_tokens; pub(crate) mod get_data; pub(crate) mod http; +pub(crate) mod package_query; pub(crate) mod package_tree; pub(crate) mod pg; pub(crate) mod query; pub(crate) mod request_data; pub(crate) mod sitemap; -pub mod sql; +pub(crate) mod sql; pub(crate) mod sqlite; pub(crate) mod toc; pub(crate) mod user_details; diff --git a/fastn-core/src/library2022/processor/package_query.rs b/fastn-core/src/library2022/processor/package_query.rs new file mode 100644 index 0000000000..537b499fd8 --- /dev/null +++ b/fastn-core/src/library2022/processor/package_query.rs @@ -0,0 +1,61 @@ +pub async fn process( + value: ftd::ast::VariableValue, + kind: ftd::interpreter::Kind, + doc: &ftd::interpreter::TDoc<'_>, + req_config: &fastn_core::RequestConfig, +) -> ftd::interpreter::Result { + let (headers, query) = + fastn_core::library2022::processor::sqlite::get_p1_data("package-data", &value, doc.name)?; + + fastn_core::library2022::utils::log_deprecation_warning( + "`package-query` has been deprecated, use `sql` processor instead.", + ); + + let sqlite_database = + match headers.get_optional_string_by_key("db", doc.name, value.line_number())? { + Some(k) => k, + None => { + return ftd::interpreter::utils::e2( + "`db` is not specified".to_string(), + doc.name, + value.line_number(), + ) + } + }; + + let sqlite_database_path = req_config.config.root.join(sqlite_database.as_str()); + + if !sqlite_database_path.exists() { + return ftd::interpreter::utils::e2( + "`db` does not exists for package-query processor".to_string(), + doc.name, + value.line_number(), + ); + } + + let query_response = fastn_core::library2022::processor::sqlite::execute_query( + &sqlite_database_path, + query.as_str(), + doc, + headers, + value.line_number(), + ) + .await; + + match query_response { + Ok(result) => fastn_core::library2022::processor::sqlite::result_to_value( + Ok(result), + kind, + doc, + &value, + super::sql::STATUS_OK, + ), + Err(e) => fastn_core::library2022::processor::sqlite::result_to_value( + Err(e.to_string()), + kind, + doc, + &value, + super::sql::STATUS_ERROR, + ), + } +} diff --git a/fastn-core/src/library2022/processor/sqlite.rs b/fastn-core/src/library2022/processor/sqlite.rs index 91a7e6e8fe..a7723aa96e 100644 --- a/fastn-core/src/library2022/processor/sqlite.rs +++ b/fastn-core/src/library2022/processor/sqlite.rs @@ -28,10 +28,11 @@ pub async fn process( value: ftd::ast::VariableValue, kind: ftd::interpreter::Kind, doc: &ftd::interpreter::TDoc<'_>, - config: &fastn_core::RequestConfig, + req_config: &fastn_core::RequestConfig, db_config: &fastn_core::library2022::processor::sql::DatabaseConfig, ) -> ftd::interpreter::Result { let (headers, query) = get_p1_data("package-data", &value, doc.name)?; + let sqlite_database_path = req_config.config.root.join(&db_config.db_url); // need the query params // question is they can be multiple @@ -41,9 +42,14 @@ pub async fn process( // for now they wil be ordered // select * from users where - let db_path = config.config.root.join(&db_config.db_url); - let query_response = - execute_query(&db_path, query.as_str(), doc, headers, value.line_number()).await; + let query_response = execute_query( + &sqlite_database_path, + query.as_str(), + doc, + headers, + value.line_number(), + ) + .await; match query_response { Ok(result) => result_to_value(Ok(result), kind, doc, &value, super::sql::STATUS_OK), @@ -286,7 +292,7 @@ fn extract_named_parameters( Ok(params) } -async fn execute_query( +pub(crate) async fn execute_query( database_path: &camino::Utf8PathBuf, query: &str, doc: &ftd::interpreter::TDoc<'_>, diff --git a/fastn-core/src/library2022/utils.rs b/fastn-core/src/library2022/utils.rs index d91b49e0fe..21f9872ff1 100644 --- a/fastn-core/src/library2022/utils.rs +++ b/fastn-core/src/library2022/utils.rs @@ -14,3 +14,9 @@ pub fn document_full_id( Ok(format!("/{}/", full_document_id.trim_matches('/'))) } + +pub fn log_deprecation_warning(message: &str) { + use colored::Colorize; + + println!("{}", format!("Warning: {}", message).bright_yellow()); +} From 6b00e4f2e6411d59256b1e9a7306321be8ff41b3 Mon Sep 17 00:00:00 2001 From: Arpita-Jaiswal Date: Wed, 8 Nov 2023 13:00:51 +0530 Subject: [PATCH 43/43] Fix: Code theme css was not applying on element --- fastn-core/src/commands/serve.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/fastn-core/src/commands/serve.rs b/fastn-core/src/commands/serve.rs index b3cac66e35..8c7aa25673 100644 --- a/fastn-core/src/commands/serve.rs +++ b/fastn-core/src/commands/serve.rs @@ -583,7 +583,7 @@ fn handle_default_route( let theme_css = ftd::theme_css(); return theme_css.get(theme).cloned().map(|theme| { actix_web::HttpResponse::Ok() - .content_type(mime_guess::mime::TEXT_JAVASCRIPT) + .content_type(mime_guess::mime::TEXT_CSS) .body(theme) }); } else if req.path().ends_with(fastn_core::utils::hashed_prism_js()) { @@ -595,7 +595,7 @@ fn handle_default_route( } else if req.path().ends_with(fastn_core::utils::hashed_prism_css()) { return Some( actix_web::HttpResponse::Ok() - .content_type(mime_guess::mime::TEXT_JAVASCRIPT) + .content_type(mime_guess::mime::TEXT_CSS) .body(ftd::prism_css()), ); }