// gumdrone: Order chewing gum from a website // // This program is free software: you can redistribute it and/or modify it // under the terms of the GNU Affero General Public License as published // by the Free Software Foundation, version 3. use std::io::prelude::*; const ORDERS_FILE: &str = "/var/lib/gumdrone/orders"; const ORDER_PAGE: &str = " Order!

Order Gum!

QtyProduct
Wrigley's Big Red
Wrigley's Doublemint
Wrigley's Eclipse Peppermint
Wrigley's Eclipse Spearmint
Wrigley's Eclipse Winterfrost
Wrigley's Extra Classic Bubble
Wrigley's Extra Peppermint
Wrigley's Extra Polar Ice
Wrigley's Extra Refreshers Polar Ice
Wrigley's Extra Refreshers Spearmint
Wrigley's Extra Spearmint
Wrigley's Extra Sweet Watermellon
Wrigley's Extra Winterfresh
Wrigley's Hubba Bubba Bubble Tape Original
Wrigley's Hubba Bubba Bubble Tape Sour Blue Raspberry
Wrigley's Hubba Bubba Max Original
Wrigley's Hubba Bubba Max Sour Blue Raspberry
Wrigley's Hubba Bubba Max Strawberry Watermellon
Wrigley's Juicy Fruit
Wrigley's Spearmint
Wrigley's Winterfresh
Address:
"; fn prompt_for_order(_: &cgi::Request) -> Result { Ok(cgi::html_response(200, ORDER_PAGE)) } fn write_order(order: &[u8]) -> std::io::Result<()> { let datum = [order, b"\n"].concat(); let ofile = std::fs::File::options() .append(true) .create(true) .open(ORDERS_FILE)?; let mut vlock = fd_lock::RwLock::new(ofile); vlock.write()?.write_all(&datum)?; Ok(()) } fn record_order(request: &cgi::Request) -> Result { let body = request.body(); write_order(body).map_err(|_| cgi::text_response(503, "Couldn't record order"))?; Ok(cgi::text_response(200, "Order recorded")) } fn strip_body(mut response: cgi::Response) -> cgi::Response { response.body_mut().clear(); response } fn respond(request: &cgi::Request) -> Result { match *request.method() { cgi::http::Method::HEAD => prompt_for_order(request).map(strip_body), cgi::http::Method::GET => prompt_for_order(request), cgi::http::Method::PUT => record_order(request), _ => Err(cgi::text_response(405, "Huh?")), } } fn respond_or_report_error(request: cgi::Request) -> cgi::Response { match respond(&request) { Ok(result) => result, Err(error) => error, } } cgi::cgi_main! { respond_or_report_error }