diff options
Diffstat (limited to 'src/main.rs')
-rw-r--r-- | src/main.rs | 39 |
1 files changed, 31 insertions, 8 deletions
diff --git a/src/main.rs b/src/main.rs index 72abb0e..e0f20a1 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,6 +1,7 @@ use core::panic; use std::{net::SocketAddr, pin::Pin}; +use confindent::Confindent; use http_body_util::{BodyExt, Full}; use hyper::{ Request, Response, StatusCode, @@ -11,16 +12,33 @@ use hyper::{ use hyper_util::rt::TokioIo; use tokio::{net::TcpListener, process::Command, runtime::Runtime}; +#[derive(Clone, Debug)] +pub struct Settings { + script_filename: String, + env: Vec<(String, String)>, +} + +const CONF_DEFAULT: &str = "/etc/corgi.conf"; + fn main() { + let conf_path = std::env::args().nth(1).unwrap_or(String::from(CONF_DEFAULT)); + let conf = Confindent::from_file(conf_path).expect("failed to open conf"); + + let script = conf.child("Script").expect("no 'Script' key in conf"); + let settings = Settings { + script_filename: script.value_owned().expect("'Script' key has no value'"), + env: vec![], + }; + let rt = Runtime::new().unwrap(); - rt.block_on(async { run().await }); + rt.block_on(async { run(settings).await }); } -async fn run() { +async fn run(settings: Settings) { let addr = SocketAddr::from(([0, 0, 0, 0], 2562)); let listen = TcpListener::bind(addr).await.unwrap(); - let svc = Svc {}; + let svc = Svc { settings }; loop { let (stream, _caddr) = listen.accept().await.unwrap(); @@ -33,7 +51,9 @@ async fn run() { } #[derive(Clone, Debug)] -struct Svc {} +struct Svc { + settings: Settings, +} impl Service<Request<Incoming>> for Svc { type Response = Response<Full<Bytes>>; @@ -45,20 +65,23 @@ impl Service<Request<Incoming>> for Svc { Ok(Response::builder().body(Full::new(b.into())).unwrap()) } - Box::pin(async { Ok(Self::handle(req).await) }) + let settings = self.settings.clone(); + Box::pin(async { Ok(Self::handle(settings, req).await) }) } } impl Svc { - async fn handle(req: Request<Incoming>) -> Response<Full<Bytes>> { + async fn handle(settings: Settings, req: Request<Incoming>) -> Response<Full<Bytes>> { let method = req.method().as_str().to_ascii_uppercase(); let path = req.uri().path().to_owned(); let headers = req.headers().clone(); let body = req.into_body().collect().await.unwrap().to_bytes(); let content_length = body.len(); - let mut cmd = Command::new("/usr/lib/cgit/cgit.cgi"); - cmd.env("PATH_INFO", path).env("REQUEST_METHOD", method); + let mut cmd = Command::new(&settings.script_filename); + cmd.env("SCRIPT_NAME", settings.script_filename) + .env("PATH_INFO", path) + .env("REQUEST_METHOD", method); for (header, value) in headers { if let Some(header) = header { |