Skip to content

Commit

Permalink
feat: convert to use Axum
Browse files Browse the repository at this point in the history
  • Loading branch information
protochron committed May 7, 2024
1 parent 517d5eb commit 847cdcd
Show file tree
Hide file tree
Showing 4 changed files with 394 additions and 147 deletions.
213 changes: 213 additions & 0 deletions cloud-hello/Cargo.lock

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

3 changes: 3 additions & 0 deletions cloud-hello/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,14 @@ version = "0.6.0"
crate-type = ["cdylib"]

[dependencies]
axum = {version = "0.7", default-features = false}
handlebars = "5"
http = "1"
rust-embed = "8"
serde = {version = "1", features = ["derive"]}
serde_json = "1"
tokio = {version = "1", features = ["rt"]}
tower-service = "0.3"
url = "2"
wit-bindgen = { version = "0.24", features = ["default"] }
woothee = "0.13.0"
Expand Down
46 changes: 46 additions & 0 deletions cloud-hello/src/helpers.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// This struct implementation is from
// https://github.com/wasmCloud/wasmCloud/blob/ef3955a754ab59d2597dd1ef1801ac667eaf19a5/crates/actor/src/wrappers/io.rs#L45-L89
// Copied here for convenience so we don't have to depend on the wasmcloud crate that implements
// it.
pub struct OutputStreamWriter<'a> {
stream: &'a mut crate::wasi::io::streams::OutputStream,
}

impl<'a> From<&'a mut crate::wasi::io::streams::OutputStream> for OutputStreamWriter<'a> {
fn from(stream: &'a mut crate::wasi::io::streams::OutputStream) -> Self {
Self { stream }
}
}

impl std::io::Write for OutputStreamWriter<'_> {
fn write(&mut self, buf: &[u8]) -> std::io::Result<usize> {
use crate::wasi::io::streams::StreamError;
use std::io;

let n = match self.stream.check_write().map(std::num::NonZeroU64::new) {
Ok(Some(n)) => n,
Ok(None) | Err(StreamError::Closed) => return Ok(0),
Err(StreamError::LastOperationFailed(e)) => {
return Err(io::Error::new(io::ErrorKind::Other, e.to_debug_string()))
}
};
let n = n
.get()
.try_into()
.map_err(|e| io::Error::new(io::ErrorKind::Other, e))?;
let n = buf.len().min(n);
self.stream.write(&buf[..n]).map_err(|e| match e {
StreamError::Closed => io::ErrorKind::UnexpectedEof.into(),
StreamError::LastOperationFailed(e) => {
io::Error::new(io::ErrorKind::Other, e.to_debug_string())
}
})?;
Ok(n)
}

fn flush(&mut self) -> std::io::Result<()> {
self.stream
.blocking_flush()
.map_err(|e| std::io::Error::new(std::io::ErrorKind::Other, e))
}
}
Loading

0 comments on commit 847cdcd

Please sign in to comment.