use lunatic::Tag;
use serde::{Deserialize, Serialize};
#[allow(unused)]
pub static DEFAULT_USER_AGENT: &str =
concat!(env!("CARGO_PKG_NAME"), "/", env!("CARGO_PKG_VERSION"));
#[derive(Serialize, Deserialize, Clone, Debug)]
pub struct DummyError;
pub struct DummyProcess;
#[lunatic::abstract_process]
impl DummyProcess {
#[init]
fn init(_: lunatic::ap::Config<Self>, _: ()) -> Result<Self, DummyError> {
Ok(Self)
}
#[terminate]
fn terminate(self) {
println!("Shutdown process");
}
#[handle_link_death]
fn handle_link_trapped(&self, _: Tag) {
println!("Link trapped");
}
}
#[macro_export]
macro_rules! wrap_server {
($name:ident, $router:ident, $addr:ident) => {
mod $name {
use lunatic::{
abstract_process, spawn_link,
supervisor::{Supervisor, SupervisorStrategy},
AbstractProcess, Process, Tag,
};
use submillisecond::Application;
struct ServerProcess(Process<()>);
struct ServerSup;
#[abstract_process]
impl ServerProcess {
#[init]
fn init(
_: lunatic::ap::Config<Self>,
_: (),
) -> Result<Self, crate::support::DummyError> {
Ok(Self(spawn_link!(|| {
Application::new(super::$router)
.serve(super::$addr)
.unwrap();
})))
}
#[terminate]
fn terminate(self) {
println!("Shutdown process");
}
#[handle_link_death]
fn handle_link_trapped(&self, _: Tag) {
println!("Link trapped");
}
}
impl Supervisor for ServerSup {
type Arg = String;
type Children = (ServerProcess, crate::support::DummyProcess);
fn init(config: &mut lunatic::supervisor::SupervisorConfig<Self>, name: Self::Arg) {
config.set_strategy(SupervisorStrategy::OneForOne);
config.children_args((((), None), ((), None)));
}
}
pub fn ensure_server() {
let name = format!("__{}__", stringify!($name));
if let Some(_) = Process::<Process<()>>::lookup(&name) {
return;
}
ServerSup::start(name.to_owned()).expect("should have started server");
}
}
};
}
pub type RouterFn =
fn() -> fn(req: ::submillisecond::RequestContext) -> ::submillisecond::response::Response;