pub struct Application { /* private fields */ }Expand description
The application: a Container with a boot lifecycle, an environment, and
a filesystem layout.
Implementations§
Source§impl Application
impl Application
Sourcepub fn new(base_path: impl Into<PathBuf>) -> Self
pub fn new(base_path: impl Into<PathBuf>) -> Self
A new application rooted at base_path, in the production
environment until told otherwise.
Sourcepub fn with_environment(self, environment: impl Into<String>) -> Self
pub fn with_environment(self, environment: impl Into<String>) -> Self
Builder form of set_environment.
Sourcepub fn container(&self) -> &Arc<Container> ⓘ
pub fn container(&self) -> &Arc<Container> ⓘ
The shared container. Hand this to anything that needs to resolve services without holding the whole application.
Sourcepub fn register(&self, provider: impl ServiceProvider) -> Result<()>
pub fn register(&self, provider: impl ServiceProvider) -> Result<()>
Sourcepub fn register_arc(&self, provider: Arc<dyn ServiceProvider>) -> Result<()>
pub fn register_arc(&self, provider: Arc<dyn ServiceProvider>) -> Result<()>
register for a provider that is already shared.
Sourcepub fn provider_names(&self) -> Vec<&'static str>
pub fn provider_names(&self) -> Vec<&'static str>
The names of every registered provider, in registration order.
Sourcepub async fn boot(&self) -> Result<()>
pub async fn boot(&self) -> Result<()>
Boot every provider that has not booted yet, running the booting
hooks first and the booted hooks after.
Idempotent, and safe to call again after registering more providers — only the new ones boot.
Sourcepub fn booting(&self, hook: impl Fn(&Application) + Send + Sync + 'static)
pub fn booting(&self, hook: impl Fn(&Application) + Send + Sync + 'static)
Run hook immediately before providers boot.
Ignored if the application has already booted — “before boot” has passed, and silently running it late would be worse than not running it.
Sourcepub fn booted(&self, hook: impl Fn(&Application) + Send + Sync + 'static)
pub fn booted(&self, hook: impl Fn(&Application) + Send + Sync + 'static)
Run hook once the application has booted — or immediately, if it
already has. The immediate call is what makes this usable from code
that cannot know whether boot has happened yet.
Sourcepub fn terminating(&self, hook: impl Fn(&Application) + Send + Sync + 'static)
pub fn terminating(&self, hook: impl Fn(&Application) + Send + Sync + 'static)
Run hook when terminate is called — after a
response has been sent, for work that should not delay it.
Sourcepub fn terminate(&self)
pub fn terminate(&self)
Run the terminating hooks. The HTTP kernel calls this after flushing
a response; a console command calls it before exiting.
Sourcepub fn environment(&self) -> String
pub fn environment(&self) -> String
The current environment name (local, testing, production, …).
Sourcepub fn set_environment(&self, environment: impl Into<String>)
pub fn set_environment(&self, environment: impl Into<String>)
Set the environment name.
Sourcepub fn environment_is(&self, names: &[&str]) -> bool
pub fn environment_is(&self, names: &[&str]) -> bool
Whether the environment is any of names.
Sourcepub fn is_testing(&self) -> bool
pub fn is_testing(&self) -> bool
Whether the environment is testing.
Sourcepub fn is_production(&self) -> bool
pub fn is_production(&self) -> bool
Whether the environment is production.
Sourcepub fn path(&self, relative: impl AsRef<Path>) -> PathBuf
pub fn path(&self, relative: impl AsRef<Path>) -> PathBuf
A path relative to the application root.
Sourcepub fn config_path(&self) -> PathBuf
pub fn config_path(&self) -> PathBuf
<base>/config.
Sourcepub fn storage_path(&self) -> PathBuf
pub fn storage_path(&self) -> PathBuf
<base>/storage.
Sourcepub fn resource_path(&self) -> PathBuf
pub fn resource_path(&self) -> PathBuf
<base>/resources — where views live.
Sourcepub fn database_path(&self) -> PathBuf
pub fn database_path(&self) -> PathBuf
<base>/database.
Methods from Deref<Target = Container>§
Sourcepub fn singleton<T, F>(&self, factory: F)
pub fn singleton<T, F>(&self, factory: F)
Bind a factory that runs once; every resolution afterwards hands
back the same Arc.
Sourcepub fn scoped<T, F>(&self, factory: F)
pub fn scoped<T, F>(&self, factory: F)
Like singleton, but flush_scoped drops the
memoised instance so the next resolution rebuilds it.
This is the binding a long-running worker wants for per-job state: the queue worker flushes scoped bindings between jobs so one job cannot leak state into the next.
Sourcepub fn instance<T: Send + Sync + 'static>(&self, value: T)
pub fn instance<T: Send + Sync + 'static>(&self, value: T)
Register an already-built value. The container never constructs it.
Sourcepub fn instance_arc<T: Send + Sync + 'static>(&self, value: Arc<T>)
pub fn instance_arc<T: Send + Sync + 'static>(&self, value: Arc<T>)
Register an already-built value that is already behind an Arc —
useful when the caller must keep a handle to it too.
Sourcepub fn resolve<T: Send + Sync + 'static>(&self) -> Result<Arc<T>>
pub fn resolve<T: Send + Sync + 'static>(&self) -> Result<Arc<T>>
Resolve T, building it if necessary.
Fails when T was never bound, when its factory fails, or when
resolving it re-enters itself (a dependency cycle).
Sourcepub fn try_resolve<T: Send + Sync + 'static>(&self) -> Option<Arc<T>>
pub fn try_resolve<T: Send + Sync + 'static>(&self) -> Option<Arc<T>>
Resolve T, or None if it is unbound or its factory failed.
Sourcepub fn expect_resolve<T: Send + Sync + 'static>(&self) -> Arc<T> ⓘ
pub fn expect_resolve<T: Send + Sync + 'static>(&self) -> Arc<T> ⓘ
Resolve T, panicking with the resolution error if it is not
available. For bootstrap code and facades, where an unbound service is
a configuration bug rather than a runtime condition.
Sourcepub fn flush_scoped(&self)
pub fn flush_scoped(&self)
Drop the memoised instance of every scoped binding.
The bindings survive; only the built values are discarded.