Skip to content
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

feat: DX #577

Merged
merged 6 commits into from
Jan 12, 2023
Merged
Show file tree
Hide file tree
Changes from 4 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: 1 addition & 1 deletion .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -405,7 +405,7 @@ workflows:
- workspace-clippy
matrix:
parameters:
crate: ["shuttle-deployer", "cargo-shuttle", "shuttle-codegen", "shuttle-common", "shuttle-proto", "shuttle-provisioner"]
crate: ["shuttle-deployer", "cargo-shuttle", "shuttle-codegen", "shuttle-common", "shuttle-proto", "shuttle-provisioner", "shuttle-runtime"]
- e2e-test:
requires:
- service-test
Expand Down
18 changes: 16 additions & 2 deletions Cargo.lock

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

11 changes: 6 additions & 5 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ members = [
"common",
"deployer",
"gateway",
"next",
"proto",
"provisioner",
"runtime",
Expand Down Expand Up @@ -37,13 +38,13 @@ shuttle-service = { path = "service", version = "0.8.0" }

anyhow = "1.0.66"
async-trait = "0.1.58"
axum = "0.6.0"
chrono = "0.4.23"
axum = { version = "0.6.0", default-features = false }
chrono = { version = "0.4.23", default-features = false, features = ["clock"] }
once_cell = "1.16.0"
uuid = "1.2.2"
thiserror = "1.0.37"
serde = "1.0.148"
serde = { version = "1.0.148", default-features = false }
serde_json = "1.0.89"
tonic = "0.8.3"
tracing = "0.1.37"
tracing-subscriber = "0.3.16"
tracing = { version = "0.1.37", default-features = false }
tracing-subscriber = { version = "0.3.16", default-features = false, features = ["registry", "std"] }
2 changes: 1 addition & 1 deletion cargo-shuttle/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ toml = "0.5.9"
toml_edit = "0.15.0"
tonic = { workspace = true }
tracing = { workspace = true }
tracing-subscriber = { workspace = true, features = ["env-filter"] }
tracing-subscriber = { workspace = true, features = ["env-filter", "fmt"] }
url = "2.3.1"
uuid = { workspace = true, features = ["v4"] }
webbrowser = "0.8.2"
Expand Down
42 changes: 21 additions & 21 deletions codegen/src/next/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ impl ToTokens for Endpoint {
}
};

let route = quote!(.route(#route, axum::routing::#method(#function)));
let route = quote!(.route(#route, shuttle_next::routing::#method(#function)));

route.to_tokens(tokens);
}
Expand Down Expand Up @@ -238,11 +238,11 @@ impl ToTokens for App {
let Self { endpoints } = self;

let app = quote!(
async fn __app(request: http::Request<axum::body::BoxBody>,) -> axum::response::Response
async fn __app(request: shuttle_next::Request<shuttle_next::body::BoxBody>,) -> shuttle_next::response::Response
{
use tower_service::Service;
use shuttle_next::Service;

let mut router = axum::Router::new()
let mut router = shuttle_next::Router::new()
#(#endpoints)*;

let response = router.call(request).await.unwrap();
Expand All @@ -267,18 +267,18 @@ pub(crate) fn wasi_bindings(app: App) -> proc_macro2::TokenStream {
body_read_fd: std::os::wasi::prelude::RawFd,
body_write_fd: std::os::wasi::prelude::RawFd,
) {
use axum::body::HttpBody;
use shuttle_common::wasm::Logger;
use shuttle_next::body::{Body, HttpBody};
use shuttle_next::tracing_prelude::*;
use shuttle_next::Logger;
use std::io::{Read, Write};
use std::os::wasi::io::FromRawFd;
use tracing_subscriber::prelude::*;

println!("inner handler awoken; interacting with fd={},{},{},{}", logs_fd, parts_fd, body_read_fd, body_write_fd);

// file descriptor 2 for writing logs to
let logs_fd = unsafe { std::fs::File::from_raw_fd(logs_fd) };

tracing_subscriber::registry()
shuttle_next::tracing_registry()
.with(Logger::new(logs_fd))
.init(); // this sets the subscriber as the global default and also adds a compatibility layer for capturing `log::Record`s

Expand All @@ -288,7 +288,7 @@ pub(crate) fn wasi_bindings(app: App) -> proc_macro2::TokenStream {
let reader = std::io::BufReader::new(&mut parts_fd);

// deserialize request parts from rust messagepack
let wrapper: shuttle_common::wasm::RequestWrapper = rmp_serde::from_read(reader).unwrap();
let wrapper: shuttle_next::RequestWrapper = shuttle_next::from_read(reader).unwrap();

// file descriptor 4 for reading http body into wasm
let mut body_read_stream = unsafe { std::fs::File::from_raw_fd(body_read_fd) };
Expand All @@ -297,20 +297,20 @@ pub(crate) fn wasi_bindings(app: App) -> proc_macro2::TokenStream {
let mut body_buf = Vec::new();
reader.read_to_end(&mut body_buf).unwrap();

let body = axum::body::Body::from(body_buf);
let body = Body::from(body_buf);

let request = wrapper
.into_request_builder()
.body(axum::body::boxed(body))
.body(shuttle_next::body::boxed(body))
.unwrap();

println!("inner router received request: {:?}", &request);
let res = futures_executor::block_on(__app(request));
let res = shuttle_next::block_on(__app(request));

let (parts, mut body) = res.into_parts();

// wrap and serialize response parts as rmp
let response_parts = shuttle_common::wasm::ResponseWrapper::from(parts).into_rmp();
let response_parts = shuttle_next::ResponseWrapper::from(parts).into_rmp();

// write response parts
parts_fd.write_all(&response_parts).unwrap();
Expand All @@ -319,7 +319,7 @@ pub(crate) fn wasi_bindings(app: App) -> proc_macro2::TokenStream {
let mut body_write_stream = unsafe { std::fs::File::from_raw_fd(body_write_fd) };

// write body if there is one
if let Some(body) = futures_executor::block_on(body.data()) {
if let Some(body) = shuttle_next::block_on(body.data()) {
body_write_stream.write_all(body.unwrap().as_ref()).unwrap();
}
}
Expand All @@ -345,7 +345,7 @@ mod tests {
};

let actual = quote!(#endpoint);
let expected = quote!(.route("/hello", axum::routing::get(hello)));
let expected = quote!(.route("/hello", shuttle_next::routing::get(hello)));

assert_eq!(actual.to_string(), expected.to_string());
}
Expand All @@ -370,13 +370,13 @@ mod tests {
let actual = quote!(#app);
let expected = quote!(
async fn __app(
request: http::Request<axum::body::BoxBody>,
) -> axum::response::Response {
use tower_service::Service;
request: shuttle_next::Request<shuttle_next::body::BoxBody>,
) -> shuttle_next::response::Response {
use shuttle_next::Service;

let mut router = axum::Router::new()
.route("/hello", axum::routing::get(hello))
.route("/goodbye", axum::routing::post(goodbye));
let mut router = shuttle_next::Router::new()
.route("/hello", shuttle_next::routing::get(hello))
.route("/goodbye", shuttle_next::routing::post(goodbye));

let response = router.call(request).await.unwrap();

Expand Down
15 changes: 8 additions & 7 deletions common/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,21 @@ description = "Common library for the shuttle platform (https://www.shuttle.rs/)
anyhow = { workspace = true, optional = true }
async-trait = { workspace = true , optional = true }
axum = { workspace = true, optional = true }
chrono = { workspace = true, features = ["serde"] }
chrono = { workspace = true }
comfy-table = { version = "6.1.3", optional = true }
crossterm = { version = "0.25.0", optional = true }
http = { version = "0.2.8", optional = true }
http-serde = { version = "1.1.2", optional = true }
once_cell = { workspace = true }
once_cell = { workspace = true, optional = true }
reqwest = { version = "0.11.13", optional = true }
rmp-serde = { version = "1.1.1", optional = true }
rustrict = "0.5.5"
serde = { workspace = true, features = ["derive"] }
rustrict = { version = "0.5.5", optional = true }
serde = { workspace = true }
serde_json = { workspace = true, optional = true }
strum = { version = "0.24.1", features = ["derive"] }
strum = { version = "0.24.1", features = ["derive"], optional = true }
tracing = { workspace = true }
tracing-subscriber = { workspace = true, optional = true }
uuid = { workspace = true, features = ["v4", "serde"] }
uuid = { workspace = true, features = ["v4", "serde"], optional = true }

[dev-dependencies]
cap-std = "1.0.2"
Expand All @@ -35,4 +35,5 @@ backend = ["async-trait", "axum"]
display = ["comfy-table", "crossterm"]
tracing = ["serde_json"]
wasm = ["http-serde", "http", "rmp-serde", "tracing", "tracing-subscriber"]
models = ["anyhow", "async-trait", "display", "http", "reqwest", "serde_json"]
models = ["anyhow", "async-trait", "display", "http", "reqwest", "serde_json", "service"]
service = ["chrono/serde", "once_cell", "rustrict", "serde/derive", "strum", "uuid"]
9 changes: 9 additions & 0 deletions common/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,21 +1,29 @@
#[cfg(feature = "backend")]
pub mod backends;
#[cfg(feature = "service")]
pub mod database;
#[cfg(feature = "service")]
pub mod deployment;
#[cfg(feature = "service")]
pub mod log;
#[cfg(feature = "models")]
pub mod models;
#[cfg(feature = "service")]
pub mod project;
#[cfg(feature = "service")]
pub mod storage_manager;
#[cfg(feature = "tracing")]
pub mod tracing;
#[cfg(feature = "wasm")]
pub mod wasm;

use serde::{Deserialize, Serialize};
#[cfg(feature = "service")]
use uuid::Uuid;

#[cfg(feature = "service")]
pub use log::Item as LogItem;
#[cfg(feature = "service")]
pub use log::STATE_MESSAGE;

#[cfg(debug_assertions)]
Expand All @@ -27,6 +35,7 @@ pub const API_URL_DEFAULT: &str = "https://api.shuttle.rs";
pub type ApiKey = String;
pub type ApiUrl = String;
pub type Host = String;
#[cfg(feature = "service")]
pub type DeploymentId = Uuid;

#[derive(Debug, Clone, Serialize, Deserialize)]
Expand Down
2 changes: 1 addition & 1 deletion gateway/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ publish = false
[dependencies]
acme2 = "0.5.1"
async-trait = { workspace = true }
axum = { workspace = true, features = [ "headers" ] }
axum = { workspace = true, features = [ "default", "headers" ] }
axum-server = { version = "0.4.4", features = [ "tls-rustls" ] }
base64 = "0.13.1"
bollard = "0.13.0"
Expand Down
27 changes: 27 additions & 0 deletions next/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
[package]
name = "shuttle-next"
version = "0.8.0"
edition.workspace = true
license.workspace = true
description = "Macros and aliases to deploy wasm on the shuttle platform (https://www.shuttle.rs/)"
homepage = "https://www.shuttle.rs"

[lib]

[dependencies]
# most axum features can be enabled, but "tokio" and "ws" depend on socket2
# via "hyper/tcp" which is not compatible with wasi
axum = { workspace = true }
futures-executor = "0.3.21"
http = "0.2.7"
rmp-serde = "1.1.1"
tower-service = "0.3.1"
tracing-subscriber = { workspace = true }

[dependencies.shuttle-codegen]
workspace = true
features = ["next"]

[dependencies.shuttle-common]
workspace = true
features = ["wasm"]
8 changes: 8 additions & 0 deletions next/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
pub use axum::*;
pub use futures_executor::block_on;
pub use http::Request;
pub use rmp_serde::from_read;
pub use shuttle_codegen::app;
pub use shuttle_common::wasm::{Logger, RequestWrapper, ResponseWrapper};
pub use tower_service::Service;
pub use tracing_subscriber::{prelude as tracing_prelude, registry as tracing_registry};
2 changes: 1 addition & 1 deletion proto/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ uuid = { workspace = true, features = ["v4"] }

[dependencies.shuttle-common]
workspace = true
features = ["models", "wasm"]
features = ["models", "service", "wasm"]

[build-dependencies]
tonic-build = "0.8.3"
2 changes: 1 addition & 1 deletion runtime/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ tokio = { version = "=1.22.0", features = ["full"] }
tokio-stream = "0.1.11"
tonic = { workspace = true }
tracing = { workspace = true }
tracing-subscriber = { workspace = true, features = ["env-filter"] }
tracing-subscriber = { workspace = true, features = ["env-filter", "fmt"] }
uuid = { workspace = true, features = ["v4"] }
wasi-common = "4.0.0"
wasmtime = "4.0.0"
Expand Down
2 changes: 1 addition & 1 deletion service/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ optional = true

[dependencies.shuttle-common]
workspace = true
features = ["tracing"]
features = ["tracing", "service"]

[dev-dependencies]
portpicker = "0.1.1"
Expand Down
1 change: 1 addition & 0 deletions service/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -680,3 +680,4 @@ pub type ShuttlePoise<T, E> = Result<std::sync::Arc<poise::Framework<T, E>>, Err

pub const VERSION: &str = env!("CARGO_PKG_VERSION");
pub const NAME: &str = env!("CARGO_PKG_NAME");
pub const NEXT_NAME: &str = "shuttle-next";
13 changes: 3 additions & 10 deletions service/src/loader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ use futures::FutureExt;
use uuid::Uuid;

use crate::error::CustomError;
use crate::{logger, Bootstrapper, NAME, VERSION};
use crate::{logger, Bootstrapper, NAME, NEXT_NAME, VERSION};
use crate::{Error, Factory, ServeHandle};

const ENTRYPOINT_SYMBOL_NAME: &[u8] = b"_create_service\0";
Expand Down Expand Up @@ -296,17 +296,10 @@ fn make_name_unique(summary: &mut Summary, deployment_id: Uuid) {
}

fn is_next(summary: &Summary) -> bool {
let features = if let Some(shuttle) = summary
summary
.dependencies()
.iter()
.find(|dependency| dependency.package_name() == "shuttle-codegen")
{
shuttle.features()
} else {
&[]
};

features.contains(&InternedString::new("next"))
.any(|dependency| dependency.package_name() == NEXT_NAME)
}

/// Check that the crate being build is compatible with this version of loader
Expand Down
14 changes: 1 addition & 13 deletions tmp/axum-wasm-expanded/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,6 @@ edition = "2021"
crate-type = [ "cdylib" ]

[dependencies]
# most axum features can be enabled, but "tokio" and "ws" depend on socket2
# via "hyper/tcp" which is not compatible with wasi
axum = { version = "0.6.0", default-features = false }
futures = "0.3.25"
futures-executor = "0.3.21"
http = "0.2.7"
tower-service = "0.3.1"
rmp-serde = { version = "1.1.1" }
shuttle-next = "0.8.0"
tracing = "0.1.37"
tracing-subscriber = "0.3.16"

[dependencies.shuttle-common]
path = "../../common"
features = ["wasm"]
version = "0.8.0"
Loading