pub trait App: Send + Sync {
// Required method
fn name(&self) -> &str;
// Provided methods
fn init<'life0, 'life1, 'async_trait>(
&'life0 self,
context: &'life1 mut ProjectContext,
) -> Pin<Box<dyn Future<Output = Result<()>> + Send + 'async_trait>>
where Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait { ... }
fn router(&self) -> Router { ... }
fn migrations(&self) -> Vec<Box<SyncDynMigration>> { ... }
fn admin_model_managers(&self) -> Vec<Box<dyn AdminModelManager>> { ... }
fn static_files(&self) -> Vec<(String, Bytes)> { ... }
}Expand description
A building block for a Cot project.
A Cot app is a part (ideally, reusable) of a Cot project that is responsible for its own set of functionalities. Examples of apps could be:
- admin panel
- user authentication
- blog
- message board
- session management
- etc.
Each app can have its own set of URLs that it can handle which can be mounted on the project’s router, its own set of middleware, database migrations (which can depend on other apps), etc.
Required Methods§
Provided Methods§
Sourcefn init<'life0, 'life1, 'async_trait>(
&'life0 self,
context: &'life1 mut ProjectContext,
) -> Pin<Box<dyn Future<Output = Result<()>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
fn init<'life0, 'life1, 'async_trait>(
&'life0 self,
context: &'life1 mut ProjectContext,
) -> Pin<Box<dyn Future<Output = Result<()>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
Initializes the app.
This method is called when the app is initialized. It can be used to initialize whatever is needed for the app to work, possibly depending on other apps, or the project’s configuration.
§Errors
This method returns an error if the app fails to initialize.
Sourcefn router(&self) -> Router
fn router(&self) -> Router
Returns the router for the app. By default, it returns an empty router.
§Examples
use cot::request::Request;
use cot::response::{Response, ResponseExt};
use cot::router::{Route, Router};
use cot::{App, Body, StatusCode};
async fn index(request: Request) -> cot::Result<Response> {
Ok(Response::new_html(
StatusCode::OK,
Body::fixed("Hello world!"),
))
}
struct MyApp;
impl App for MyApp {
fn name(&self) -> &str {
"my_app"
}
fn router(&self) -> Router {
Router::with_urls([Route::with_handler("/", index)])
}
}Sourcefn migrations(&self) -> Vec<Box<SyncDynMigration>>
Available on crate feature db only.
fn migrations(&self) -> Vec<Box<SyncDynMigration>>
db only.Returns the migrations for the app. By default, it returns an empty list.
Sourcefn admin_model_managers(&self) -> Vec<Box<dyn AdminModelManager>>
fn admin_model_managers(&self) -> Vec<Box<dyn AdminModelManager>>
Returns the admin model managers for the app. By default, it returns an empty list.
Sourcefn static_files(&self) -> Vec<(String, Bytes)>
fn static_files(&self) -> Vec<(String, Bytes)>
Returns a list of static files that the app serves. By default, it returns an empty list.