Skip to content

Commit

Permalink
fix: actix integration with state (#523)
Browse files Browse the repository at this point in the history
  • Loading branch information
oddgrd authored Dec 12, 2022
1 parent 84250da commit 489b925
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 7 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

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

7 changes: 4 additions & 3 deletions cargo-shuttle/src/init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,18 +85,19 @@ impl ShuttleInit for ShuttleInitActixWeb {

fn get_boilerplate_code_for_framework(&self) -> &'static str {
indoc! {r#"
use actix_web::web::{resource, ServiceConfig};
use actix_web::{get, web::ServiceConfig};
use shuttle_service::ShuttleActixWeb;
#[get("/hello")]
async fn hello_world() -> &'static str {
"Hello World!"
}
#[shuttle_service::main]
async fn actix_web(
) -> ShuttleActixWeb<impl FnOnce(&mut ServiceConfig) + Sync + Send + Copy + Clone + 'static> {
) -> ShuttleActixWeb<impl FnOnce(&mut ServiceConfig) + Sync + Send + Clone + 'static> {
Ok(move |cfg: &mut ServiceConfig| {
cfg.service(resource("/hello").to(hello_world));
cfg.service(hello_world);
})
}"#}
}
Expand Down
3 changes: 2 additions & 1 deletion service/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ crossbeam-channel = "0.5.6"
futures = { version = "0.3.25", features = ["std"] }
hyper = { version = "0.14.23", features = ["server", "tcp", "http1"], optional = true }
libloading = { version = "0.7.4", optional = true }
num_cpus = { version = "1.14.0", optional = true }
pipe = "0.4.0"
poem = { version = "1.3.49", optional = true }
rocket = { version = "0.5.0-rc.2", optional = true }
Expand Down Expand Up @@ -65,7 +66,7 @@ default = ["codegen"]
codegen = ["shuttle-codegen"]
loader = ["cargo", "libloading"]

web-actix-web = ["actix-web"]
web-actix-web = ["actix-web", "num_cpus"]
web-axum = ["axum", "sync_wrapper"]
web-rocket = ["rocket"]
web-thruster = ["thruster"]
Expand Down
10 changes: 7 additions & 3 deletions service/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -557,10 +557,14 @@ impl Service for sync_wrapper::SyncWrapper<axum::Router> {
#[async_trait]
impl<F> Service for F
where
F: FnOnce(&mut actix_web::web::ServiceConfig) + Sync + Send + Copy + Clone + 'static,
F: FnOnce(&mut actix_web::web::ServiceConfig) + Sync + Send + Clone + 'static,
{
async fn bind(self: Box<Self>, addr: SocketAddr) -> Result<(), Error> {
let srv = actix_web::HttpServer::new(move || actix_web::App::new().configure(*self))
async fn bind(mut self: Box<Self>, addr: SocketAddr) -> Result<(), Error> {
// Start a worker for each cpu, but no more than 8.
let worker_count = num_cpus::get().max(8);

let srv = actix_web::HttpServer::new(move || actix_web::App::new().configure(self.clone()))
.workers(worker_count)
.bind(addr)?
.run();
srv.await.map_err(error::CustomError::new)?;
Expand Down

0 comments on commit 489b925

Please sign in to comment.