Extract router from main to its own module

This commit is contained in:
Juno Takano 2025-12-14 21:17:14 -03:00
commit 089b507299
6 changed files with 44 additions and 41 deletions

View file

@ -45,7 +45,6 @@ fn make_body(code: Option<u16>, message: Option<&str>) -> String {
.0
}
#[expect(clippy::unused_async)]
pub async fn not_found() -> Response<Body> {
by_code(
Some(404),

View file

@ -2,7 +2,6 @@ use axum::{body::Body, extract::Path, http::Response};
use crate::{formats::populate_graph, handlers, types::Node};
#[expect(clippy::unused_async)]
pub async fn node(Path(id): Path<String>) -> Response<Body> {
let mut context = tera::Context::new();

View file

@ -20,7 +20,6 @@ pub async fn nexus(template: &str) -> Response<Body> {
handlers::template::by_filename(template, &context, 500, None, false)
}
#[expect(clippy::unused_async)]
pub async fn search(Form(query): Form<Query>) -> Redirect {
Redirect::permanent(format!("/node/{}", query.node).as_str())
}

View file

@ -2,6 +2,7 @@ use std::{sync, time};
pub mod formats;
pub mod types;
pub mod router;
pub mod handlers;
pub mod syntax;
pub mod dev;

View file

@ -1,8 +1,6 @@
use std::{backtrace, io, panic};
use axum::{routing::get, Router};
use en::{ONSET, handlers, syntax, dev, formats::Format};
use en::{ONSET, syntax, dev};
#[tokio::main]
async fn main() -> io::Result<()> {
@ -27,41 +25,7 @@ async fn main() -> io::Result<()> {
}
}));
let app = Router::new()
.route(
"/",
get(|| handlers::navigation::nexus("index.html"))
.post(handlers::navigation::search),
)
.route(
"/graph/toml",
get(|| handlers::fixed::serial(&Format::Toml)),
)
.route(
"/graph/json",
get(|| handlers::fixed::serial(&Format::Json)),
)
.route(
"/static/style.css",
get(|| handlers::fixed::file("./static/style.css", "text/css")),
)
.route(
"/static/favicon.svg",
get(|| {
handlers::fixed::file("./static/favicon.svg", "image/svg+xml")
}),
)
.route(
"/node/{node_id}",
get(handlers::graph::node).post(handlers::graph::node),
)
.route("/tree", get(|| handlers::navigation::nexus("tree.html")))
.route("/about", get(|| handlers::template::fixed("about.html")))
.route(
"/acknowledgments",
get(|| handlers::template::fixed("acknowledgments.html")),
)
.fallback(handlers::error::not_found);
let app = en::router::new();
let listener =
tokio::net::TcpListener::bind(&address).await.map_err(|e| {

41
src/router.rs Normal file
View file

@ -0,0 +1,41 @@
use axum::{routing::get, Router};
use crate::{handlers, formats::Format};
pub fn new() -> Router {
Router::new()
.route(
"/",
get(|| handlers::navigation::nexus("index.html"))
.post(handlers::navigation::search),
)
.route(
"/graph/toml",
get(|| handlers::fixed::serial(&Format::Toml)),
)
.route(
"/graph/json",
get(|| handlers::fixed::serial(&Format::Json)),
)
.route(
"/static/style.css",
get(|| handlers::fixed::file("./static/style.css", "text/css")),
)
.route(
"/static/favicon.svg",
get(|| {
handlers::fixed::file("./static/favicon.svg", "image/svg+xml")
}),
)
.route(
"/node/{node_id}",
get(handlers::graph::node).post(handlers::graph::node),
)
.route("/tree", get(|| handlers::navigation::nexus("tree.html")))
.route("/about", get(|| handlers::template::fixed("about.html")))
.route(
"/acknowledgments",
get(|| handlers::template::fixed("acknowledgments.html")),
)
.fallback(handlers::error::not_found)
}