Skip to content

Commit

Permalink
updating fastly version
Browse files Browse the repository at this point in the history
  • Loading branch information
BrooksCunningham committed Aug 15, 2024
1 parent 0d5cc9e commit c85fd12
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 52 deletions.
60 changes: 42 additions & 18 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@ publish = false
debug = 1

[dependencies]
fastly = "0.9.4"
fastly = "0.10.3"
serde_json = "1.0.107"
73 changes: 40 additions & 33 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
use fastly::handle::client_ip_addr;
#[allow(unused_imports)]
use fastly::http::StatusCode;
#[allow(unused_imports)]
use fastly::{Error, mime, KVStore, Request, Response};
use fastly::handle::client_ip_addr;
use fastly::{mime, Error, KVStore, Request, Response};
use serde_json::Value;
// use std::{thread, time};
use std::thread::sleep;
use std::io::Write;
use std::thread::sleep;
use std::time::Duration;

fn main() -> Result<(), Error> {
Expand All @@ -15,7 +15,6 @@ fn main() -> Result<(), Error> {

match server_resp.get_header_str("action-tarpit") {
Some(ep) if ep.contains("1") => {

let body = server_resp.take_body();
let mut streamer = server_resp.stream_to_client();
// The following code will force the client to wait for 1 second
Expand All @@ -24,9 +23,9 @@ fn main() -> Result<(), Error> {
let _ = streamer.write(chunk)?;
streamer.flush()?;
sleep(Duration::from_millis(1000));
};
return Ok(())
},
}
return Ok(());
}
_ => (),
};

Expand All @@ -50,9 +49,7 @@ fn handler(mut req: Request) -> Result<Response, Error> {
// TODOs
// Add do tarpitting in the response header in the handler function if tarpitting should occur. Get that header and tarpit based on some information in the main function.
match req.get_header_str("endpoint") {
Some(ep) if ep.contains("tarpit") => {
resp.set_header("action-tarpit", "1")
},
Some(ep) if ep.contains("tarpit") => resp.set_header("action-tarpit", "1"),
_ => (),
};

Expand All @@ -63,11 +60,11 @@ fn handler(mut req: Request) -> Result<Response, Error> {
s if s.starts_with("/forms/post") => return Ok(get_static_asset(&req, resp)?),

"/" => return Ok(swagger_ui_html(resp)?),

// Do nothing
_ => (),
};
return Ok::<fastly::Response, Error>(resp)
return Ok::<fastly::Response, Error>(resp);
}

fn anything(mut req: Request, mut resp: Response) -> Result<Response, Error> {
Expand Down Expand Up @@ -112,12 +109,16 @@ fn status(mut req: &Request, mut resp: Response) -> Result<Response, Error> {
status_str = ep.split("=").collect::<Vec<&str>>()[1];
status_parsed = status_str.parse::<u16>()?;
return status_result(status_parsed, resp);
},
_ => ()
}
_ => (),
}

let req_url = req.get_url();
let path_segments: Vec<&str> = req_url.path_segments().ok_or_else(|| "cannot be base").unwrap().collect();
let path_segments: Vec<&str> = req_url
.path_segments()
.ok_or_else(|| "cannot be base")
.unwrap()
.collect();

// If the path segment is too short, then just return a 500
if path_segments.len() < 2 {
Expand All @@ -138,32 +139,35 @@ fn status(mut req: &Request, mut resp: Response) -> Result<Response, Error> {
// https://docs.rs/fastly/latest/fastly/http/struct.StatusCode.html
resp.set_status(status_int);
Ok(resp)
},
}
_ => {
resp.set_status(500);
let data = serde_json::json!({ "error": "unable to parse status code properly. Try sending request like /status/302"});
let _ = resp.set_body_json(&data);
Ok(resp)
}
}
};
}
}

fn swagger_ui_html(mut resp: Response) -> Result<Response, Error> {
// Define a KV store instance using the resource link name
let store: KVStore = KVStore::open("assets_store")?.unwrap();
let store: KVStore = KVStore::open("assets_store")?.unwrap();

// Get the value back from the KV store (as a string),
let swagger_html: String = store.lookup_str("static-assets/swagger.html")?.unwrap();
// Get the value back from the KV store (as a string),
let swagger_html: String = store.lookup_str("static-assets/swagger.html")?.unwrap();

resp.set_body_text_html(&swagger_html);
return Ok(resp)
resp.set_body_text_html(&swagger_html);
return Ok(resp);
}

fn get_static_asset(req: &Request, mut resp: Response) -> Result<Response, Error> {

let req_url = req.get_url();
let path_segments: Vec<&str> = req_url.path_segments().ok_or_else(|| "cannot be base").unwrap().collect();
let path_segments: Vec<&str> = req_url
.path_segments()
.ok_or_else(|| "cannot be base")
.unwrap()
.collect();

let req_filename = path_segments.last().cloned().unwrap_or("Not Found");

Expand All @@ -172,15 +176,14 @@ fn get_static_asset(req: &Request, mut resp: Response) -> Result<Response, Error

// Get the value back from the KV store (as a string),
let req_filename_lookup = format!("static-assets/{}", &req_filename);
let static_asset: String = store.lookup_str(&req_filename_lookup)?.unwrap_or("Not Found".to_string());
let static_asset: String = store
.lookup_str(&req_filename_lookup)?
.unwrap_or("Not Found".to_string());

// using the set_body_text_plain since that accepts a &str value.
resp.set_body_text_plain(&static_asset);

let filename_parts = req_filename.split(".").collect::<Vec<&str>>();
let filename_ext = filename_parts.last().cloned().unwrap_or("html");
let static_filename_parts = req_filename.split(".").collect::<Vec<&str>>();
let static_filename_ext = static_filename_parts.last().cloned().unwrap_or("html");

match filename_ext {
match static_filename_ext {
"js" => resp.set_header("content-type", "application/javascript; charset=utf-8"),
"css" => resp.set_header("content-type", "text/css; charset=utf-8"),
"html" => resp.set_header("content-type", "text/html; charset=utf-8"),
Expand All @@ -190,9 +193,13 @@ fn get_static_asset(req: &Request, mut resp: Response) -> Result<Response, Error
_ => resp.set_body_text_plain(&static_asset),
};

return Ok(resp)
}
// using the set_body_text_plain since that accepts a &str value.
// Need to check the content-type before setting the type of body.
// resp.set_body_text_plain(&static_asset);
resp.set_body(static_asset.as_bytes());

return Ok(resp);
}

#[test]
fn test_homepage() {
Expand Down

0 comments on commit c85fd12

Please sign in to comment.