Add favicon
This commit is contained in:
parent
a590d7e1e9
commit
b2572ebfe5
9 changed files with 73 additions and 22 deletions
|
|
@ -13,7 +13,10 @@ use chrono::prelude::*;
|
|||
use clap::Parser;
|
||||
use config::Config;
|
||||
use itertools::Itertools;
|
||||
use tower_http::{services::ServeDir, trace::TraceLayer};
|
||||
use tower_http::{
|
||||
services::{ServeDir, ServeFile},
|
||||
trace::TraceLayer,
|
||||
};
|
||||
use tracing::{Level, event};
|
||||
|
||||
use data::{db, model};
|
||||
|
|
@ -85,7 +88,7 @@ async fn main() {
|
|||
.with_max_level(TRACING_LEVEL)
|
||||
.init();
|
||||
|
||||
if process_args().await {
|
||||
if !process_args().await {
|
||||
return;
|
||||
}
|
||||
|
||||
|
|
@ -254,22 +257,22 @@ async fn main() {
|
|||
"/user/edit",
|
||||
get(services::user::edit_user_get).post(services::user::edit_user_post),
|
||||
)
|
||||
.nest("/fragments", fragments_routes)
|
||||
.route_layer(middleware::from_fn(services::ron_error_to_html));
|
||||
|
||||
let app = Router::new()
|
||||
.merge(html_routes)
|
||||
.nest("/fragments", fragments_routes)
|
||||
.nest("/ron-api", ron_api_routes)
|
||||
.fallback(services::not_found)
|
||||
.layer(TraceLayer::new_for_http())
|
||||
// FIXME: Should be 'route_layer' but it doesn't work for 'fallback(..)'.
|
||||
.layer(middleware::from_fn(translation))
|
||||
.layer(middleware::from_fn_with_state(
|
||||
state.clone(),
|
||||
user_authentication,
|
||||
))
|
||||
.nest_service("/static", ServeDir::new("static"))
|
||||
.with_state(state)
|
||||
.nest_service("/favicon.ico", ServeFile::new("static/favicon.ico"))
|
||||
.nest_service("/static", ServeDir::new("static"))
|
||||
.layer(TraceLayer::new_for_http())
|
||||
.into_make_service_with_connect_info::<SocketAddr>();
|
||||
|
||||
let addr = SocketAddr::from(([0, 0, 0, 0], port));
|
||||
|
|
@ -291,11 +294,53 @@ async fn user_authentication(
|
|||
Ok(next.run(req).await)
|
||||
}
|
||||
|
||||
/// The language of the current HTTP request is defined in the current order:
|
||||
/// - Extraction from the url: like in '/fr/recipe/view/42' (Not yet implemented).
|
||||
/// - Get from the user database record.
|
||||
/// - Get from the cookie.
|
||||
/// - Get from the HTTP header `accept-language`.
|
||||
/// - Set as `translation::DEFAULT_LANGUAGE_CODE`.
|
||||
async fn translation(
|
||||
Extension(user): Extension<Option<model::User>>,
|
||||
mut req: Request,
|
||||
next: Next,
|
||||
) -> Result<Response> {
|
||||
// Here we are extracting the language from the url then rewriting it.
|
||||
// For example:
|
||||
// "/fr/recipe/view/1"
|
||||
// lang = "fr" and uri rewritten as = "/recipe/view/1"
|
||||
// Disable because it doesn't work at this level, see:
|
||||
// https://docs.rs/axum/latest/axum/middleware/index.html#rewriting-request-uri-in-middleware
|
||||
|
||||
// let lang_and_new_uri = 'lang_from_uri: {
|
||||
// if let Some(path_query) = req.uri().path_and_query() {
|
||||
// event!(Level::INFO, "path: {:?}", path_query.path());
|
||||
// let mut parts = path_query.path().split('/');
|
||||
// let _ = parts.next(); // Empty part due to the first '/'.
|
||||
// if let Some(lang) = parts.next() {
|
||||
// let available_codes = translation::available_codes();
|
||||
// if available_codes.contains(&lang) {
|
||||
// let mut rest: String = String::from("");
|
||||
// for part in parts {
|
||||
// rest.push('/');
|
||||
// rest.push_str(part);
|
||||
// }
|
||||
// // let uri_builder = Uri::builder()
|
||||
// if let Ok(new_uri) = rest.parse::<Uri>() {
|
||||
// event!(Level::INFO, "path rewrite: {:?}", new_uri.path());
|
||||
// break 'lang_from_uri Some((lang.to_string(), new_uri));
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
// None
|
||||
// };
|
||||
// let language = if let Some((lang, uri)) = lang_and_new_uri {
|
||||
// *req.uri_mut() = uri; // Replace the URI without the language.
|
||||
// event!(Level::INFO, "URI: {:?}", req.uri());
|
||||
// lang
|
||||
// } else
|
||||
|
||||
let language = if let Some(user) = user {
|
||||
user.lang
|
||||
} else {
|
||||
|
|
@ -370,6 +415,7 @@ struct Args {
|
|||
dbtest: bool,
|
||||
}
|
||||
|
||||
/// Returns `true` if the server can be started.
|
||||
async fn process_args() -> bool {
|
||||
let args = Args::parse();
|
||||
|
||||
|
|
@ -418,8 +464,8 @@ async fn process_args() -> bool {
|
|||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
return false;
|
||||
}
|
||||
|
||||
false
|
||||
true
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue