Skip to content

chore: additional logging in upload API #57

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 16, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/file_uploader/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ impl<'a, T: PinataClient, E: S3Client> FileUploader<'a, T, E> {
}

pub async fn upload_file(&self, path: &Path) -> Result<String, UploadError> {
tracing::info!("Uploading file to IPFS: {:?}", path);
let ipfs_hash = self.pinata_client.upload_file_to_ipfs(path).await?;

// Read file contents
Expand All @@ -29,6 +30,7 @@ impl<'a, T: PinataClient, E: S3Client> FileUploader<'a, T, E> {
.map_err(|_| UploadError::ReadFile)?;

// Upload to S3
tracing::info!("Uploading file to S3: {:?}", path);
self.s3_client
.upload_file_to_s3(path, ipfs_hash.clone())
.await?;
Expand Down
2 changes: 1 addition & 1 deletion src/file_uploader/s3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ impl S3Client for S3ClientImpl {
.body(ByteStream::from(buffer))
.send()
.await
.map_err(|e| UploadError::S3UploadFailed(format!("{:?}", e)))?;
.map_err(|e| UploadError::S3UploadFailed(e.to_string()))?;
}
Ok(())
}
Expand Down
20 changes: 17 additions & 3 deletions src/handlers/upload.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ pub async fn handle_project_upload<'a>(
let project_dir = upload_dir.join(PROJECT_DIR);

// Unpack the tarball.
tracing::info!("Unpacking tarball: {}", orig_tarball_path.to_string_lossy());
let tarball = File::open(orig_tarball_path).map_err(|_| UploadError::OpenFile)?;
let decompressed = GzDecoder::new(tarball);
let mut archive = Archive::new(decompressed);
Expand All @@ -105,6 +106,7 @@ pub async fn handle_project_upload<'a>(
// Remove `out` directory if it exists.
let _ = fs::remove_dir_all(unpacked_dir.join("out"));

tracing::info!("Executing forc build: {}", forc_path.to_string_lossy());
let output = Command::new(format!("{}/bin/forc", forc_path.to_string_lossy()))
.arg("build")
.arg("--release")
Expand All @@ -120,6 +122,10 @@ pub async fn handle_project_upload<'a>(
}

// Copy files that are part of the Sway project to a new directory.
tracing::info!(
"Copying files to project directory: {}",
project_dir.to_string_lossy()
);
let output = Command::new("rsync")
.args([
"-av",
Expand All @@ -145,6 +151,7 @@ pub async fn handle_project_upload<'a>(
}

// Pack the new tarball.
tracing::info!("Packing tarball: {}", upload_dir.to_string_lossy());
let final_tarball_path = upload_dir.join(TARBALL_NAME);
let tar_gz = File::create(&final_tarball_path).map_err(|_| UploadError::OpenFile)?;
let enc = GzEncoder::new(tar_gz, Compression::default());
Expand All @@ -161,7 +168,11 @@ pub async fn handle_project_upload<'a>(
let enc = tar.into_inner().map_err(|_| UploadError::CopyFiles)?;
enc.finish().map_err(|_| UploadError::CopyFiles)?;

// Store the tarball in IPFS.
// Store the tarball.
tracing::info!(
"Uploading tarball: {}",
final_tarball_path.to_string_lossy()
);
let tarball_ipfs_hash = file_uploader.upload_file(&final_tarball_path).await?;

fn find_file_in_dir_by_suffix(dir: &Path, suffix: &str) -> Option<PathBuf> {
Expand All @@ -184,9 +195,12 @@ pub async fn handle_project_upload<'a>(
.next()
}

// Store the ABI in IPFS.
// Store the ABI.
let abi_ipfs_hash = match find_file_in_dir_by_suffix(&release_dir, "-abi.json") {
Some(abi_path) => Some(file_uploader.upload_file(&abi_path).await?),
Some(abi_path) => {
tracing::info!("Uploading ABI: {}", release_dir.to_string_lossy());
Some(file_uploader.upload_file(&abi_path).await?)
}
None => None,
};

Expand Down
21 changes: 16 additions & 5 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,13 @@ use forc_pub::middleware::cors::Cors;
use forc_pub::middleware::session_auth::{SessionAuth, SESSION_COOKIE_NAME};
use forc_pub::middleware::token_auth::TokenAuth;
use forc_pub::util::validate_or_format_semver;
use rocket::http::Status;
use rocket::{
data::Capped,
fs::TempFile,
http::{Cookie, CookieJar},
request::Request,
response::{self},
serde::json::Json,
State,
};
Expand Down Expand Up @@ -241,10 +244,18 @@ fn all_options() {
// Intentionally left empty
}

/// Catch 404 not founds.
#[catch(404)]
fn not_found() -> String {
"Not found".to_string()
/// Catch all errors and log them before returning a custom error message.
#[catch(default)]
fn default_catcher(status: Status, _req: &Request<'_>) -> response::status::Custom<String> {
tracing::error!(
"Error occurred: {} - {:?}",
status.code,
status.reason_lossy()
);
response::status::Custom(
status,
format!("Error: {} - {}", status.code, status.reason_lossy()),
)
}

// Indicates the service is running
Expand Down Expand Up @@ -289,5 +300,5 @@ async fn rocket() -> _ {
health
],
)
.register("/", catchers![not_found])
.register("/", catchers![default_catcher])
}
9 changes: 7 additions & 2 deletions src/util.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use semver::Version;
use std::path::Path;
use std::{env, path::Path};

pub fn validate_or_format_semver(version: &str) -> Option<String> {
// Remove the leading 'v' if it exists
Expand All @@ -23,7 +23,12 @@ pub fn load_env() {

// Then load `.env.local`, potentially overwriting values from `.env`
if let Err(e) = dotenvy::from_path_override(Path::new(".env.local")) {
tracing::error!("Could not load .env.local: {}", e);
if env::var("RUN_ENV").unwrap_or_default() == "local" {
// If RUN_ENV is not set, log the error
tracing::error!("Could not load .env.local: {}", e);
}

tracing::info!("Could not load .env.local: {}", e);
}
}

Expand Down
Loading