From 0e56a198c62ed9a9c1812cff1ffa618260535a35 Mon Sep 17 00:00:00 2001 From: Akshay Date: Mon, 24 Aug 2020 12:10:20 +0530 Subject: handle landing page --- src/service.rs | 27 ++++++++++++++++++++++++--- 1 file changed, 24 insertions(+), 3 deletions(-) diff --git a/src/service.rs b/src/service.rs index 35471ea..b7b204c 100644 --- a/src/service.rs +++ b/src/service.rs @@ -16,6 +16,26 @@ fn get_host(req: &Request) -> Option { return Some(host); } +fn welcome(req: Request) -> Response { + let _h = get_host(&req); + let host = _h.as_ref().map(|h| h.as_bytes()).unwrap_or(b""); + let text = format!( + " +This URL shortening services is powered by hedge. + + github.com/nerdypepper/hedge + +To shorten urls: + curl -F'shorten=https://shorten.some/long/url' {} + ", + String::from_utf8_lossy(host) + ); + return Response::builder() + .header("content-type", "text/plain") + .body(Body::from(text)) + .unwrap(); +} + fn respond_with_shortlink>(shortlink: S, host: &[u8]) -> Response { let url = [host, b"/", shortlink.as_ref()].concat(); info!("Successfully generated shortlink"); @@ -102,8 +122,8 @@ async fn process_form(req: Request, conn: &mut Connection) -> Result, mut conn: Connection) -> Result> { - match req.method() { - &Method::POST => { + match (req.method(), req.uri().path()) { + (&Method::POST, "/") => { let boundary = req .headers() .get(CONTENT_TYPE) @@ -117,7 +137,8 @@ pub async fn shortner_service(req: Request, mut conn: Connection) -> Resul trace!("Attempting to parse multipart request"); return process_multipart(req, boundary.unwrap(), &mut conn).await; } - &Method::GET => { + (&Method::GET, "/") => Ok(welcome(req)), + (&Method::GET, _) => { trace!("GET: {}", req.uri()); let shortlink = req.uri().path().to_string(); let link = get_link(&shortlink[1..], &conn); -- cgit v1.2.3