aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorAkshay <[email protected]>2020-08-24 07:40:20 +0100
committerAkshay <[email protected]>2020-08-24 07:40:20 +0100
commit0e56a198c62ed9a9c1812cff1ffa618260535a35 (patch)
tree0a904ce4656d7c18cd97a22556fc4da1e4a8322c /src
parent0a0952e6edb2f4a8c7883b661ef6d4bb4eb14405 (diff)
handle landing page
Diffstat (limited to 'src')
-rw-r--r--src/service.rs27
1 files 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<Body>) -> Option<HeaderValue> {
16 return Some(host); 16 return Some(host);
17} 17}
18 18
19fn welcome(req: Request<Body>) -> Response<Body> {
20 let _h = get_host(&req);
21 let host = _h.as_ref().map(|h| h.as_bytes()).unwrap_or(b"");
22 let text = format!(
23 "
24This URL shortening services is powered by hedge.
25
26 github.com/nerdypepper/hedge
27
28To shorten urls:
29 curl -F'shorten=https://shorten.some/long/url' {}
30 ",
31 String::from_utf8_lossy(host)
32 );
33 return Response::builder()
34 .header("content-type", "text/plain")
35 .body(Body::from(text))
36 .unwrap();
37}
38
19fn respond_with_shortlink<S: AsRef<[u8]>>(shortlink: S, host: &[u8]) -> Response<Body> { 39fn respond_with_shortlink<S: AsRef<[u8]>>(shortlink: S, host: &[u8]) -> Response<Body> {
20 let url = [host, b"/", shortlink.as_ref()].concat(); 40 let url = [host, b"/", shortlink.as_ref()].concat();
21 info!("Successfully generated shortlink"); 41 info!("Successfully generated shortlink");
@@ -102,8 +122,8 @@ async fn process_form(req: Request<Body>, conn: &mut Connection) -> Result<Respo
102} 122}
103 123
104pub async fn shortner_service(req: Request<Body>, mut conn: Connection) -> Result<Response<Body>> { 124pub async fn shortner_service(req: Request<Body>, mut conn: Connection) -> Result<Response<Body>> {
105 match req.method() { 125 match (req.method(), req.uri().path()) {
106 &Method::POST => { 126 (&Method::POST, "/") => {
107 let boundary = req 127 let boundary = req
108 .headers() 128 .headers()
109 .get(CONTENT_TYPE) 129 .get(CONTENT_TYPE)
@@ -117,7 +137,8 @@ pub async fn shortner_service(req: Request<Body>, mut conn: Connection) -> Resul
117 trace!("Attempting to parse multipart request"); 137 trace!("Attempting to parse multipart request");
118 return process_multipart(req, boundary.unwrap(), &mut conn).await; 138 return process_multipart(req, boundary.unwrap(), &mut conn).await;
119 } 139 }
120 &Method::GET => { 140 (&Method::GET, "/") => Ok(welcome(req)),
141 (&Method::GET, _) => {
121 trace!("GET: {}", req.uri()); 142 trace!("GET: {}", req.uri());
122 let shortlink = req.uri().path().to_string(); 143 let shortlink = req.uri().path().to_string();
123 let link = get_link(&shortlink[1..], &conn); 144 let link = get_link(&shortlink[1..], &conn);