Added webpage binary
This commit is contained in:
27
crates/webpage/Cargo.toml
Normal file
27
crates/webpage/Cargo.toml
Normal 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 }
|
||||
19
crates/webpage/src/cmd/mod.rs
Normal file
19
crates/webpage/src/cmd/mod.rs
Normal 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,
|
||||
}
|
||||
}
|
||||
}
|
||||
93
crates/webpage/src/cmd/serve.rs
Normal file
93
crates/webpage/src/cmd/serve.rs
Normal 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),
|
||||
}
|
||||
}
|
||||
}
|
||||
61
crates/webpage/src/main.rs
Normal file
61
crates/webpage/src/main.rs
Normal 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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user