use std::sync::Arc;
use claviron::Server;
use claviron::runner::{Owner, launch};
use claviron::runtime::{AppState, Register, registry};
use crate::Options;
use crate::application::{PhpOwner, PhpService};
use crate::options::Launch;
const PHP_RUNTIME: &str = "php";
type Registration = Box<dyn FnOnce(&AppState) -> registry::Result<()> + Send>;
pub struct Runner {
application: String,
options: Options,
request_mode: Option<crate::PhpRunner>,
registrations: Vec<Registration>,
#[cfg(feature = "axum")]
interceptor: Option<Registration>,
}
impl Runner {
pub fn new(application: impl Into<String>) -> Self {
Self {
application: application.into(),
options: Options::default(),
request_mode: None,
registrations: Vec::new(),
#[cfg(feature = "axum")]
interceptor: None,
}
}
pub fn with_options(
mut self,
options: Options,
) -> Self {
self.options = options;
self
}
pub fn with_request_mode(
mut self,
mode: crate::PhpRunner,
) -> Self {
self.request_mode = Some(mode);
self
}
pub fn with_config_path(
mut self,
path: impl AsRef<std::path::Path>,
) -> crate::Result<Self> {
self.options = Options::from_file(path)?;
Ok(self)
}
pub fn with_fleet_name(
mut self,
name: impl Into<crate::FleetName>,
) -> Self {
self.options.runner.fleet_name = Some(name.into().to_string());
self
}
pub fn with_services<C: Register<AppState> + 'static>(self) -> Self {
self.with_bootstrap(C::register)
}
pub fn with_bootstrap<F>(
mut self,
configure: F,
) -> Self
where
F: FnOnce(&AppState) -> registry::Result<()> + Send + 'static,
{
self.registrations.push(Box::new(configure));
self
}
#[cfg(feature = "axum")]
pub fn with_interceptor<F>(
mut self,
router: F,
) -> Self
where
F: FnOnce(AppState) -> claviron::axum::Router + Send + 'static,
{
self.interceptor = Some(Box::new(move |state| {
claviron::runner::register_router(state, router)
}));
self
}
pub async fn run(mut self) -> crate::Result<()> {
let registrations = self.registrations;
#[cfg(feature = "axum")]
let interceptor = self.interceptor;
if let Some(mode) = self.request_mode {
self.options.configuration.request_mode = Some(mode);
}
let root = std::env::current_dir()?;
let detected_framework =
self.options.configuration.prepare_php(&root)?;
if self.options.runner.frameworks.is_none() {
self.options.runner.frameworks = Some(detected_framework);
}
self.options.runner.icon.get_or_insert_with(|| "laravel".into());
self.options.validate()?;
let name = self
.options
.runner
.name
.clone()
.unwrap_or_else(|| self.application.clone());
claviron::orbit::transport::segment::check_application_name(&name)?;
#[cfg(feature = "watch")]
if self.options.runner.watch
&& self.options.runner.workers.unwrap_or(0) > 0
{
return Err(
"watch restarts the PHP application in its process: run it standalone".into(),
);
}
let system_log = self.options.system_log()?;
let config_dir = self
.options
.config_path
.parent()
.filter(|path| !path.as_os_str().is_empty())
.unwrap_or_else(|| std::path::Path::new("."))
.to_path_buf();
let production = self.options.runner.production;
let environment = self.options.runner.environment.clone();
let mut launch = Launch::from(self.options);
let prepared = launch.prepare(name)?;
let application = self.application;
let definitions = launch.clone();
let definition_name = application.clone();
let server = match (production, environment) {
(true, _) => Server::new().production(),
(false, Some(environment)) => {
Server::new().environment(environment)
}
(false, None) => Server::new(),
};
server
.name(prepared.name.clone())
.fleet_name(prepared.fleet_name)
.workers(prepared.workers)
.launch(prepared.envelope)
.config_dir(config_dir)
.system_log(system_log)
.with_definitions(move |state| {
let definitions = definitions.inherited(state)?;
claviron::runner::supervisor::register_policy(
state,
definitions.runner.supervisor.clone().unwrap_or_default(),
)?;
definitions.own.register(
state,
&definition_name,
definitions.runner.frameworks.clone().unwrap_or_default(),
definitions.runner.icon.clone(),
)
})
.register(move |state| {
let launch = launch.inherited(state)?;
let runner = &launch.runner;
let php = PhpService::new();
let script = if launch.own.request_mode
== Some(crate::PhpRunner::Script)
{
Some(launch.own.script_route(state)?)
} else {
None
};
state.registry_ref().register(Arc::new(PhpOwner::new(
application.clone(),
php.clone(),
script,
)))?;
claviron::runner::register_owner(state, Owner::new(php))?;
let root = std::env::current_dir()
.map_err(|error| registry::Error::msg(error.to_string()))?;
launch::register_routing(state, runner, &root)?;
#[cfg(feature = "bench")]
claviron::bench::runner::BenchService::register(state)?;
for register in registrations {
register(state)?;
}
#[cfg(feature = "axum")]
if let Some(interceptor) = interceptor {
interceptor(state)?;
}
claviron::runner::ensure_application(state)?;
launch::register_trust(state, runner)?;
#[cfg(feature = "watch")]
if runner.watch {
let watcher = crate::watch::watcher(
root,
runner.watch_paths.clone(),
application.clone(),
)
.map_err(registry::Error::msg)?;
state.registry_ref().register(Arc::new(watcher))?;
}
launch::register_doors(
state,
runner,
PHP_RUNTIME,
Some(claviron::orbit::transport::TRANSPORT_INBOX_CAPACITY),
)
})
.serve()
.await?;
Ok(())
}
}