Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/main' into ci/circleci_migration
Browse files Browse the repository at this point in the history
  • Loading branch information
chesedo committed Jul 26, 2022
2 parents 7988c80 + 50e7cc2 commit bba85da
Show file tree
Hide file tree
Showing 26 changed files with 740 additions and 403 deletions.
4 changes: 3 additions & 1 deletion .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -239,13 +239,15 @@ workflows:
- workspace-fmt
matrix:
parameters:
framework: ["web-axum", "web-rocket", "web-tide", "web-tower"]
framework: ["web-axum", "web-rocket", "web-poem", "web-tide", "web-tower"]
- check-standalone:
matrix:
parameters:
path:
- examples/axum/hello-world
- examples/axum/websocket
- examples/poem/hello-world
- examples/poem/postgres
- examples/rocket/authentication
- examples/rocket/hello-world
- examples/rocket/postgres
Expand Down
2 changes: 2 additions & 0 deletions api/users.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,6 @@ projects = [
'hello-world-tide-app',
'hello-world-tower-app',
'postgres-tide-app',
'hello-world-poem-app',
'postgres-poem-app'
]
11 changes: 7 additions & 4 deletions cargo-shuttle/src/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,17 +98,20 @@ pub struct RunArgs {
#[derive(Parser, Debug)]
pub struct InitArgs {
/// Initialize with axum framework
#[clap(long, conflicts_with_all = &["rocket", "tide", "tower"])]
#[clap(long, conflicts_with_all = &["rocket", "tide", "tower", "poem"])]
pub axum: bool,
/// Initialize with actix-web framework
#[clap(long, conflicts_with_all = &["axum", "tide", "tower"])]
#[clap(long, conflicts_with_all = &["axum", "tide", "tower", "poem"])]
pub rocket: bool,
/// Initialize with tide framework
#[clap(long, conflicts_with_all = &["axum", "rocket", "tower"])]
#[clap(long, conflicts_with_all = &["axum", "rocket", "tower", "poem"])]
pub tide: bool,
/// Initialize with tower framework
#[clap(long, conflicts_with_all = &["axum", "rocket", "tide"])]
#[clap(long, conflicts_with_all = &["axum", "rocket", "tide", "poem"])]
pub tower: bool,
/// Initialize with poem framework
#[clap(long, conflicts_with_all = &["axum", "rocket", "tide", "tower"])]
pub poem: bool,
/// Path to initialize a new shuttle project
#[clap(
parse(try_from_os_str = parse_init_path),
Expand Down
83 changes: 82 additions & 1 deletion cargo-shuttle/src/init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,49 @@ impl ShuttleInit for ShuttleInitTide {
}
}

pub struct ShuttleInitPoem;

impl ShuttleInit for ShuttleInitPoem {
fn set_cargo_dependencies(
&self,
dependencies: &mut Table,
manifest_path: &Path,
url: &Url,
get_dependency_version_fn: GetDependencyVersionFn,
) {
set_inline_table_dependency_features(
"shuttle-service",
dependencies,
vec!["web-poem".to_string()],
);

set_key_value_dependency_version(
"poem",
dependencies,
manifest_path,
url,
get_dependency_version_fn,
);
}

fn get_boilerplate_code_for_framework(&self) -> &'static str {
indoc! {r#"
use poem::{get, handler, Route};
#[handler]
fn hello_world() -> &'static str {
"Hello, world!"
}
#[shuttle_service::main]
async fn poem() -> shuttle_service::ShuttlePoem<impl poem::Endpoint> {
let app = Route::new().at("/hello", get(hello_world));
Ok(app)
}"#}
}
}

pub struct ShuttleInitTower;

impl ShuttleInit for ShuttleInitTower {
Expand Down Expand Up @@ -267,6 +310,10 @@ pub fn get_framework(init_args: &InitArgs) -> Box<dyn ShuttleInit> {
return Box::new(ShuttleInitTower);
}

if init_args.poem {
return Box::new(ShuttleInitPoem);
}

Box::new(ShuttleInitNoOp)
}

Expand Down Expand Up @@ -405,6 +452,7 @@ mod shuttle_init_tests {
rocket: false,
tide: false,
tower: false,
poem: false,
path: PathBuf::new(),
};

Expand All @@ -413,6 +461,7 @@ mod shuttle_init_tests {
"rocket" => init_args.rocket = true,
"tide" => init_args.tide = true,
"tower" => init_args.tower = true,
"poem" => init_args.poem = true,
_ => unreachable!(),
}

Expand All @@ -437,12 +486,13 @@ mod shuttle_init_tests {

#[test]
fn test_get_framework_via_get_boilerplate_code() {
let frameworks = vec!["axum", "rocket", "tide", "tower"];
let frameworks = vec!["axum", "rocket", "tide", "tower", "poem"];
let framework_inits: Vec<Box<dyn ShuttleInit>> = vec![
Box::new(ShuttleInitAxum),
Box::new(ShuttleInitRocket),
Box::new(ShuttleInitTide),
Box::new(ShuttleInitTower),
Box::new(ShuttleInitPoem),
];

for (framework, expected_framework_init) in frameworks.into_iter().zip(framework_inits) {
Expand Down Expand Up @@ -644,4 +694,35 @@ mod shuttle_init_tests {

assert_eq!(cargo_toml.to_string(), expected);
}

#[test]
fn test_set_cargo_dependencies_poem() {
let mut cargo_toml = cargo_toml_factory();
let dependencies = cargo_toml["dependencies"].as_table_mut().unwrap();
let manifest_path = PathBuf::new();
let url = Url::parse("https://shuttle.rs").unwrap();

set_inline_table_dependency_version(
"shuttle-service",
dependencies,
&manifest_path,
&url,
mock_get_latest_dependency_version,
);

ShuttleInitPoem.set_cargo_dependencies(
dependencies,
&manifest_path,
&url,
mock_get_latest_dependency_version,
);

let expected = indoc! {r#"
[dependencies]
shuttle-service = { version = "1.0", features = ["web-poem"] }
poem = "1.0"
"#};

assert_eq!(cargo_toml.to_string(), expected);
}
}
2 changes: 2 additions & 0 deletions cargo-shuttle/tests/integration/init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ async fn cargo_shuttle_init(path: PathBuf) -> anyhow::Result<CommandOutcome> {
rocket: false,
tide: false,
tower: false,
poem: false,
path,
}),
})
Expand All @@ -45,6 +46,7 @@ async fn cargo_shuttle_init_framework(path: PathBuf) -> anyhow::Result<CommandOu
rocket: true,
tide: false,
tower: false,
poem: false,
path,
}),
})
Expand Down
47 changes: 47 additions & 0 deletions cargo-shuttle/tests/integration/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -203,3 +203,50 @@ async fn tower_hello_world() {

assert_eq!(request_text, "Hello, world!");
}

#[tokio::test]
async fn poem_hello_world() {
let port = cargo_shuttle_run("../examples/poem/hello-world").await;

let request_text = reqwest::Client::new()
.get(format!("http://localhost:{port}/hello"))
.send()
.await
.unwrap()
.text()
.await
.unwrap();

assert_eq!(request_text, "Hello, world!");
}

// This example uses a shared Postgres. Thus local runs should create a docker container for it.
#[tokio::test]
async fn poem_postgres() {
let port = cargo_shuttle_run("../examples/poem/postgres").await;
let client = reqwest::Client::new();

let post_text = client
.post(format!("http://localhost:{port}/todo"))
.body("{\"note\": \"Deploy to shuttle\"}")
.header("content-type", "application/json")
.send()
.await
.unwrap()
.text()
.await
.unwrap();

assert_eq!(post_text, "{\"id\":1,\"note\":\"Deploy to shuttle\"}");

let request_text = client
.get(format!("http://localhost:{port}/todo/1"))
.send()
.await
.unwrap()
.text()
.await
.unwrap();

assert_eq!(request_text, "{\"id\":1,\"note\":\"Deploy to shuttle\"}");
}
Loading

0 comments on commit bba85da

Please sign in to comment.