From 895a301ace545b5caa3b652dc36b36cd5306ed59 Mon Sep 17 00:00:00 2001 From: FIFIFiSKA Date: Sat, 28 Mar 2026 13:52:02 +0300 Subject: [PATCH] base html tamplates & routes --- Cargo.lock | 111 +++++++++++++++++++++++++++++++++++++++++++ Cargo.toml | 5 +- "\\" | 25 ++++++++++ askama.toml | 2 + config.yaml | 3 ++ src/config.rs | 25 ++++++++++ src/main.rs | 61 ++++++++++++++++++++---- templates/index.html | 15 ++++++ templates/prefs.html | 15 ++++++ 9 files changed, 252 insertions(+), 10 deletions(-) create mode 100644 "\\" create mode 100644 askama.toml create mode 100644 config.yaml create mode 100644 src/config.rs create mode 100644 templates/index.html create mode 100644 templates/prefs.html diff --git a/Cargo.lock b/Cargo.lock index e5fed41..0b2371f 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2,6 +2,48 @@ # It is not intended for manual editing. version = 4 +[[package]] +name = "askama" +version = "0.14.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f75363874b771be265f4ffe307ca705ef6f3baa19011c149da8674a87f1b75c4" +dependencies = [ + "askama_derive", + "itoa", + "percent-encoding", + "serde", + "serde_json", +] + +[[package]] +name = "askama_derive" +version = "0.14.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "129397200fe83088e8a68407a8e2b1f826cf0086b21ccdb866a722c8bcd3a94f" +dependencies = [ + "askama_parser", + "basic-toml", + "memchr", + "proc-macro2", + "quote", + "rustc-hash", + "serde", + "serde_derive", + "syn", +] + +[[package]] +name = "askama_parser" +version = "0.14.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d6ab5630b3d5eaf232620167977f95eb51f3432fc76852328774afbd242d4358" +dependencies = [ + "memchr", + "serde", + "serde_derive", + "winnow", +] + [[package]] name = "atomic-waker" version = "1.1.2" @@ -60,6 +102,15 @@ dependencies = [ "tracing", ] +[[package]] +name = "basic-toml" +version = "0.1.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ba62675e8242a4c4e806d12f11d136e626e6c8361d6b829310732241652a178a" +dependencies = [ + "serde", +] + [[package]] name = "bitflags" version = "2.11.0" @@ -82,10 +133,19 @@ checksum = "9330f8b2ff13f34540b44e946ef35111825727b38d33286ef986142615121801" name = "dashcore" version = "0.1.0" dependencies = [ + "askama", "axum", + "serde", + "serde_yaml", "tokio", ] +[[package]] +name = "equivalent" +version = "1.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "877a4ace8713b0bcf2a4e7eec82529c029f1d0619886d18145fea96c3ffe5c0f" + [[package]] name = "errno" version = "0.3.14" @@ -138,6 +198,12 @@ dependencies = [ "slab", ] +[[package]] +name = "hashbrown" +version = "0.16.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "841d1cc9bed7f9236f321df977030373f4a4163ae1a7dbfe1a51a2c1a51d9100" + [[package]] name = "http" version = "1.4.0" @@ -219,6 +285,16 @@ dependencies = [ "tower-service", ] +[[package]] +name = "indexmap" +version = "2.13.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7714e70437a7dc3ac8eb7e6f8df75fd8eb422675fc7678aff7364301092b1017" +dependencies = [ + "equivalent", + "hashbrown", +] + [[package]] name = "itoa" version = "1.0.18" @@ -349,6 +425,12 @@ dependencies = [ "bitflags", ] +[[package]] +name = "rustc-hash" +version = "2.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "94300abf3f1ae2e2b8ffb7b58043de3d399c73fa6f4b73826402a5c457614dbe" + [[package]] name = "ryu" version = "1.0.23" @@ -368,6 +450,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9a8e94ea7f378bd32cbbd37198a4a91436180c5bb472411e48b5ec2e2124ae9e" dependencies = [ "serde_core", + "serde_derive", ] [[package]] @@ -426,6 +509,19 @@ dependencies = [ "serde", ] +[[package]] +name = "serde_yaml" +version = "0.9.34+deprecated" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6a8b1a1a2ebf674015cc02edccce75287f1a0130d394307b36743c2f5d504b47" +dependencies = [ + "indexmap", + "itoa", + "ryu", + "serde", + "unsafe-libyaml", +] + [[package]] name = "signal-hook-registry" version = "1.4.8" @@ -557,6 +653,12 @@ version = "1.0.24" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e6e4313cd5fcd3dad5cafa179702e2b244f760991f45397d14d4ebf38247da75" +[[package]] +name = "unsafe-libyaml" +version = "0.2.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "673aac59facbab8a9007c7f6108d11f63b603f7cabff99fabf650fea5c32b861" + [[package]] name = "wasi" version = "0.11.1+wasi-snapshot-preview1" @@ -578,6 +680,15 @@ dependencies = [ "windows-link", ] +[[package]] +name = "winnow" +version = "0.7.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "df79d97927682d2fd8adb29682d1140b343be4ac0f08fd68b7765d9c059d3945" +dependencies = [ + "memchr", +] + [[package]] name = "zmij" version = "1.0.21" diff --git a/Cargo.toml b/Cargo.toml index 512427c..b5cdbf0 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,8 +1,11 @@ [package] name = "dashcore" version = "0.1.0" -edition = "2024" +edition = "2021" [dependencies] axum = "0.8.8" +serde = { version = "1.0.228", features = ["derive"] } +serde_yaml = "0.9.34" tokio = { version = "1", features = ["full", "rt-multi-thread"] } +askama = "0.14" diff --git "a/\\" "b/\\" new file mode 100644 index 0000000..c3a8064 --- /dev/null +++ "b/\\" @@ -0,0 +1,25 @@ +use serde::{Deserialize, Serialize}; +use std::fs; + +#[derive(Debug, Serialize, Deserialize)] +pub struct Config { + pub host: String, + pub port: u16, + pub debug: bool, +} + +impl Config { + // Чтение из YAML + pub fn load(path: &str) -> Result> { + let contents = fs::read_to_string(path)?; + let config: Config = serde_yaml::from_str(&contents)?; + Ok(config) + } + + // Запись в YAML + pub fn save(&self, path: &str) -> Result<(), Box> { + let yaml_string = serde_yaml::to_string(self)?; + fs::write(path, yaml_string)?; + Ok(()) + } +} diff --git a/askama.toml b/askama.toml new file mode 100644 index 0000000..e199e1a --- /dev/null +++ b/askama.toml @@ -0,0 +1,2 @@ +[general] +# По умолчанию шаблоны ищутся в директории templates/ diff --git a/config.yaml b/config.yaml new file mode 100644 index 0000000..edba2a6 --- /dev/null +++ b/config.yaml @@ -0,0 +1,3 @@ +host: "127.0.0.1" +port: 8080 +debug: true diff --git a/src/config.rs b/src/config.rs new file mode 100644 index 0000000..c3a8064 --- /dev/null +++ b/src/config.rs @@ -0,0 +1,25 @@ +use serde::{Deserialize, Serialize}; +use std::fs; + +#[derive(Debug, Serialize, Deserialize)] +pub struct Config { + pub host: String, + pub port: u16, + pub debug: bool, +} + +impl Config { + // Чтение из YAML + pub fn load(path: &str) -> Result> { + let contents = fs::read_to_string(path)?; + let config: Config = serde_yaml::from_str(&contents)?; + Ok(config) + } + + // Запись в YAML + pub fn save(&self, path: &str) -> Result<(), Box> { + let yaml_string = serde_yaml::to_string(self)?; + fs::write(path, yaml_string)?; + Ok(()) + } +} diff --git a/src/main.rs b/src/main.rs index fb0e561..4bf6542 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,15 +1,58 @@ use axum::{ routing::get, Router, + response::{Html, IntoResponse}, }; -const host:&str = "0.0.0.0:8080"; -#[tokio::main] -async fn main() { - println!("dash.core is running on"); - // build our application with a single route - let app = Router::new().route("/", get(|| async { "dashcore is running" })); +use askama::Template; - // run our app with hyper, listening globally on port 3000 - let listener = tokio::net::TcpListener::bind(host).await.unwrap(); - axum::serve(listener, app).await.unwrap(); +mod config; +use crate::config::Config; + +#[derive(Template)] +#[template(path = "index.html")] +struct IndexTemplate { + title: String, + message: String, +} + +#[derive(Template)] +#[template(path = "prefs.html")] +struct PrefsTemplate { + title: String, + description: String, +} + +async fn index() -> impl IntoResponse { + let template = IndexTemplate { + title: "dashcore".to_string(), + message: "Добро пожаловать в dashcore!".to_string(), + }; + Html(template.render().unwrap()) +} + +async fn about() -> impl IntoResponse { + let template = PrefsTemplate { + title: "Prefs".to_string(), + description: "настройки хоста".to_string(), + }; + Html(template.render().unwrap()) +} + +#[tokio::main] +async fn main() -> Result<(), Box> { + // Загружаем конфиг + let cfg = Config::load("config.yaml")?; + let addr = format!("{}:{}", cfg.host, cfg.port); + println!("dash.core is running on {}", addr); + + // Build our application with routes + let app = Router::new() + .route("/", get(index)) + .route("/prefs", get(about)); + + // Запускаем сервер + let listener = tokio::net::TcpListener::bind(&addr).await?; + axum::serve(listener, app).await?; + + Ok(()) } diff --git a/templates/index.html b/templates/index.html new file mode 100644 index 0000000..d4dc8be --- /dev/null +++ b/templates/index.html @@ -0,0 +1,15 @@ + + + + + {{ title }} + + +

{{ title }}

+

{{ message }}

+ + + diff --git a/templates/prefs.html b/templates/prefs.html new file mode 100644 index 0000000..f618226 --- /dev/null +++ b/templates/prefs.html @@ -0,0 +1,15 @@ + + + + + {{ title }} + + +

{{ title }}

+

{{ description }}

+ + +