init httpd crate skeleton

This commit is contained in:
yggverse 2026-03-05 15:41:15 +02:00
parent 470a7d0e73
commit 802e0821e4
13 changed files with 1877 additions and 432 deletions

19
crates/httpd/Cargo.toml Normal file
View file

@ -0,0 +1,19 @@
[package]
name = "hlstate-httpd"
version = "0.1.0"
edition = "2024"
license = "MIT"
readme = "README.md"
description = "Web stats for Xash3D servers, based on Rocket engine"
keywords = ["hlstate", "half-life", "xash3d", "http", "server"]
categories = ["parsing", "text-processing", "value-formatting"]
repository = "https://github.com/YGGverse/hlstate-rs"
[dependencies]
chrono = { version = "0.4.41", features = ["serde"] }
clap = { version = "4.5.54", features = ["derive"] }
#mysql = { package = "hlstate-mysql", version = "0.1.0", path = "../mysql" }
rocket = "0.5.1"
rocket_dyn_templates = { version = "0.2.0", features = ["tera"] }
serde = { version = "1.0.228", features = ["derive"] }
toml = "0.9.10"

21
crates/httpd/LICENSE Normal file
View file

@ -0,0 +1,21 @@
MIT License
Copyright (c) 2026 YGGverse
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

11
crates/httpd/README.md Normal file
View file

@ -0,0 +1,11 @@
# hlstate-httpd
Web server implementation based on the Rocket engine
> [!NOTE]
> In development!
```
cd crates/httpd
cargo run -- -c config.toml
```

12
crates/httpd/config.toml Normal file
View file

@ -0,0 +1,12 @@
title = "HLState"
format_time = "%d/%m/%Y %H:%M"
# Bind server on given host
host = "127.0.0.1"
# Bind server on given port
port = 8000
#Configure instance in the debug mode
debug = true

View file

@ -0,0 +1,12 @@
use clap::Parser;
use std::path::PathBuf;
#[derive(Parser, Debug)]
#[command(version, about, long_about = None)]
pub struct Argument {
/// Path to config file
///
/// * see `config.toml`
#[arg(short, long)]
pub config: PathBuf,
}

View file

@ -0,0 +1,12 @@
use serde::Deserialize;
use std::net::IpAddr;
#[derive(Debug, Deserialize)]
pub struct Config {
pub title: String,
pub description: Option<String>,
pub format_time: String,
pub host: IpAddr,
pub port: u16,
pub debug: bool,
}

View file

@ -0,0 +1,7 @@
use rocket::serde::Serialize;
#[derive(Clone, Debug, Serialize)]
#[serde(crate = "rocket::serde")]
pub struct Global {
pub format_time: String,
}

64
crates/httpd/src/main.rs Normal file
View file

@ -0,0 +1,64 @@
#[macro_use]
extern crate rocket;
mod argument;
mod config;
mod global;
mod meta;
use chrono::{DateTime, Utc};
use global::Global;
use meta::Meta;
use rocket::{State, http::Status, serde::Serialize};
use rocket_dyn_templates::{Template, context};
#[get("/")]
fn index(meta: &State<Meta>, global: &State<Global>) -> Result<Template, Status> {
#[derive(Serialize)]
#[serde(crate = "rocket::serde")]
struct Server {
name: String,
}
let servers: Vec<Server> = Vec::new();
Ok(Template::render(
"index",
context! {
title: &meta.title,
servers: servers,
},
))
}
#[launch]
fn rocket() -> _ {
use clap::Parser;
let argument = argument::Argument::parse();
let config: config::Config =
toml::from_str(&std::fs::read_to_string(argument.config).unwrap()).unwrap();
rocket::build()
.attach(Template::fairing())
.configure(rocket::Config {
port: config.port,
address: config.host,
..if config.debug {
rocket::Config::debug_default()
} else {
rocket::Config::release_default()
}
})
.manage(Global {
format_time: config.format_time,
})
.manage(Meta {
description: config.description,
title: config.title,
version: env!("CARGO_PKG_VERSION").into(),
})
.mount("/", routes![index])
}
const S: &str = "";
fn time(timestamp: i64) -> DateTime<Utc> {
DateTime::<Utc>::from_timestamp(timestamp, 0).unwrap()
}

9
crates/httpd/src/meta.rs Normal file
View file

@ -0,0 +1,9 @@
use rocket::serde::Serialize;
#[derive(Clone, Debug, Serialize)]
#[serde(crate = "rocket::serde")]
pub struct Meta {
pub description: Option<String>,
pub title: String,
pub version: String,
}

View file

@ -0,0 +1,14 @@
{% extends "layout" %}
{% block content %}
{% if servers %}
{% for server in servers %}
<div>
<h2><a href="{{ server.host }}">{{ server.name }}</a></h2>
</div>
{% endfor %}
{% else %}
<div>
<p>Nobody.</p>
</div>
{% endif %}
{% endblock content %}

View file

@ -0,0 +1,18 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<title>{{ title }}</title>
<style>
* {color-scheme: light dark}
</style>
</head>
<body>
<header>
<h1><a href="/">{{ title }}</a></h1>
</header>
<main>
{% block content %}{% endblock content %}
</main>
</body>
</html>