add: implement basic server

This commit is contained in:
2025-03-30 00:09:49 -07:00
parent 092658b039
commit eac109379e
5 changed files with 1109 additions and 13 deletions

999
Cargo.lock generated

File diff suppressed because it is too large Load Diff

View File

@@ -5,9 +5,11 @@ edition = "2024"
[dependencies]
anyhow = "1.0.97"
chrono = "0.4.40"
clap = { version = "4.5.34", features = ["derive"] }
dir-diff = "0.3.3"
env_logger = "0.11.7"
include_directory = "0.1.1"
log = "0.4.27"
rouille = "3.6.2"
tempdir = "0.3.7"

5
src/lib.rs Normal file
View File

@@ -0,0 +1,5 @@
mod rewriter;
mod server;
pub use rewriter::*;
pub use server::*;

View File

@@ -1,10 +1,14 @@
use std::sync::Mutex;
use clap::Parser;
mod rewriter;
use skubelb::Rewriter;
use skubelb::Server;
use env_logger::Env;
use log::info;
use rewriter::Rewriter;
use log::{info, warn};
use anyhow::Result;
use rouille::{router, Request, Response};
/// Implements a HTTP server which allows clients to 'register'
/// themselves. Their IP address will be used to replace a
@@ -29,11 +33,20 @@ struct Args {
/// The folder which contains the templates that
/// will be be searched for the needle.
#[arg(short, long)]
source_folder: String,
template_dir: String,
/// Where to write the replaced lines.
/// The symlink that should be updated each time the config changes.
///
/// Symlinks are used because file updates are not atomic.
#[arg(short, long)]
dest_folder: String,
config_symlink: String,
/// Where to actually store the generated configs.
#[arg(short, long)]
workspace_dir: String,
#[arg(short, long, default_value_t = String::from("0.0.0.0:8080"))]
listen: String,
}
fn main() {
@@ -41,11 +54,31 @@ fn main() {
env_logger::Builder::from_env(Env::default().default_filter_or("info")).init();
let args = Args::parse();
let mut rewriter = Rewriter::new(args.rewrite_string);
let rewriter = Rewriter::new(args.rewrite_string);
let server_impl = Mutex::new(Server::new(rewriter, args.workspace_dir, args.template_dir, args.config_symlink));
rewriter.add_replacement(String::from("abc"));
rewriter
.rewrite_folder(&args.source_folder, &args.dest_folder)
.unwrap();
info!("Finished writing new config to {}", args.dest_folder);
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()}
}
}
});
}
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()),
)
}

59
src/server.rs Normal file
View File

@@ -0,0 +1,59 @@
use std::{fs, path::Path, time::Instant};
use chrono::{Datelike, Utc};
use log::info;
use rouille::Request;
use anyhow::{Context, Result};
use crate::Rewriter;
pub struct Server {
rewriter: Rewriter,
// Where we write temporary files
workspace_dir: String,
// Directory to read configs from
template_dir: String,
// The symlink that is updated
config_dir: String,
}
impl Server {
pub fn new(rewriter: Rewriter, workspace_dir: String, template_dir: String, config_dir: String) -> Self {
Self {
rewriter,
workspace_dir,
template_dir,
config_dir,
}
}
pub fn register(&mut self, request: &Request) -> Result<()> {
let ip = request.remote_addr().ip().to_string();
info!("Registering {} as a handler", ip);
self.rewriter.add_replacement(ip);
self.generate_config()?;
Ok(())
}
pub fn unregister(&mut self, request: &Request) -> Result<()> {
let ip = request.remote_addr().ip().to_string();
info!("Deregistering {} as a handler", ip);
self.rewriter.remove_replacement(&ip);
Ok(())
}
pub fn generate_config(&self) -> Result<()> {
// Create a new directory in our workspace
let now = Utc::now();
// Writes into 2020/01/01/<unix timestamp>
// This will fail if we have multiple requests per second
let path = Path::new(&self.workspace_dir).join(&now.format("%Y/%m/%d/%s").to_string());
let path = path.as_os_str().to_str().unwrap();
fs::create_dir_all(path).with_context(|| "creating directory")?;
self.rewriter.rewrite_folder(&self.template_dir, path).with_context(|| "generating configs")?;
// Finally, symlink it to the output folder; only support Linux for now
fs::remove_file(&self.config_dir).with_context(|| "removing old symlink")?;
std::os::unix::fs::symlink(path, &self.config_dir).with_context(|| "updating symlink")?;
Ok(())
}
}