Skip to content

Commit

Permalink
Merge branch 'main' into b5/gateway_templates
Browse files Browse the repository at this point in the history
  • Loading branch information
b5 committed Oct 13, 2022
2 parents 2a8ba85 + 2db7bf0 commit 2e3bc28
Show file tree
Hide file tree
Showing 9 changed files with 171 additions and 7 deletions.
2 changes: 2 additions & 0 deletions .cargo/config
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[alias]
xtask = "run --package xtask --"
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,3 @@
/iroh_gateway/test_files/*
.env
Cargo.lock
foo
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ members = [
"iroh-share",
"iroh-util",
"stores/*",
"examples"
"examples",
"xtask"
]
# even if all crates are set to edition 2021, we still need to force the
# resolver to 2
Expand Down
8 changes: 7 additions & 1 deletion iroh-gateway/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ use iroh_metrics::{
observe, record,
};
use iroh_resolver::resolver::{
CidOrDomain, ContentLoader, Metadata, Out, OutMetrics, OutPrettyReader, Resolver, Source,
CidOrDomain, ContentLoader, Metadata, Out, OutMetrics, OutPrettyReader, OutType, Resolver,
Source,
};
use tokio::io::{AsyncReadExt, AsyncWrite};
use tokio_util::io::ReaderStream;
Expand All @@ -32,6 +33,7 @@ pub struct PrettyStreamBody<T: ContentLoader>(ReaderStream<OutPrettyReader<T>>,
pub enum FileResult<T: ContentLoader> {
File(PrettyStreamBody<T>),
Directory(Out),
Raw(PrettyStreamBody<T>),
}

impl<T: ContentLoader + std::marker::Unpin> http_body::Body for PrettyStreamBody<T> {
Expand Down Expand Up @@ -100,6 +102,10 @@ impl<T: ContentLoader + std::marker::Unpin> Client<T> {
let stream = ReaderStream::new(reader);
let body = PrettyStreamBody(stream, metadata.size);

if metadata.typ == OutType::Raw {
return Ok((FileResult::Raw(body), metadata));
}

Ok((FileResult::File(body), metadata))
}
}
Expand Down
19 changes: 17 additions & 2 deletions iroh-gateway/src/handlers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -365,7 +365,7 @@ async fn serve_raw<T: ContentLoader + std::marker::Unpin>(
.map_err(|e| error(StatusCode::INTERNAL_SERVER_ERROR, &e, &state))?;

match body {
FileResult::File(body) => {
FileResult::File(body) | FileResult::Raw(body) => {
set_content_disposition_headers(
&mut headers,
format!("{}.bin", req.cid).as_str(),
Expand Down Expand Up @@ -399,7 +399,7 @@ async fn serve_car<T: ContentLoader + std::marker::Unpin>(
.map_err(|e| error(StatusCode::INTERNAL_SERVER_ERROR, &e, &state))?;

match body {
FileResult::File(body) => {
FileResult::File(body) | FileResult::Raw(body) => {
set_content_disposition_headers(
&mut headers,
format!("{}.car", req.cid).as_str(),
Expand Down Expand Up @@ -515,6 +515,21 @@ async fn serve_fs<T: ContentLoader + std::marker::Unpin>(
)),
}
}
FileResult::Raw(body) => {
// todo(arqu): error on no size
// todo(arqu): add lazy seeking
add_cache_control_headers(&mut headers, metadata.clone());
set_etag_headers(&mut headers, get_etag(&req.cid, Some(req.format.clone())));
add_ipfs_roots_headers(&mut headers, metadata);
let name = add_content_disposition_headers(
&mut headers,
&req.query_file_name,
&req.content_path,
req.download,
);
add_content_type_headers(&mut headers, &name);
response(StatusCode::OK, body, headers)
}
}
}

Expand Down
3 changes: 1 addition & 2 deletions iroh-resolver/src/resolver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -400,7 +400,6 @@ pub enum OutType {

#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub enum UnixfsType {
Raw,
Dir,
File,
Symlink,
Expand Down Expand Up @@ -963,7 +962,7 @@ impl<T: ContentLoader> Resolver<T> {
path: root_path,
size: Some(loaded_cid.data.len() as u64),
typ: OutType::Raw,
unixfs_type: Some(UnixfsType::Raw),
unixfs_type: None,
resolved_path: vec![cid],
source: loaded_cid.source,
};
Expand Down
13 changes: 13 additions & 0 deletions xtask/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
[package]
name = "xtask"
version = "0.1.0"
edition = "2021"
private = true

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
anyhow = "1.0"
clap = { version = "4.0.9", features = ["derive"] }
clap_mangen = "0.2.2"
iroh = { path = "../iroh" }
5 changes: 5 additions & 0 deletions xtask/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# iroh xtasks

This crate contains automation tasks similar to `make` or `npm run` commands in other languages, using the [cargo xtask pattern](https://github.com/matklad/cargo-xtask).

from the root of the project run `cargo xtask $TASK` to execute a task.
124 changes: 124 additions & 0 deletions xtask/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
use anyhow::Result;
use clap::{CommandFactory, Parser, Subcommand};
use std::{
env, fs, io,
path::{Path, PathBuf},
process::{Command, Stdio},
};

#[derive(Debug, Parser)]
#[command(name = "xtasks")]
#[command(about = "iroh automation tasks", long_about = None)]
#[command(arg_required_else_help = true)]
struct Cli {
#[command(subcommand)]
command: Commands,
}

#[derive(Debug, Subcommand)]
enum Commands {
#[command(about = "build application and man pages", long_about = None)]
Dist {},
#[command(about = "build man pages")]
Man {},
}

fn main() {
let args = Cli::parse();
if let Err(e) = run_subcommand(args) {
eprintln!("{}", e);
std::process::exit(-1);
}
}

fn run_subcommand(args: Cli) -> Result<()> {
match args.command {
Commands::Dist {} => dist()?,
Commands::Man {} => dist_manpage()?,
}
Ok(())
}

fn dist() -> Result<()> {
let _ = fs::remove_dir_all(&dist_dir());
fs::create_dir_all(&dist_dir())?;

dist_binary()?;
dist_manpage()?;

Ok(())
}

fn dist_binary() -> Result<()> {
let cargo = env::var("CARGO").unwrap_or_else(|_| "cargo".to_string());
let status = Command::new(cargo)
.current_dir(project_root())
.args(&["build", "--release"])
.status()?;

if !status.success() {
Err(anyhow::anyhow!("cargo build failed"))?;
}

let dst = project_root().join("target/release/iroh");

fs::copy(&dst, dist_dir().join("iroh"))?;

if Command::new("strip")
.arg("--version")
.stdout(Stdio::null())
.status()
.is_ok()
{
eprintln!("stripping the binary");
let status = Command::new("strip").arg(&dst).status()?;
if !status.success() {
Err(anyhow::anyhow!("strip failed"))?;
}
} else {
eprintln!("no `strip` utility found")
}

Ok(())
}

fn dist_manpage() -> Result<()> {
let outdir = dist_dir();
let f = fs::File::create(outdir.join("iroh.1"))?;
let mut buf = io::BufWriter::new(f);
let cmd = iroh::run::Cli::command();
let man = clap_mangen::Man::new(cmd.clone());
man.render(&mut buf)?;

write_subcommand_man_files("iroh", &cmd, &outdir)
}

fn write_subcommand_man_files(prefix: &str, cmd: &clap::Command, outdir: &PathBuf) -> Result<()> {
for subcommand in cmd.get_subcommands() {
let subcommand_name = format!("iroh{}", subcommand.get_name());
let f = fs::File::create(outdir.join(format!("{}{}", &subcommand_name, ".1")))?;
let mut buf = io::BufWriter::new(f);
let man = clap_mangen::Man::new(subcommand.clone());
man.render(&mut buf)?;

write_subcommand_man_files(
format!("{prefix}{subcommand_name}").as_str(),
subcommand,
outdir,
)?;
}

Ok(())
}

fn project_root() -> PathBuf {
Path::new(&env!("CARGO_MANIFEST_DIR"))
.ancestors()
.nth(1)
.unwrap()
.to_path_buf()
}

fn dist_dir() -> PathBuf {
project_root().join("target/dist")
}

0 comments on commit 2e3bc28

Please sign in to comment.