base html tamplates & routes

This commit is contained in:
2026-03-28 13:52:02 +03:00
parent cccb1bd694
commit 895a301ace
9 changed files with 252 additions and 10 deletions

25
src/config.rs Normal file
View File

@@ -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<Self, Box<dyn std::error::Error>> {
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<dyn std::error::Error>> {
let yaml_string = serde_yaml::to_string(self)?;
fs::write(path, yaml_string)?;
Ok(())
}
}

View File

@@ -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<dyn std::error::Error>> {
// Загружаем конфиг
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(())
}