lxy 0.1.1

A convenient async http and RPC framework in Rust
Documentation
use std::future::Future;
use std::pin::Pin;
use std::sync::Arc;

use crate::server::Server;
use axum::Router;

use super::ServerConfig;
use crate::tracing::info;

pub(crate) struct HttpServer {
  router: Router,
  config: Arc<ServerConfig>,
}

impl HttpServer {
  pub fn new(router: Router, config: Arc<ServerConfig>) -> Self {
    Self { router, config }
  }
}

impl Server for HttpServer {
  type Future = Pin<Box<dyn Future<Output = ()> + Send>>;

  fn start(&self) -> Self::Future {
    let router = self.router.clone();
    let config = self.config.clone();

    Box::pin(async move {
      let listener = tokio::net::TcpListener::bind((config.host.as_str(), config.port))
        .await
        .expect("Failed to bind HTTP server");

      info!("HTTP server listening on {}:{}", config.host, config.port);

      axum::serve(listener, router.into_make_service())
        .await
        .expect("HTTP server error");
    })
  }
}