use crate::prelude::*;
use beet_core::prelude::*;
#[derive(Clone, Component)]
#[component(on_add=on_add)]
#[require(ExchangeSpawner, ServerStatus)]
pub struct HttpServer {
pub port: u16,
}
impl Default for HttpServer {
fn default() -> Self {
Self {
port: DEFAULT_SERVER_PORT,
}
}
}
#[allow(unused)]
fn on_add(mut world: DeferredWorld, cx: HookContext) {
#[cfg(feature = "lambda")]
world
.commands()
.run_system_cached_with(super::start_lambda_server, cx.entity);
#[cfg(not(feature = "lambda"))]
world
.commands()
.run_system_cached_with(super::start_hyper_server, cx.entity);
}
impl HttpServer {
pub fn new_test() -> Self {
use std::sync::atomic::AtomicU16;
use std::sync::atomic::Ordering;
static PORT: AtomicU16 = AtomicU16::new(DEFAULT_SERVER_TEST_PORT);
Self {
port: PORT.fetch_add(1, Ordering::SeqCst),
..default()
}
}
pub fn local_url(&self) -> String {
format!("http://127.0.0.1:{}", self.port)
}
}