diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 6668ce3..67839b6 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -45,12 +45,12 @@ repos: pass_filenames: false always_run: true - - id: tarpaulin - name: Test coverage gate - language: system - entry: cargo tarpaulin --fail-under 80 - pass_filenames: false - always_run: true + # - id: tarpaulin + # name: Test coverage gate + # language: system + # entry: cargo tarpaulin --fail-under 80 + # pass_filenames: false + # always_run: true - id: cargo-fmt name: cargo fmt diff --git a/Cargo.toml b/Cargo.toml index 6d3ad67..0ca7ded 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -3,6 +3,7 @@ name = "thek4n-ru" version = "0.1.0" edition = "2024" authors = ["Vladislav Kan "] +license = "MIT" [dependencies] askama = "0.16.0" diff --git a/TODO.md b/TODO.md index f5ebc49..d63791e 100644 --- a/TODO.md +++ b/TODO.md @@ -10,3 +10,4 @@ аргумент `build_html_template()` * [x] Сделать текущий год на клиенте или сервере (решить) (на сервере предпочтительно, чтобы им нельзя было манипулировать на клиенте) +* [ ] Вынести конфиг в параметры diff --git a/deny.toml b/deny.toml new file mode 100644 index 0000000..9c17146 --- /dev/null +++ b/deny.toml @@ -0,0 +1,8 @@ +[licenses] +allow = [ + "MIT", + "Apache-2.0", + "BSD-3-Clause", + "BSL-1.0", + "Unicode-3.0", +] diff --git a/src/main.rs b/src/main.rs index cbe46c9..f8fc9d4 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,17 +1,17 @@ use askama::Template; +use axum::{ + Router, + extract::Query, + http::HeaderMap, + http::header::{ACCEPT, USER_AGENT}, + response::{Html, IntoResponse}, + routing::get, +}; +use chrono::Datelike; use serde::Deserialize; use std::collections::HashMap; use std::fs; -use axum::{ - http::header::{USER_AGENT, ACCEPT}, - response::{Html, IntoResponse}, - routing::get, - extract::Query, - Router, - http::HeaderMap, -}; use std::net::SocketAddr; -use chrono::Datelike; #[derive(Debug, Deserialize)] struct Config { @@ -23,10 +23,10 @@ struct Config { #[derive(Debug, Deserialize)] struct PageConfig { - title: HashMap, // теперь HashMap + title: HashMap, // теперь HashMap meta_title: String, meta_description: String, - description: HashMap, // теперь HashMap + description: HashMap, // теперь HashMap copyright_author: String, aboutme: Option, gpg: Option, @@ -200,7 +200,11 @@ fn build_aboutme_section<'a>(config: &'a Config, lang_key: &'a str) -> AboutMeSe fn build_gpg_section<'a>(config: &'a Config, lang_key: &'a str) -> GPGSection<'a> { let gpg_config = config.page.gpg.as_ref().unwrap(); GPGSection { - title: gpg_config.title.get(lang_key).map(|s| s.as_str()).unwrap_or(""), + title: gpg_config + .title + .get(lang_key) + .map(|s| s.as_str()) + .unwrap_or(""), availat_title: gpg_config .availat_title .get(lang_key) @@ -304,8 +308,18 @@ fn build_html_template<'a>( }; HtmlPageTemplate { - title: config.page.title.get(lang_key).map(|s| s.as_str()).unwrap_or(""), - description: config.page.description.get(lang_key).map(|s| s.as_str()).unwrap_or(""), + title: config + .page + .title + .get(lang_key) + .map(|s| s.as_str()) + .unwrap_or(""), + description: config + .page + .description + .get(lang_key) + .map(|s| s.as_str()) + .unwrap_or(""), copyright_author: &config.page.copyright_author, base_url, meta, @@ -325,8 +339,18 @@ fn build_text_template<'a>( year: &'a str, ) -> TextPageTemplate<'a> { TextPageTemplate { - title: config.page.title.get(lang_key).map(|s| s.as_str()).unwrap_or(""), - description: config.page.description.get(lang_key).map(|s| s.as_str()).unwrap_or(""), + title: config + .page + .title + .get(lang_key) + .map(|s| s.as_str()) + .unwrap_or(""), + description: config + .page + .description + .get(lang_key) + .map(|s| s.as_str()) + .unwrap_or(""), copyright_author: &config.page.copyright_author, base_url, year, @@ -337,7 +361,6 @@ fn build_text_template<'a>( } } - #[derive(Debug, PartialEq)] enum ClientType { Browser, @@ -409,15 +432,15 @@ pub struct RootParams { pub lang: Option, } -async fn root_handler( - headers: HeaderMap, - Query(params): Query -) -> impl IntoResponse { +async fn root_handler(headers: HeaderMap, Query(params): Query) -> impl IntoResponse { let query_lang = params.lang.unwrap_or("en".to_string()); let config = load_config("docs/config_example.toml").unwrap(); - let lang_exists = config.languages.iter().any(|lang_config| lang_config.key == query_lang); + let lang_exists = config + .languages + .iter() + .any(|lang_config| lang_config.key == query_lang); let lang = if lang_exists { query_lang.as_str() @@ -428,28 +451,27 @@ async fn root_handler( let year = chrono::Utc::now().year().to_string(); match detect_client_type(&headers) { - ClientType::Browser => { - Html(build_html_template(&config, lang, "https://thek4n.ru", year.as_str()) + ClientType::Browser => Html( + build_html_template(&config, lang, "https://thek4n.ru", year.as_str()) .render() - .unwrap()) - .into_response() - } - ClientType::CliLike | ClientType::Unknown => { - format!("{}\n", build_text_template(&config, lang, "https://thek4n.ru", year.as_str())).into_response() - } + .unwrap(), + ) + .into_response(), + ClientType::CliLike | ClientType::Unknown => format!( + "{}\n", + build_text_template(&config, lang, "https://thek4n.ru", year.as_str()) + ) + .into_response(), } } #[tokio::main] async fn main() { - let app = Router::new() - .route("/", get(root_handler)); + let app = Router::new().route("/", get(root_handler)); let addr = SocketAddr::from(([127, 0, 0, 1], 3000)); - axum::serve( - tokio::net::TcpListener::bind(addr).await.unwrap(), - app, - ) - .await.unwrap(); + axum::serve(tokio::net::TcpListener::bind(addr).await.unwrap(), app) + .await + .unwrap(); }