mirror of
https://github.com/YGGverse/hlstate-rs.git
synced 2026-04-01 01:25:38 +00:00
init httpd crate skeleton
This commit is contained in:
parent
470a7d0e73
commit
802e0821e4
13 changed files with 1877 additions and 432 deletions
12
crates/httpd/src/argument.rs
Normal file
12
crates/httpd/src/argument.rs
Normal 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,
|
||||
}
|
||||
12
crates/httpd/src/config.rs
Normal file
12
crates/httpd/src/config.rs
Normal 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,
|
||||
}
|
||||
7
crates/httpd/src/global.rs
Normal file
7
crates/httpd/src/global.rs
Normal 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
64
crates/httpd/src/main.rs
Normal 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
9
crates/httpd/src/meta.rs
Normal 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,
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue