Skip to main content
The Faculty Bot includes a comprehensive web interface built using the Rocket web framework for Rust. This interface provides authentication, user management, and administrative capabilities through a browser-based UI.

Architecture

The web interface runs alongside the Discord bot in the same application using Tokio’s async runtime:
src/main.rs
tokio::select! {
    _ = start_bot() => {},
    _ = rocket_result.launch() => {},
    _ = ctrl_z => {
        println!("Shutting down");
    }
}

Rocket Setup

The Rocket server is configured with routes, API endpoints, and template rendering:
src/main.rs
let rocket_result = rocket::build()
    .mount("/", 
        routes![web::index, web::verify, web::reverify, web::admin, 
                web::login, web::logout, web::switch_account, web::setup]
    )
    .mount("/api", routes![
        web::api::send_mail, web::api::check_code, 
        web::api::discord_auth, web::api::discord_callback
    ])
    .register("/", catchers![web::unauthorized, web::not_found])
    .attach(Template::fairing());

Key Components

Template Rendering

The web interface uses Handlebars templates through the rocket_dyn_templates crate:
src/web/mod.rs
use rocket_dyn_templates::Template;

#[get("/")]
pub async fn index(jar: &CookieJar<'_>) -> Template {
    let is_logged_in = is_logged_in(jar).await;
    let is_admin = User::user_has_role(
        jar.get("token").map(|cookie| cookie.value()).unwrap_or_default(), 
        Roles::Admin
    );
    
    let ctx = HomeContext {
        is_logged_in,
        is_admin,
    };
    
    Template::render("wip", &ctx)
}
Authentication is managed through HTTP-only cookies containing JWT tokens:
  • Tokens are stored in the token cookie
  • Cookies are marked as secure and http_only for security
  • Token validation happens on every protected route

Available Routes

Public Routes

RouteDescription
GET /Home page - shows login status
GET /loginLogin page
GET /setupInitial setup page

Protected Routes (Require Authentication)

RouteDescriptionRequired Role
GET /verifyEmail verification pageUser
GET /reverifyRe-verification pageUser
GET /switch-accountSwitch between accountsUser
GET /adminAdmin dashboardAdmin
GET /logoutLogout (clears token)User

API Routes

RouteDescription
POST /api/verify/sendMailSend verification email
POST /api/verify/checkCodeVerify email code
GET /api/auth/discordInitiate Discord OAuth
GET /api/auth/discord/callbackDiscord OAuth callback

Error Handlers

Custom error pages are provided for common HTTP errors:
src/web/mod.rs
#[catch(404)]
pub fn not_found(_req: &Request) -> Template {
    Template::render("404", &{})
}

#[catch(401)]
pub fn unauthorized(_req: &Request) -> Template {
    Template::render("401", &{})
}

Next Steps

Authentication

Learn about JWT tokens, roles, and request guards

Verify Page

Understand email verification flow

Admin Dashboard

Explore admin capabilities

API Reference

View API endpoint documentation

Build docs developers (and LLMs) love