feat: Implement dedicated 404 page for routes under /_continuwuity/
This commit is contained in:
parent
43aa172829
commit
eec7103910
6 changed files with 24 additions and 14 deletions
|
|
@ -1,6 +1,10 @@
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
|
|
||||||
use axum::{Router, response::IntoResponse};
|
use axum::{
|
||||||
|
Router,
|
||||||
|
response::{IntoResponse, Redirect},
|
||||||
|
routing::get,
|
||||||
|
};
|
||||||
use conduwuit::Error;
|
use conduwuit::Error;
|
||||||
use conduwuit_service::{Services, state, state::Guard};
|
use conduwuit_service::{Services, state, state::Guard};
|
||||||
use http::{StatusCode, Uri};
|
use http::{StatusCode, Uri};
|
||||||
|
|
@ -10,7 +14,8 @@ pub(crate) fn build(services: &Arc<Services>) -> (Router, Guard) {
|
||||||
let router = Router::<state::State>::new();
|
let router = Router::<state::State>::new();
|
||||||
let (state, guard) = state::create(services.clone());
|
let (state, guard) = state::create(services.clone());
|
||||||
let router = conduwuit_api::router::build(router, &services.server)
|
let router = conduwuit_api::router::build(router, &services.server)
|
||||||
.merge(conduwuit_web::build())
|
.nest("/_continuwuity/", conduwuit_web::build())
|
||||||
|
.route("/", get(async || Redirect::permanent("/_continuwuity/")))
|
||||||
.fallback(not_found)
|
.fallback(not_found)
|
||||||
.with_state(state);
|
.with_state(state);
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -20,6 +20,8 @@ enum WebError {
|
||||||
ValidationError(#[from] validator::ValidationErrors),
|
ValidationError(#[from] validator::ValidationErrors),
|
||||||
#[error("Bad request: {0}")]
|
#[error("Bad request: {0}")]
|
||||||
BadRequest(String),
|
BadRequest(String),
|
||||||
|
#[error("This page does not exist.")]
|
||||||
|
NotFound,
|
||||||
#[error("Internal server error: {0}")]
|
#[error("Internal server error: {0}")]
|
||||||
InternalError(#[from] conduwuit_core::Error),
|
InternalError(#[from] conduwuit_core::Error),
|
||||||
}
|
}
|
||||||
|
|
@ -28,14 +30,13 @@ impl IntoResponse for WebError {
|
||||||
fn into_response(self) -> Response {
|
fn into_response(self) -> Response {
|
||||||
#[derive(Debug, Template)]
|
#[derive(Debug, Template)]
|
||||||
#[template(path = "error.html.j2")]
|
#[template(path = "error.html.j2")]
|
||||||
#[allow(unused)]
|
|
||||||
struct Error {
|
struct Error {
|
||||||
error: WebError,
|
error: WebError,
|
||||||
status: StatusCode,
|
status: StatusCode,
|
||||||
}
|
}
|
||||||
|
|
||||||
let status = match &self {
|
let status = match &self {
|
||||||
| Self::ValidationError(_) | Self::BadRequest(_) => StatusCode::BAD_REQUEST,
|
| Self::ValidationError(_) | Self::BadRequest(_) => StatusCode::BAD_REQUEST,
|
||||||
|
| Self::NotFound => StatusCode::NOT_FOUND,
|
||||||
| _ => StatusCode::INTERNAL_SERVER_ERROR,
|
| _ => StatusCode::INTERNAL_SERVER_ERROR,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
@ -57,6 +58,7 @@ pub fn build() -> Router<state::State> {
|
||||||
.merge(index::build())
|
.merge(index::build())
|
||||||
.merge(resources::build())
|
.merge(resources::build())
|
||||||
.merge(password_reset::build())
|
.merge(password_reset::build())
|
||||||
|
.fallback(async || WebError::NotFound)
|
||||||
.layer(SetResponseHeaderLayer::if_not_present(
|
.layer(SetResponseHeaderLayer::if_not_present(
|
||||||
header::CONTENT_SECURITY_POLICY,
|
header::CONTENT_SECURITY_POLICY,
|
||||||
HeaderValue::from_static("default-src 'self'; img-src 'self' data:;"),
|
HeaderValue::from_static("default-src 'self'; img-src 'self' data:;"),
|
||||||
|
|
|
||||||
|
|
@ -2,17 +2,13 @@ use askama::Template;
|
||||||
use axum::{
|
use axum::{
|
||||||
Router,
|
Router,
|
||||||
extract::State,
|
extract::State,
|
||||||
response::{Html, IntoResponse, Redirect},
|
response::{Html, IntoResponse},
|
||||||
routing::get,
|
routing::get,
|
||||||
};
|
};
|
||||||
|
|
||||||
use crate::WebError;
|
use crate::WebError;
|
||||||
|
|
||||||
pub(crate) fn build() -> Router<crate::State> {
|
pub(crate) fn build() -> Router<crate::State> { Router::new().route("/", get(index_handler)) }
|
||||||
Router::new()
|
|
||||||
.route("/", get(async || Redirect::permanent("/_continuwuity/")))
|
|
||||||
.route("/_continuwuity/", get(index_handler))
|
|
||||||
}
|
|
||||||
|
|
||||||
async fn index_handler(
|
async fn index_handler(
|
||||||
State(services): State<crate::State>,
|
State(services): State<crate::State>,
|
||||||
|
|
|
||||||
|
|
@ -6,7 +6,6 @@ use axum::{
|
||||||
response::{Html, IntoResponse, Response},
|
response::{Html, IntoResponse, Response},
|
||||||
routing::get,
|
routing::get,
|
||||||
};
|
};
|
||||||
use conduwuit_service::password_reset::PASSWORD_RESET_PATH;
|
|
||||||
use serde::Deserialize;
|
use serde::Deserialize;
|
||||||
use validator::Validate;
|
use validator::Validate;
|
||||||
|
|
||||||
|
|
@ -54,7 +53,8 @@ form! {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn build() -> Router<crate::State> {
|
pub(crate) fn build() -> Router<crate::State> {
|
||||||
Router::new().route(PASSWORD_RESET_PATH, get(get_password_reset).post(post_password_reset))
|
Router::new()
|
||||||
|
.route("/account/reset_password", get(get_password_reset).post(post_password_reset))
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn password_reset_form(
|
async fn password_reset_form(
|
||||||
|
|
|
||||||
|
|
@ -2,7 +2,7 @@ use axum::Router;
|
||||||
|
|
||||||
pub(crate) fn build() -> Router<crate::State> {
|
pub(crate) fn build() -> Router<crate::State> {
|
||||||
Router::new().nest(
|
Router::new().nest(
|
||||||
"/_continuwuity/resources/",
|
"/resources/",
|
||||||
#[allow(unused_qualifications)]
|
#[allow(unused_qualifications)]
|
||||||
memory_serve::load!().index_file(None).into_router(),
|
memory_serve::load!().index_file(None).into_router(),
|
||||||
)
|
)
|
||||||
|
|
|
||||||
|
|
@ -21,7 +21,14 @@
|
||||||
\二つ
|
\二つ
|
||||||
</pre>
|
</pre>
|
||||||
<div class="panel">
|
<div class="panel">
|
||||||
<h1>Request error<small aria-hidden>(︶^︶)</small></h1>
|
<h1>
|
||||||
|
{% if status == StatusCode::NOT_FOUND %}
|
||||||
|
Not found
|
||||||
|
{% else %}
|
||||||
|
Request error
|
||||||
|
{% endif %}
|
||||||
|
<small aria-hidden>(︶^︶)</small>
|
||||||
|
</h1>
|
||||||
|
|
||||||
<pre><code>{{ error }}</code></pre>
|
<pre><code>{{ error }}</code></pre>
|
||||||
</div>
|
</div>
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue