apistos_shuttle/
lib.rs

1use std::net::SocketAddr;
2
3use actix_web::App;
4
5use apistos::app::OpenApiWrapper;
6use apistos::spec::Spec;
7
8#[derive(Clone)]
9pub struct ApistosActixWebService<T> {
10  pub spec: Spec,
11  pub service_config: T,
12  pub openapi_path: String,
13}
14
15#[shuttle_runtime::async_trait]
16impl<T> shuttle_runtime::Service for ApistosActixWebService<T>
17where
18  T: FnOnce(&mut apistos::web::ServiceConfig) + Send + Clone + 'static,
19{
20  async fn bind(mut self, addr: SocketAddr) -> Result<(), shuttle_runtime::Error> {
21    // Start a worker for each cpu, but no more than 4.
22    let worker_count = num_cpus::get().min(4);
23
24    let server = actix_web::HttpServer::new(move || {
25      let spec = self.spec.clone();
26      App::new()
27        .document(spec)
28        .configure(self.service_config.clone())
29        .build(&self.openapi_path)
30    })
31    .workers(worker_count)
32    .bind(addr)?
33    .run();
34
35    server.await.map_err(shuttle_runtime::CustomError::new)?;
36
37    Ok(())
38  }
39}
40
41#[doc = include_str!("../README.md")]
42pub type ShuttleApistosActixWeb<T> = Result<ApistosActixWebService<T>, shuttle_runtime::Error>;