Expand description
Manage web server routing
§Example
This example you can adding custom routes into your application by
implementing routes trait from crate::app::Hooks
and adding your
endpoints to your application
use async_trait::async_trait;
use loco_rs::{
app::{AppContext, Hooks},
boot::{create_app, BootResult, StartMode},
controller::{channels::AppChannels, AppRoutes},
worker::Processor,
task::Tasks,
environment::Environment,
Result,
};
use sea_orm::DatabaseConnection;
use std::path::Path;
/// this code block should be taken from the sea_orm migration model.
pub struct App;
pub use sea_orm_migration::prelude::*;
pub struct Migrator;
#[async_trait::async_trait]
impl MigratorTrait for Migrator {
fn migrations() -> Vec<Box<dyn MigrationTrait>> {
vec![]
}
}
#[async_trait]
impl Hooks for App {
fn app_name() -> &'static str {
env!("CARGO_CRATE_NAME")
}
fn routes(ctx: &AppContext) -> AppRoutes {
AppRoutes::with_default_routes()
// .add_route(controllers::notes::routes())
}
async fn boot(mode: StartMode, environment: &Environment) -> Result<BootResult>{
create_app::<Self, Migrator>(mode, environment).await
}
/// Only when `channels` feature is enabled
fn register_channels(_ctx: &AppContext) -> AppChannels {
let channels = AppChannels::default();
//channels.register.ns("/", channels::application::on_connect);
channels
}
fn connect_workers<'a>(p: &'a mut Processor, ctx: &'a AppContext) {}
fn register_tasks(tasks: &mut Tasks) {}
async fn truncate(db: &DatabaseConnection) -> Result<()> {
Ok(())
}
async fn seed(db: &DatabaseConnection, base: &Path) -> Result<()> {
Ok(())
}
}
}
Modules§
- format
- This module contains utility functions for generating HTTP responses that are commonly used in web applications. These functions simplify the process of creating responses with various data types.
- middleware
- views
Structs§
- AppRoutes
- Represents the routes of the application.
- Error
Detail - Structure representing details about an error.
- Json
- List
Routes - Routes
Functions§
- not_
found - return not found status code
- unauthorized
- Create an unauthorized error with a specified message.