Files
skubelb/src/main.rs

84 lines
2.7 KiB
Rust
Raw Normal View History

2025-03-30 00:09:49 -07:00
use std::sync::Mutex;
2025-03-29 22:44:06 -07:00
use clap::Parser;
2025-03-30 00:09:49 -07:00
use skubelb::Rewriter;
use skubelb::Server;
2025-03-29 22:44:06 -07:00
use env_logger::Env;
2025-03-30 00:09:49 -07:00
use log::{info, warn};
use anyhow::Result;
use rouille::{router, Request, Response};
2025-03-29 22:44:06 -07:00
/// Implements a HTTP server which allows clients to 'register'
/// themselves. Their IP address will be used to replace a
/// needle in a set of config files. This is intended to be
/// used as a low-cost way of enabling Kubernetes ingress
/// using nginx running on a machine that has a public port.
///
/// The needle is expected to be a dummy IP address; something
/// fairly unique. The goal is to replace nginx files, where
/// we often repeat lines if we want nginx to load balance between
/// multiple destination IPs.
#[derive(Parser, Debug)]
#[command(version, about, long_about = None)]
struct Args {
/// The needle that will be replaced. Anytime a line
/// is encountered with this needle, the line is dropped
/// and instead N lines (one per replacement) is added to
/// the output.
#[arg(short, long)]
rewrite_string: String,
/// The folder which contains the templates that
/// will be be searched for the needle.
#[arg(short, long)]
2025-03-30 00:09:49 -07:00
template_dir: String,
/// The symlink that should be updated each time the config changes.
///
/// Symlinks are used because file updates are not atomic.
#[arg(short, long)]
config_symlink: String,
2025-03-29 22:44:06 -07:00
2025-03-30 00:09:49 -07:00
/// Where to actually store the generated configs.
2025-03-29 22:44:06 -07:00
#[arg(short, long)]
2025-03-30 00:09:49 -07:00
workspace_dir: String,
#[arg(short, long, default_value_t = String::from("0.0.0.0:8080"))]
listen: String,
2025-03-29 22:44:06 -07:00
}
fn main() {
// Log info and above by default
env_logger::Builder::from_env(Env::default().default_filter_or("info")).init();
let args = Args::parse();
2025-03-30 00:09:49 -07:00
let rewriter = Rewriter::new(args.rewrite_string);
let server_impl = Mutex::new(Server::new(rewriter, args.workspace_dir, args.template_dir, args.config_symlink));
2025-03-29 22:44:06 -07:00
2025-03-30 00:09:49 -07:00
rouille::start_server(args.listen, move |request| {
info!("Processing request: {:?}", request);
match handle(request, &server_impl) {
Ok(resp) => resp,
Err(e) => {
warn!("{:?}", e);
Response{status_code: 500, ..Response::empty_400()}
}
}
});
2025-03-29 22:44:06 -07:00
}
2025-03-30 00:09:49 -07:00
fn handle(request: &Request, server_impl: &Mutex<Server>) -> Result<Response> {
router!(request,
(POST) (/register) => {
server_impl.lock().unwrap().register(request)?;
Ok(Response{status_code: 200, ..Response::empty_204()})
},
(DELETE) (/register) => {
server_impl.lock().unwrap().unregister(request)?;
Ok(Response{status_code: 200, ..Response::empty_204()})
},
_ => Ok(Response::empty_404()),
)
}