Skip to content

Commit

Permalink
feat: shuttle next (shuttle-hq#27)
Browse files Browse the repository at this point in the history
* feat: initial commit of shuttle-next example

* feat: add post endpoint

* feat: convert all examples to bins

* feat: remove syncwrapper

* feat: refactor axum hello-world

* feat: from impl for AxumService

* feat: update all axum examples

* feat: update actix examples (postgres not working)

* feat: update poem examples

* feat: update poise example

* feat: update rocket examples

* feat: update serenity and salvo

* feat: update thruster examples

* feat: update the tower and warp examples

* feat: update tide examples

* misc: bump versions (shuttle-hq#28)

* misc: v0.12.0-rc1 (shuttle-hq#29)

* refactor: update next example (shuttle-hq#30)

* chore: v0.12.0 (shuttle-hq#32)

---------

Co-authored-by: oddgrd <[email protected]>
  • Loading branch information
chesedo and oddgrd authored Mar 20, 2023
1 parent e0ef5e5 commit 1f6cbf9
Show file tree
Hide file tree
Showing 58 changed files with 292 additions and 241 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
target/
Cargo.lock
.cargo
6 changes: 4 additions & 2 deletions actix-web/hello-world/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,7 @@ version = "0.1.0"
edition = "2021"

[dependencies]
actix-web = "4.2.1"
shuttle-service = { version = '0.11.0', features = ["web-actix-web"] }
actix-web = "4.3.1"
shuttle-actix-web = { version = "0.12.0" }
shuttle-runtime = { version = "0.12.0" }
tokio = { version = "1.26.0" }
15 changes: 0 additions & 15 deletions actix-web/hello-world/src/lib.rs

This file was deleted.

17 changes: 17 additions & 0 deletions actix-web/hello-world/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
use actix_web::{get, web::ServiceConfig};
use shuttle_actix_web::ShuttleActixWeb;

#[get("/hello")]
async fn hello_world() -> &'static str {
"Hello World!"
}

#[shuttle_runtime::main]
async fn actix_web(
) -> ShuttleActixWeb<impl FnOnce(&mut ServiceConfig) + Send + Clone + 'static> {
let config = move |cfg: &mut ServiceConfig| {
cfg.service(hello_world);
};

Ok(config.into())
}
10 changes: 5 additions & 5 deletions actix-web/postgres/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ name = "postgres"
version = "0.1.0"
edition = "2021"

[lib]

[dependencies]
actix-web = "4.2.1"
actix-web = "4.3.1"
shuttle-actix-web = { version = "0.12.0" }
shuttle-runtime = { version = "0.12.0" }
serde = "1.0.148"
shuttle-service = { version = "0.11.0", features = ["web-actix-web"] }
shuttle-shared-db = { version = "0.11.0", features = ["postgres"] }
shuttle-shared-db = { version = "0.12.0", features = ["postgres"] }
sqlx = { version = "0.6.2", features = ["runtime-tokio-native-tls", "postgres"] }
tokio = { version = "1.26.0" }
13 changes: 8 additions & 5 deletions actix-web/postgres/src/lib.rs → actix-web/postgres/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ use actix_web::{
Result,
};
use serde::{Deserialize, Serialize};
use shuttle_service::{error::CustomError, ShuttleActixWeb};
use shuttle_actix_web::ShuttleActixWeb;
use shuttle_runtime::{CustomError};
use sqlx::{Executor, FromRow, PgPool};

#[get("/{id}")]
Expand Down Expand Up @@ -35,25 +36,27 @@ struct AppState {
pool: PgPool,
}

#[shuttle_service::main]
#[shuttle_runtime::main]
async fn actix_web(
#[shuttle_shared_db::Postgres] pool: PgPool,
) -> ShuttleActixWeb<impl FnOnce(&mut ServiceConfig) + Sync + Send + Clone + 'static> {
) -> ShuttleActixWeb<impl FnOnce(&mut ServiceConfig) + Send + Clone + 'static> {
pool.execute(include_str!("../schema.sql"))
.await
.map_err(CustomError::new)?;

let state = web::Data::new(AppState { pool });

Ok(move |cfg: &mut ServiceConfig| {
let config = move |cfg: &mut ServiceConfig| {
cfg.service(
web::scope("/todos")
.wrap(Logger::default())
.service(retrieve)
.service(add)
.app_data(state),
);
})
};

Ok(config.into())
}

#[derive(Deserialize)]
Expand Down
15 changes: 7 additions & 8 deletions actix-web/websocket-actorless/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,17 @@ version = "0.1.0"
edition = "2021"
publish = false

[lib]

[dependencies]
shuttle-service = { version = "0.11.0", features = ["web-actix-web"] }
shuttle-static-folder = "0.11.3"
actix-web = "4.2.1"
actix-ws = "0.2.5"
actix-files = "0.6.2"
actix-web = "4.3.1"
actix-ws = "0.2.5"
chrono = { version = "0.4.23", features = ["serde"] }
futures = "0.3"
reqwest = "0.11"
tokio = { version = "1", features = ["rt-multi-thread", "sync"] }
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
chrono = { version = "0.4.23", features = ["serde"] }
shuttle-actix-web = { version = "0.12.0" }
shuttle-runtime = { version = "0.12.0" }
shuttle-static-folder = "0.12.0"
tokio = { version = "1", features = ["rt-multi-thread", "sync"] }
tracing = "0.1"
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use actix_ws::Message;
use chrono::{DateTime, Utc};
use futures::StreamExt;
use serde::Serialize;
use shuttle_service::ShuttleActixWeb;
use shuttle_actix_web::ShuttleActixWeb;
use std::{
sync::{atomic::AtomicUsize, Arc},
time::Duration,
Expand Down Expand Up @@ -104,9 +104,9 @@ async fn index() -> impl Responder {
.map_err(|e| actix_web::error::ErrorInternalServerError(e))
}

#[shuttle_service::main]
#[shuttle_runtime::main]
async fn actix_web(
) -> ShuttleActixWeb<impl FnOnce(&mut ServiceConfig) + Sync + Send + Clone + 'static> {
) -> ShuttleActixWeb<impl FnOnce(&mut ServiceConfig) + Send + Clone + 'static> {
// We're going to use channels to communicate between threads.
// api state channel
let (tx_api_state, rx_api_state) = watch::channel(ApiStateMessage::default());
Expand Down Expand Up @@ -179,14 +179,15 @@ async fn actix_web(

let app_state = web::Data::new((tx_ws_state, rx_api_state));

Ok(move |cfg: &mut ServiceConfig| {
let config = move |cfg: &mut ServiceConfig| {
cfg.service(web::resource("/").route(web::get().to(index)))
.service(
web::resource("/ws")
.app_data(app_state)
.route(web::get().to(websocket)),
);
})
};
Ok(config.into())
}

async fn get_api_status(client: &reqwest::Client) -> bool {
Expand Down
9 changes: 4 additions & 5 deletions axum/hello-world/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,8 @@ name = "hello-world"
version = "0.1.0"
edition = "2021"

[lib]

[dependencies]
axum = "0.6.0"
shuttle-service = { version = "0.11.0", features = ["web-axum"] }
sync_wrapper = "0.1.1"
axum = "0.6.10"
shuttle-axum = { version = "0.12.0" }
shuttle-runtime = { version = "0.12.0" }
tokio = { version = "1.26.0" }
14 changes: 0 additions & 14 deletions axum/hello-world/src/lib.rs

This file was deleted.

12 changes: 12 additions & 0 deletions axum/hello-world/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
use axum::{routing::get, Router};

async fn hello_world() -> &'static str {
"Hello, world!"
}

#[shuttle_runtime::main]
async fn axum() -> shuttle_axum::ShuttleAxum {
let router = Router::new().route("/hello", get(hello_world));

Ok(router.into())
}
9 changes: 5 additions & 4 deletions axum/static-files/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@ edition = "2021"
publish = false

[dependencies]
axum = "0.6.1"
axum = "0.6.10"
axum-extra = { version = "0.4.2", features = ["spa"] }
shuttle-service = { version = "0.11.0", features = ["web-axum"] }
shuttle-static-folder = "0.11.3"
sync_wrapper = "0.1.1"
shuttle-axum = { version = "0.12.0" }
shuttle-runtime = { version = "0.12.0" }
shuttle-static-folder = "0.12.0"
tokio = { version = "1.26.0" }
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,20 @@ use std::path::PathBuf;

use axum::{routing::get, Router};
use axum_extra::routing::SpaRouter;
use sync_wrapper::SyncWrapper;

async fn hello_world() -> &'static str {
"Hello, world!"
}

#[shuttle_service::main]
#[shuttle_runtime::main]
async fn axum(
// Name your static assets folder by passing `folder = <name>` to `StaticFolder`
// If you don't pass a name, it will default to `static`.
#[shuttle_static_folder::StaticFolder(folder = "assets")] static_folder: PathBuf,
) -> shuttle_service::ShuttleAxum {
) -> shuttle_axum::ShuttleAxum {
let router = Router::new()
.route("/hello", get(hello_world))
.merge(SpaRouter::new("/assets", static_folder).index_file("index.html"));

let sync_wrapper = SyncWrapper::new(router);

Ok(sync_wrapper)
Ok(router.into())
}
9 changes: 5 additions & 4 deletions axum/static-next-server/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@ edition = "2021"
publish = false

[dependencies]
axum = "0.6.1"
axum = "0.6.10"
axum-extra = { version = "0.4.2", features = ["spa"] }
shuttle-service = { version = "0.11.0", features = ["web-axum"] }
shuttle-static-folder = "0.11.3"
sync_wrapper = "0.1.1"
shuttle-axum = { version = "0.12.0" }
shuttle-runtime = { version = "0.12.0" }
shuttle-static-folder = "0.12.0"
tokio = { version = "1.26.0" }
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,13 @@ use std::path::PathBuf;

use axum::Router;
use axum_extra::routing::SpaRouter;
use sync_wrapper::SyncWrapper;

#[shuttle_service::main]
#[shuttle_runtime::main]
async fn axum(
#[shuttle_static_folder::StaticFolder] static_folder: PathBuf,
) -> shuttle_service::ShuttleAxum {
) -> shuttle_axum::ShuttleAxum {
let router =
Router::new().merge(SpaRouter::new("/", static_folder).index_file("index.html"));

let sync_wrapper = SyncWrapper::new(router);

Ok(sync_wrapper)
Ok(router.into())
}
12 changes: 5 additions & 7 deletions axum/websocket/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,16 @@ name = "websocket"
version = "0.1.0"
edition = "2021"

[lib]

[dependencies]
axum = { version = "0.6.0", features = ["ws"] }
axum = { version = "0.6.10", features = ["ws"] }
chrono = { version = "0.4.23", features = ["serde"] }
futures = "0.3.25"
hyper = { version = "0.14.23", features = ["client", "http2"] }
hyper-tls = "0.5.0"
serde = { version = "1.0.148", features = ["derive"] }
serde_json = "1.0.89"
shuttle-service = { version = "0.11.0", features = ["web-axum"] }
shuttle-static-folder = "0.11.3"
sync_wrapper = "0.1.1"
tokio = { version = "1.22.0", features = ["full"] }
shuttle-axum = { version = "0.12.0" }
shuttle-runtime = { version = "0.12.0" }
shuttle-static-folder = "0.12.0"
tokio = { version = "1.26.0" }
tower-http = { version = "0.3.0", features = ["fs"] }
11 changes: 4 additions & 7 deletions axum/websocket/src/lib.rs → axum/websocket/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@ use futures::{SinkExt, StreamExt};
use hyper::{Client, Uri};
use hyper_tls::HttpsConnector;
use serde::Serialize;
use shuttle_service::ShuttleAxum;
use sync_wrapper::SyncWrapper;
use shuttle_axum::ShuttleAxum;
use tokio::{
sync::{watch, Mutex},
time::sleep,
Expand All @@ -39,8 +38,8 @@ struct Response {
is_up: bool,
}

#[shuttle_service::main]
async fn main(#[shuttle_static_folder::StaticFolder] static_folder: PathBuf) -> ShuttleAxum {
#[shuttle_runtime::main]
async fn axum(#[shuttle_static_folder::StaticFolder] static_folder: PathBuf) -> ShuttleAxum {
let (tx, rx) = watch::channel(Message::Text("{}".to_string()));

let state = Arc::new(Mutex::new(State {
Expand Down Expand Up @@ -82,9 +81,7 @@ async fn main(#[shuttle_static_folder::StaticFolder] static_folder: PathBuf) ->
.fallback_service(serve_dir)
.layer(Extension(state));

let sync_wrapper = SyncWrapper::new(router);

Ok(sync_wrapper)
Ok(router.into())
}

async fn handle_error(_err: std::io::Error) -> impl IntoResponse {
Expand Down
13 changes: 13 additions & 0 deletions next/hello-world/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
[package]
name = "hello-world"
version = "0.1.0"
edition = "2021"

[lib]
crate-type = [ "cdylib" ]

[dependencies]
# TODO: bump to 0.9 before merge
shuttle-next = "0.12.0"
tracing = "0.1.37"
futures = "0.3.25"
33 changes: 33 additions & 0 deletions next/hello-world/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
shuttle_next::app! {
use futures::TryStreamExt;
use tracing::debug;
use shuttle_next::body::StreamBody;
use shuttle_next::extract::BodyStream;
use shuttle_next::response::{Response, IntoResponse};

#[shuttle_next::endpoint(method = get, route = "/hello")]
async fn hello() -> &'static str {
"Hello, World!"
}

// We can also use tracing/log macros directly:
#[shuttle_next::endpoint(method = get, route = "/goodbye")]
async fn goodbye() -> &'static str {
debug!("goodbye endpoint called");
"Goodbye, World!"
}

// We can also extract the http body in our handlers.
// The endpoint below takes the body from the request using the axum `BodyStream`
// extractor, lazily maps its bytes to uppercase and streams it back in our response:
#[shuttle_next::endpoint(method = post, route = "/uppercase")]
async fn uppercase(body: BodyStream) -> impl IntoResponse {
let chunk_stream = body.map_ok(|chunk| {
chunk
.iter()
.map(|byte| byte.to_ascii_uppercase())
.collect::<Vec<u8>>()
});
Response::new(StreamBody::new(chunk_stream))
}
}
Loading

0 comments on commit 1f6cbf9

Please sign in to comment.