simple-web-app

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README

routes.rs (3374B)


      1 use axum::{
      2     Router,
      3     body::Body,
      4     http::{StatusCode, header},
      5     response::{IntoResponse, Response},
      6     routing::{get, post},
      7 };
      8 
      9 use crate::handlers;
     10 use crate::state::AppState;
     11 
     12 // Embed static files at compile time
     13 const STYLE_CSS: &[u8] = include_bytes!("../static/style.css");
     14 const DATASTAR_JS: &[u8] = include_bytes!("../static/datastar.js");
     15 const VALIDATION_ENHANCER_JS: &[u8] = include_bytes!("../static/validation-enhancer.js");
     16 
     17 async fn serve_static(content_type: &'static str, body: &'static [u8]) -> Response {
     18     Response::builder()
     19         .status(StatusCode::OK)
     20         .header(header::CONTENT_TYPE, content_type)
     21         .header(header::CACHE_CONTROL, "public, max-age=86400")
     22         .body(Body::from(body))
     23         .unwrap()
     24 }
     25 
     26 async fn serve_style() -> Response { serve_static("text/css", STYLE_CSS).await }
     27 async fn serve_datastar() -> Response { serve_static("application/javascript", DATASTAR_JS).await }
     28 async fn serve_validation_enhancer() -> Response { serve_static("application/javascript", VALIDATION_ENHANCER_JS).await }
     29 
     30 async fn static_404() -> impl IntoResponse {
     31     (StatusCode::NOT_FOUND, "Not found")
     32 }
     33 
     34 pub fn create_router(state: AppState) -> Router {
     35     Router::new()
     36         // Feeds
     37         .route("/", get(handlers::show_feed))
     38         .route("/new", get(handlers::show_new))
     39         .route("/top", get(handlers::show_top))
     40 
     41         // Stories
     42         .route("/story/{id}", get(handlers::show_story))
     43         .route("/submit", get(handlers::show_submit))
     44         .route("/submit", post(handlers::submit_story))
     45         .route("/story/{id}/edit", get(handlers::show_edit))
     46         .route("/story/{id}/edit", post(handlers::edit_story))
     47 
     48         // Comments
     49         .route("/story/{id}/comments-stream", get(handlers::comments_stream))
     50         .route("/story/{id}/comment", post(handlers::create_comment))
     51         .route("/comment/{id}/reply", get(handlers::show_reply_form))
     52         .route("/comment/{id}/reply", post(handlers::submit_reply))
     53 
     54         // Voting (Datastar)
     55         .route("/vote/story/{id}", post(handlers::vote_story))
     56         .route("/vote/comment/{id}", post(handlers::vote_comment))
     57 
     58         // Users
     59         .route("/user/{username}", get(handlers::show_user))
     60 
     61         // Tags
     62         .route("/tag/{name}", get(handlers::show_tag))
     63 
     64         // Search
     65         .route("/search", get(handlers::open_search))
     66         .route("/search", post(handlers::search))
     67 
     68         // Settings
     69         .route("/settings", get(handlers::open_settings))
     70         .route("/settings/tab", get(handlers::open_settings_tab))
     71 
     72         // Auth
     73         .route("/login", get(handlers::show_login))
     74         .route("/login", post(handlers::login))
     75         .route("/register", get(handlers::show_register))
     76         .route("/register", post(handlers::register))
     77         .route("/logout", get(handlers::logout))
     78         .route("/resend-verification", get(handlers::show_resend_verification))
     79         .route("/resend-verification", post(handlers::resend_verification))
     80         .route("/verify-email", get(handlers::verify_email))
     81 
     82         // Static files
     83         .route("/static/style.css", get(serve_style))
     84         .route("/static/datastar.js", get(serve_datastar))
     85         .route("/static/validation-enhancer.js", get(serve_validation_enhancer))
     86         .route("/static/{*path}", get(static_404))
     87         .with_state(state)
     88 }