Trait App

Source
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§

Source

fn name(&self) -> &str

The name of the app.

This should usually be the name of the crate.

§Examples
use cot::App;

struct MyApp;
impl App for MyApp {
    fn name(&self) -> &str {
        env!("CARGO_PKG_NAME")
    }
}

Provided Methods§

Source

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.

Source

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)])
    }
}
Source

fn migrations(&self) -> Vec<Box<SyncDynMigration>>

Available on crate feature db only.

Returns the migrations for the app. By default, it returns an empty list.

Source

fn admin_model_managers(&self) -> Vec<Box<dyn AdminModelManager>>

Returns the admin model managers for the app. By default, it returns an empty list.

Source

fn static_files(&self) -> Vec<(String, Bytes)>

Returns a list of static files that the app serves. By default, it returns an empty list.

Implementors§

Source§

impl App for AdminApp

Source§

impl App for DatabaseUserApp

Available on crate feature db only.