use std::net::ToSocketAddrs;
use actix_web::body::MessageBody;
use actix_web::dev::{ServiceFactory, ServiceRequest, ServiceResponse};
use actix_web::{App, Error, HttpRequest, HttpResponse, HttpServer, web};
use serenade_http::AsyncHttpKernel;
use crate::dispatch;
#[must_use]
pub fn app(
kernel: web::Data<AsyncHttpKernel>,
) -> App<
impl ServiceFactory<
ServiceRequest,
Config = (),
Response = ServiceResponse<impl MessageBody>,
Error = Error,
InitError = (),
>,
> {
App::new()
.app_data(kernel)
.default_service(web::to(serenade_service))
}
pub fn bind_server(
addr: impl ToSocketAddrs,
kernel: AsyncHttpKernel,
) -> std::io::Result<actix_web::dev::Server> {
let data = web::Data::new(kernel);
Ok(HttpServer::new(move || app(data.clone())).bind(addr)?.run())
}
#[allow(clippy::future_not_send)]
pub async fn await_bound(server: actix_web::dev::Server) -> std::io::Result<()> {
server.await
}
#[allow(clippy::future_not_send)]
pub async fn listen(addr: impl ToSocketAddrs, kernel: AsyncHttpKernel) -> std::io::Result<()> {
await_bound(bind_server(addr, kernel)?).await
}
#[allow(clippy::future_not_send)]
async fn serenade_service(
request: HttpRequest,
body: web::Bytes,
kernel: web::Data<AsyncHttpKernel>,
) -> HttpResponse {
dispatch::dispatch_async(kernel.get_ref(), &request, body).await
}