Added webpage binary

This commit is contained in:
2025-11-01 22:53:06 -07:00
parent ce830ae280
commit 5da2311583
6 changed files with 2940 additions and 0 deletions

2608
Cargo.lock generated Normal file

File diff suppressed because it is too large Load Diff

132
Cargo.toml Normal file
View File

@@ -0,0 +1,132 @@
[workspace]
members = ["crates/*"]
resolver = "2"
[workspace.package]
rust-version = "1.90.0"
edition = "2024"
version = "0.0.1"
[workspace.lints.rust]
unused_import_braces = "deny"
unit_bindings = "deny"
single_use_lifetimes = "deny"
non_ascii_idents = "deny"
macro_use_extern_crate = "deny"
elided_lifetimes_in_paths = "deny"
absolute_paths_not_starting_with_crate = "deny"
explicit_outlives_requirements = "warn"
unused_crate_dependencies = "warn"
redundant_lifetimes = "warn"
missing_docs = "allow"
[workspace.lints.clippy]
todo = "deny"
uninlined_format_args = "allow"
result_large_err = "allow"
too_many_arguments = "allow"
upper_case_acronyms = "deny"
needless_return = "allow"
new_without_default = "allow"
tabs_in_doc_comments = "allow"
dbg_macro = "deny"
allow_attributes = "deny"
create_dir = "deny"
filetype_is_file = "deny"
integer_division = "allow"
lossy_float_literal = "deny"
map_err_ignore = "deny"
mutex_atomic = "deny"
needless_raw_strings = "deny"
str_to_string = "deny"
string_add = "deny"
string_to_string = "deny"
use_debug = "allow"
verbose_file_reads = "deny"
large_types_passed_by_value = "deny"
wildcard_dependencies = "deny"
negative_feature_names = "deny"
redundant_feature_names = "deny"
multiple_crate_versions = "allow"
missing_safety_doc = "warn"
identity_op = "allow"
print_stderr = "deny"
print_stdout = "deny"
comparison_chain = "allow"
unimplemented = "deny"
unwrap_used = "warn"
expect_used = "warn"
type_complexity = "allow"
#
# MARK: dependencies
#
[workspace.dependencies]
assetserver = { path = "crates/assetserver" }
toolbox = { path = "crates/toolbox" }
libservice = { path = "crates/libservice" }
service-webpage = { path = "crates/service-webpage" }
macro-assets = { path = "crates/macro-assets" }
macro-sass = { path = "crates/macro-sass" }
#
# MARK: Servers
#
axum = { version = "0.8.6", features = ["macros", "multipart"] }
tower-http = { version = "0.6.6", features = ["trace"] }
utoipa = "5.4.0"
utoipa-swagger-ui = { version = "9.0.2", features = [
"axum",
"debug-embed",
"vendored",
] }
maud = { version = "0.27.0", features = ["axum"] }
grass = "0.13.4"
markdown = "1.0.0"
#
# MARK: Async & Parallelism
#
tokio = { version = "1.48.0", features = ["full"] }
#
# MARK: CLI & logging
#
tracing = "0.1.41"
tracing-subscriber = { version = "0.3.20", features = ["env-filter", "json"] }
tracing-loki = { version = "0.2.6", features = [
"rustls",
"compat-0-2-1",
], default-features = false }
clap = { version = "4.5.51", features = ["derive"] }
anstyle = { version = "1.0.13" }
#
# MARK: Serialization & formats
#
serde = { version = "1.0.228", features = ["derive"] }
serde_json = "1.0.145"
toml = "0.9.8"
base64 = "0.22.1"
#
# MARK: Misc helpers
#
strum = { version = "0.27", features = ["derive"] }
thiserror = "2.0.12"
itertools = "0.14.0"
anyhow = "1.0.97"
url = { version = "2.5.7", features = ["serde"] }
num = "0.4.3"
#
# Macro utilities
#
proc-macro2 = "1.0.95"
syn = "2.0.101"
quote = "1.0.40"
paste = "1.0.15"

27
crates/webpage/Cargo.toml Normal file
View File

@@ -0,0 +1,27 @@
[package]
name = "webpage"
version = { workspace = true }
rust-version = { workspace = true }
edition = { workspace = true }
# Only for ort
[profile.dev]
rpath = true
[profile.release]
rpath = true
[lints]
workspace = true
[dependencies]
toolbox = { workspace = true, features = ["cli", "loki"] }
libservice = { workspace = true }
service-webpage = { workspace = true }
tracing = { workspace = true }
tokio = { workspace = true }
axum = { workspace = true }
clap = { workspace = true }
anyhow = { workspace = true }

View File

@@ -0,0 +1,19 @@
use crate::CmdContext;
mod serve;
#[derive(Debug, clap::Subcommand)]
pub enum Command {
Serve {
#[command(flatten)]
cmd: serve::ServeArgs,
},
}
impl Command {
pub async fn run(self, ctx: CmdContext) -> anyhow::Result<()> {
match self {
Self::Serve { cmd } => cmd.run(ctx).await,
}
}
}

View File

@@ -0,0 +1,93 @@
use anyhow::{Context, Result};
use axum::{extract::connect_info::Connected, serve::IncomingStream};
use libservice::{Service, ToService};
use service_webpage::WebpageService;
use std::{net::SocketAddr, sync::Arc};
use tokio::net::TcpListener;
use tracing::{error, info};
use crate::CmdContext;
#[derive(Debug, clap::Args)]
pub struct ServeArgs {
/// IP and port to bind to
/// Should look like `127.0.0.1:3030`
addr: String,
}
impl ServeArgs {
pub async fn run(self, _ctx: CmdContext) -> Result<()> {
let state = Arc::new(RouterState {});
#[expect(clippy::expect_used)]
let app = make_service(Some(state.clone()))
.await
.context("while building service")?
.make_router()
.expect("service must be initialized")
.into_make_service_with_connect_info::<ServerConnectInfo>();
let listener = match tokio::net::TcpListener::bind(self.addr.clone()).await {
Ok(x) => x,
Err(error) => {
match error.kind() {
std::io::ErrorKind::AddrInUse => {
error!(
message = "Cannot bind to address, already in use",
server_addr = self.addr
);
}
_ => {
error!(message = "Error while starting server", ?error);
}
}
std::process::exit(1);
}
};
match listener.local_addr() {
Ok(x) => info!("listening on http://{x}"),
Err(error) => {
error!(message = "Could not determine local address", ?error);
std::process::exit(1);
}
}
axum::serve(listener, app)
.await
.context("in server thread")?;
return Ok(());
}
}
//
// MARK: state
//
#[derive(Debug, clap::Args, Clone)]
pub struct RouterState {}
/// If state is none, dry-init
pub async fn make_service(_state: Option<Arc<RouterState>>) -> Result<impl ToService> {
let service_webpage = WebpageService::new();
Ok(Service::new().merge(service_webpage).to_service().trace())
}
#[derive(Clone, Debug)]
pub struct ServerConnectInfo {
#[expect(dead_code)]
pub addr: Arc<SocketAddr>,
}
impl Connected<IncomingStream<'_, TcpListener>> for ServerConnectInfo {
fn connect_info(target: IncomingStream<'_, TcpListener>) -> Self {
let addr = target.remote_addr();
Self {
addr: Arc::new(*addr),
}
}
}

View File

@@ -0,0 +1,61 @@
use clap::Parser;
use toolbox::logging::{LogCliVQ, LoggingFormat, LoggingInitializer, LoggingTarget};
use tracing::error;
use crate::cmd::Command;
mod cmd;
#[derive(Parser, Debug)]
#[command(version, about, long_about = None, styles=toolbox::cli::clap_styles())]
struct Cli {
#[clap(flatten)]
vq: LogCliVQ,
/// Do not show progress bars
#[arg(long)]
noprogress: bool,
#[command(subcommand)]
command: Command,
}
#[derive(Debug)]
pub struct CmdContext {}
#[tokio::main]
async fn main() {
let cli = Cli::parse();
{
let res = LoggingInitializer {
app_name: "webpage",
loki: None,
preset: cli.vq.into_preset(),
target: LoggingTarget::Stderr {
format: LoggingFormat::Ansi,
},
}
.initialize();
if let Err(e) = res {
#[expect(clippy::print_stderr)]
for e in e.chain() {
eprintln!("{e}");
}
std::process::exit(1);
}
}
let ctx = CmdContext {};
let res = cli.command.run(ctx).await;
if let Err(e) = res {
for e in e.chain() {
error!("{e}");
}
std::process::exit(1);
}
}