|
| 1 | +use std::{path::PathBuf, sync::Arc}; |
| 2 | + |
1 | 3 | use axum::{
|
2 |
| - http::HeaderMap, |
3 |
| - response::IntoResponse, |
4 | 4 | routing::{get, post},
|
5 | 5 | Router,
|
6 | 6 | };
|
| 7 | +use tower::ServiceBuilder; |
| 8 | +use tower_http::{ |
| 9 | + compression::CompressionLayer, normalize_path::NormalizePathLayer, services::ServeDir, |
| 10 | + trace::TraceLayer, |
| 11 | +}; |
7 | 12 |
|
8 | 13 | mod index;
|
9 | 14 | mod link;
|
10 | 15 | mod login;
|
| 16 | +mod memory; |
| 17 | +mod not_found; |
11 | 18 | mod redirect;
|
12 | 19 | mod shared;
|
13 | 20 |
|
14 |
| -pub mod not_found; |
15 |
| - |
16 |
| -pub async fn get_robots_txt() -> &'static str { |
17 |
| - r"User-agent: * |
18 |
| -Allow: / |
19 |
| -Sitemap: https://bckt.xyz/sitemap.xml |
20 |
| -" |
21 |
| -} |
22 |
| - |
23 |
| -pub async fn get_sitemap_xml() -> impl IntoResponse { |
24 |
| - let body = r#"<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"> |
25 |
| - <url> |
26 |
| - <loc>https://bckt.xyz/</loc> |
27 |
| - </url> |
28 |
| -</urlset>"#; |
29 |
| - |
30 |
| - let mut headers = HeaderMap::new(); |
31 |
| - headers.insert("content-type", "application/xml".parse().unwrap()); |
32 |
| - |
33 |
| - (headers, body) |
| 21 | +#[derive(Debug, Clone)] |
| 22 | +pub struct State { |
| 23 | + pub auth: crate::services::Auth, |
34 | 24 | }
|
35 | 25 |
|
36 |
| -pub fn new() -> Router { |
| 26 | +fn new_root(state: State) -> Router { |
37 | 27 | Router::new()
|
38 | 28 | .route("/", get(index::get))
|
39 |
| - .route("/robots.txt", get(get_robots_txt)) |
40 |
| - .route("/sitemap.xml", get(get_sitemap_xml)) |
| 29 | + .route("/robots.txt", get(memory::get_robots_txt)) |
| 30 | + .route("/sitemap.xml", get(memory::get_sitemap_xml)) |
41 | 31 | .route("/link", get(link::get))
|
42 | 32 | .route("/link", post(link::post))
|
43 | 33 | .route("/login", get(login::get))
|
44 | 34 | .route("/login", post(login::post))
|
45 | 35 | .route("/:hash", get(redirect::get))
|
| 36 | + .with_state(Arc::new(state)) |
| 37 | +} |
| 38 | + |
| 39 | +pub fn new(state: State) -> Router { |
| 40 | + Router::new() |
| 41 | + .nest_service("/static", ServeDir::new(PathBuf::from("static"))) |
| 42 | + .nest("/", new_root(state)) |
| 43 | + .fallback(not_found::any) |
| 44 | + .layer( |
| 45 | + ServiceBuilder::new() |
| 46 | + .layer(TraceLayer::new_for_http()) |
| 47 | + .layer(CompressionLayer::new()) |
| 48 | + .layer(NormalizePathLayer::trim_trailing_slash()), |
| 49 | + ) |
46 | 50 | }
|
0 commit comments