Skip to content

Commit

Permalink
Merge pull request #1434 from fastn-stack/tutor_3
Browse files Browse the repository at this point in the history
fastn tutor part tres
  • Loading branch information
Arpita-Jaiswal authored Nov 7, 2023
2 parents f4c7939 + 8d570e2 commit 4af75a8
Show file tree
Hide file tree
Showing 145 changed files with 1,133 additions and 1,119 deletions.
7 changes: 7 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@ members = [
"fastn-issues",
"fastn-js",
"fastn-grammar",
"foo",
# "fastn-wasm",
# "fastn-runtime",
]
exclude = ["fastn-runtime", "fastn-wasm"]
resolver = "2"

[workspace.package]
version = "0.3.0"
authors = [
"Amit Upadhyay <[email protected]>",
"Arpita Jaiswal <[email protected]>",
Expand Down
15 changes: 8 additions & 7 deletions fastn-core/src/apis/cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,10 @@ fn query(uri: &str) -> fastn_core::Result<QueryParams> {
})
}

pub async fn clear(req: &fastn_core::http::Request) -> fastn_core::http::Response {
pub async fn clear(
config: &fastn_core::Config,
req: &fastn_core::http::Request,
) -> fastn_core::http::Response {
let query = match query(req.uri()) {
Ok(q) => q,
Err(err) => {
Expand All @@ -50,7 +53,7 @@ pub async fn clear(req: &fastn_core::http::Request) -> fastn_core::http::Respons
}
};

if let Err(err) = clear_(&query, req).await {
if let Err(err) = clear_(config, &query, req).await {
return fastn_core::server_error!(
"fastn-Error: /-/clear-cache/, query: {:?}, error: {:?}",
query,
Expand All @@ -62,12 +65,10 @@ pub async fn clear(req: &fastn_core::http::Request) -> fastn_core::http::Respons
}

pub async fn clear_(
config: &fastn_core::Config,
query: &QueryParams,
req: &fastn_core::http::Request,
_req: &fastn_core::http::Request,
) -> fastn_core::Result<()> {
let config =
fastn_core::time("Config::read()")
.it(fastn_core::Config::read(None, false, Some(req)).await?);
if config.package.download_base_url.is_none() {
return Err(fastn_core::Error::APIResponseError(
"cannot remove anything, package does not have `download_base_url`".to_string(),
Expand Down Expand Up @@ -112,7 +113,7 @@ pub async fn clear_(

// Download FASTN.ftd again after removing all the content
if !config.root.join("FASTN.ftd").exists() {
fastn_core::commands::serve::download_init_package(config.package.download_base_url)
fastn_core::commands::serve::download_init_package(&config.package.download_base_url)
.await?;
}

Expand Down
9 changes: 3 additions & 6 deletions fastn-core/src/apis/clone.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,17 @@ pub struct CloneResponse {
pub reserved_crs: Vec<i32>,
}

pub async fn clone(
req: fastn_core::http::Request,
) -> fastn_core::Result<fastn_core::http::Response> {
pub async fn clone(config: &fastn_core::Config) -> fastn_core::Result<fastn_core::http::Response> {
// TODO: implement authentication
match clone_worker(req).await {
match clone_worker(config).await {
Ok(data) => fastn_core::http::api_ok(data),
Err(err) => fastn_core::http::api_error(err.to_string()),
}
}

async fn clone_worker(req: fastn_core::http::Request) -> fastn_core::Result<CloneResponse> {
async fn clone_worker(config: &fastn_core::Config) -> fastn_core::Result<CloneResponse> {
use itertools::Itertools;

let config = fastn_core::Config::read(None, false, Some(&req)).await?;
let all_files = config
.get_all_file_path(&config.package, Default::default())?
.into_iter()
Expand Down
21 changes: 12 additions & 9 deletions fastn-core/src/apis/cr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ pub struct CreateCRRequest {
}

pub async fn create_cr(
req: &fastn_core::http::Request,
config: &fastn_core::Config,
cr_req: CreateCRRequest,
) -> fastn_core::Result<fastn_core::http::Response> {
match create_cr_worker(req, cr_req).await {
match create_cr_worker(config, cr_req).await {
Ok(cr_number) => {
#[derive(serde::Serialize)]
struct CreateCRResponse {
Expand All @@ -21,35 +21,35 @@ pub async fn create_cr(
}

async fn create_cr_worker(
req: &fastn_core::http::Request,
config: &fastn_core::Config,
cr_request: CreateCRRequest,
) -> fastn_core::Result<usize> {
let config = fastn_core::Config::read(None, false, Some(req)).await?;
let cr_number = config.extract_cr_number().await?;
let default_title = format!("CR#{cr_number}");
let cr_meta = fastn_core::cr::CRMeta {
title: cr_request.title.unwrap_or(default_title),
cr_number: cr_number as usize,
open: true,
};
fastn_core::commands::create_cr::add_cr_to_workspace(&config, &cr_meta).await?;
fastn_core::commands::create_cr::add_cr_to_workspace(config, &cr_meta).await?;
Ok(cr_number as usize)
}

pub async fn create_cr_page(
config: &fastn_core::Config,
req: fastn_core::http::Request,
) -> fastn_core::Result<fastn_core::http::Response> {
match create_cr_page_worker(req).await {
match create_cr_page_worker(config, req).await {
Ok(body) => Ok(body),
Err(err) => fastn_core::http::api_error(err.to_string()),
}
}

async fn create_cr_page_worker(
config: &fastn_core::Config,
req: fastn_core::http::Request,
) -> fastn_core::Result<fastn_core::http::Response> {
let mut config = fastn_core::Config::read(None, false, Some(&req)).await?;
let create_cr_ftd = fastn_core::package_info_create_cr(&config)?;
let create_cr_ftd = fastn_core::package_info_create_cr(config)?;

let main_document = fastn_core::Document {
id: "create-cr.ftd".to_string(),
Expand All @@ -58,7 +58,10 @@ async fn create_cr_page_worker(
package_name: config.package.name.clone(),
};

fastn_core::package::package_doc::read_ftd(&mut config, &main_document, "/", false, false)
let mut req_config =
fastn_core::RequestConfig::new(config, &req, main_document.id.as_str(), "/");

fastn_core::package::package_doc::read_ftd(&mut req_config, &main_document, "/", false, false)
.await
.map(Into::into)
}
35 changes: 11 additions & 24 deletions fastn-core/src/apis/edit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,14 @@ pub struct EditResponse {
}

pub async fn edit(
config: &fastn_core::Config,
req: &fastn_core::http::Request,
req_data: EditRequest,
) -> fastn_core::Result<fastn_core::http::Response> {
let mut config = match fastn_core::Config::read(None, false, Some(req)).await {
Ok(config) => config,
Err(err) => return fastn_core::http::api_error(err.to_string()),
};
config.current_document = Some(req_data.path.to_string());
let mut req_config = fastn_core::RequestConfig::new(config, req, "edit.ftd", "/");
req_config.current_document = Some(req_data.path.to_string());

match config.can_write(req, req_data.path.as_str()).await {
match req_config.can_write(req_data.path.as_str()).await {
Ok(can_write) => {
if !can_write {
return Ok(fastn_core::unauthorised!(
Expand All @@ -59,7 +57,7 @@ pub async fn edit(
}

pub(crate) async fn edit_worker(
config: fastn_core::Config,
config: &fastn_core::Config,
request: EditRequest,
) -> fastn_core::Result<EditResponse> {
if request.is_delete() {
Expand Down Expand Up @@ -110,7 +108,7 @@ pub(crate) async fn edit_worker(
.await
{
let snapshots = fastn_core::snapshot::get_latest_snapshots(&config.root).await?;
let workspaces = fastn_core::snapshot::get_workspace(&config).await?;
let workspaces = fastn_core::snapshot::get_workspace(config).await?;

let file = fastn_core::get_file(
config.package.name.to_string(),
Expand Down Expand Up @@ -149,7 +147,7 @@ pub(crate) async fn edit_worker(

if let Some(before_update_status) = before_update_status {
let snapshots = fastn_core::snapshot::get_latest_snapshots(&config.root).await?;
let workspaces = fastn_core::snapshot::get_workspace(&config).await?;
let workspaces = fastn_core::snapshot::get_workspace(config).await?;
let file = fastn_core::get_file(
config.package.name.to_string(),
&config.root.join(&file_name),
Expand All @@ -174,14 +172,8 @@ pub(crate) async fn edit_worker(
})
}

pub async fn sync(
req: fastn_core::http::Request,
) -> fastn_core::Result<fastn_core::http::Response> {
let config = match fastn_core::Config::read(None, false, Some(&req)).await {
Ok(config) => config,
Err(err) => return fastn_core::http::api_error(err.to_string()),
};
match fastn_core::commands::sync::sync(&config, None).await {
pub async fn sync(config: &fastn_core::Config) -> fastn_core::Result<fastn_core::http::Response> {
match fastn_core::commands::sync::sync(config, None).await {
Ok(_) => {
#[derive(serde::Serialize)]
struct SyncResponse {
Expand All @@ -199,15 +191,10 @@ pub struct RevertRequest {
}

pub async fn revert(
req: &fastn_core::http::Request,
config: &fastn_core::Config,
rev: RevertRequest,
) -> fastn_core::Result<fastn_core::http::Response> {
let config = match fastn_core::Config::read(None, false, Some(req)).await {
Ok(config) => config,
Err(err) => return fastn_core::http::api_error(err.to_string()),
};

match fastn_core::commands::revert::revert(&config, rev.path.as_str()).await {
match fastn_core::commands::revert::revert(config, rev.path.as_str()).await {
Ok(_) => {
#[derive(serde::Serialize)]
struct RevertResponse {
Expand Down
17 changes: 11 additions & 6 deletions fastn-core/src/apis/edit_source.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
pub(crate) async fn edit_source(req: &fastn_core::http::Request) -> fastn_core::http::Response {
pub(crate) async fn edit_source(
config: &fastn_core::Config,
req: &fastn_core::http::Request,
) -> fastn_core::http::Response {
// TODO: Need to remove unwrap
let path = {
let mut path: camino::Utf8PathBuf =
Expand All @@ -9,7 +12,7 @@ pub(crate) async fn edit_source(req: &fastn_core::http::Request) -> fastn_core::
path
};

match handle_view_source(req, path.as_str()).await {
match handle_view_source(config, req, path.as_str()).await {
Ok(body) => fastn_core::http::ok(body),
Err(e) => {
fastn_core::server_error!("new_path: {}, Error: {:?}", path, e)
Expand All @@ -18,24 +21,26 @@ pub(crate) async fn edit_source(req: &fastn_core::http::Request) -> fastn_core::
}

async fn handle_view_source(
config: &fastn_core::Config,
req: &fastn_core::http::Request,
path: &str,
) -> fastn_core::Result<Vec<u8>> {
let mut config = fastn_core::Config::read(None, false, Some(req)).await?;
let mut req_config = fastn_core::RequestConfig::new(config, req, "editor-source.ftd", "/");

let file_name = config.get_file_path_and_resolve(path).await?;
let file = config.get_file_and_package_by_id(path).await?;
let file = req_config.get_file_and_package_by_id(path).await?;

match file {
fastn_core::File::Ftd(_) | fastn_core::File::Markdown(_) | fastn_core::File::Code(_) => {
let editor_ftd = fastn_core::package_editor_source(&config, file_name.as_str())?;
let editor_ftd = fastn_core::package_editor_source(config, file_name.as_str())?;
let main_document = fastn_core::Document {
id: "editor-source.ftd".to_string(),
content: editor_ftd,
parent_path: config.root.as_str().to_string(),
package_name: config.package.name.clone(),
};
fastn_core::package::package_doc::read_ftd(
&mut config,
&mut req_config,
&main_document,
"/",
false,
Expand Down
13 changes: 6 additions & 7 deletions fastn-core/src/apis/sync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,25 +63,24 @@ pub struct SyncRequest {
/// If conflict occur, Then send back updated version in latest.ftd with conflicted content
///
pub async fn sync(
req: &fastn_core::http::Request,
config: &fastn_core::Config,
sync_req: SyncRequest,
) -> fastn_core::Result<fastn_core::http::Response> {
dbg!("remote server call", &sync_req.package_name);

match sync_worker(req, sync_req).await {
match sync_worker(config, sync_req).await {
Ok(data) => fastn_core::http::api_ok(data),
Err(err) => fastn_core::http::api_error(err.to_string()),
}
}

pub(crate) async fn sync_worker(
req: &fastn_core::http::Request,
config: &fastn_core::Config,
request: SyncRequest,
) -> fastn_core::Result<SyncResponse> {
use itertools::Itertools;

// TODO: Need to call at once only
let config = fastn_core::Config::read(None, false, Some(req)).await?;
let mut snapshots = fastn_core::snapshot::get_latest_snapshots(&config.root).await?;
let client_snapshots = fastn_core::snapshot::resolve_snapshots(&request.latest_ftd).await?;
// let latest_ftd = tokio::fs::read_to_string(config.history_dir().join(".latest.ftd")).await?;
Expand Down Expand Up @@ -229,12 +228,12 @@ pub(crate) async fn sync_worker(
}
}

client_current_files(&config, &snapshots, &client_snapshots, &mut synced_files).await?;
client_current_files(config, &snapshots, &client_snapshots, &mut synced_files).await?;

let history_files = clone_history_files(&config, &snapshots, &client_snapshots).await?;
let history_files = clone_history_files(config, &snapshots, &client_snapshots).await?;

fastn_core::snapshot::create_latest_snapshots(
&config,
config,
&snapshots
.into_iter()
.map(|(filename, timestamp)| fastn_core::Snapshot {
Expand Down
13 changes: 6 additions & 7 deletions fastn-core/src/apis/sync2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,12 +110,12 @@ pub struct File {
}

pub async fn sync2(
req: &fastn_core::http::Request,
config: &fastn_core::Config,
sync_req: SyncRequest,
) -> fastn_core::Result<fastn_core::http::Response> {
dbg!("remote server call", &sync_req.package_name);

match sync_worker(req, sync_req).await {
match sync_worker(config, sync_req).await {
Ok(data) => fastn_core::http::api_ok(data),
Err(err) => fastn_core::http::api_error(err.to_string()),
}
Expand Down Expand Up @@ -308,14 +308,13 @@ pub(crate) async fn do_sync(
}

pub(crate) async fn sync_worker(
req: &fastn_core::http::Request,
config: &fastn_core::Config,
request: SyncRequest,
) -> fastn_core::Result<SyncResponse> {
use itertools::Itertools;

// TODO: Need to call at once only
let config = fastn_core::Config::read(None, false, Some(req)).await?;
let mut synced_files = do_sync(&config, request.files.as_slice()).await?;
let mut synced_files = do_sync(config, request.files.as_slice()).await?;
let remote_history = config.get_history().await?;
let remote_manifest =
fastn_core::history::FileHistory::get_remote_manifest(remote_history.as_slice(), true)?;
Expand All @@ -324,9 +323,9 @@ pub(crate) async fn sync_worker(
let client_latest =
fastn_core::history::FileHistory::get_remote_manifest(clone_history.as_slice(), true)?;

client_current_files(&config, &remote_manifest, &client_latest, &mut synced_files).await?;
client_current_files(config, &remote_manifest, &client_latest, &mut synced_files).await?;

let history_files = clone_history_files(&config, &remote_manifest, &client_latest).await?;
let history_files = clone_history_files(config, &remote_manifest, &client_latest).await?;

Ok(SyncResponse {
files: synced_files.into_values().collect_vec(),
Expand Down
Loading

0 comments on commit 4af75a8

Please sign in to comment.