#![doc = include_str!("../README.md")]
use std::net::SocketAddr;
pub use actix_web;
#[derive(Clone)]
pub struct ActixWebService<F>(pub F);
#[shuttle_runtime::async_trait]
impl<F> shuttle_runtime::Service for ActixWebService<F>
where
F: FnOnce(&mut actix_web::web::ServiceConfig) + Send + Clone + 'static,
{
async fn bind(mut self, addr: SocketAddr) -> Result<(), shuttle_runtime::Error> {
let worker_count = num_cpus::get().min(4);
let server =
actix_web::HttpServer::new(move || actix_web::App::new().configure(self.0.clone()))
.workers(worker_count)
.bind(addr)?
.run();
server.await.map_err(shuttle_runtime::CustomError::new)?;
Ok(())
}
}
impl<F> From<F> for ActixWebService<F>
where
F: FnOnce(&mut actix_web::web::ServiceConfig) + Send + Clone + 'static,
{
fn from(service_config: F) -> Self {
Self(service_config)
}
}
#[doc = include_str!("../README.md")]
pub type ShuttleActixWeb<F> = Result<ActixWebService<F>, shuttle_runtime::Error>;