- main
- manifest
- deps
- main
- Install
- Run
use fastly::{Error, Request, Response};
use lazy_static::lazy_static;
use regex::Regex;
#[fastly::main]
fn main(req: Request) -> Result<Response, Error> {
log_fastly::init_simple("mylogs", log::LevelFilter::Info);
let url = req.get_url_str();
{
lazy_static! {
static ref RE: Regex = Regex::new(r"/status/(\d+)").unwrap();
}
if let Some(caps) = RE.captures(url) {
assert_eq!(&caps[1], "200");
log::info!("Captured status code using positional groups: {}", &caps[1]);
}
}
{
lazy_static! {
static ref RE: Regex = Regex::new(r"/status/(?P<code>\d+)").unwrap();
}
if let Some(caps) = RE.captures(url) {
assert_eq!(&caps["code"], "200");
log::info!("Captured status code using named groups: {}", &caps["code"]);
}
}
{
lazy_static! {
static ref RE: Regex = Regex::new(r"/(?:status|code)/(\d+)\?").unwrap();
}
if let Some(caps) = RE.captures(url) {
assert_eq!(&caps[1], "200");
log::info!("Captured status code using non-capturing groups: {}", &caps[1]);
}
}
{
lazy_static! {
static ref RE: Regex = Regex::new(r"[?&]aaa=([^&]*)").unwrap();
}
if let Some(caps) = RE.captures(url) {
assert_eq!(&caps[1], "foo");
log::info!("Captured aaa parameter using bracketed character class: {}", &caps[1]);
}
}
{
lazy_static! {
static ref RE: Regex = Regex::new(r"[\?&]a{2,6}=(foo|bar)(?:$|&)").unwrap();
}
if let Some(caps) = RE.captures(url) {
assert_eq!(&caps[1], "foo");
log::info!("Captured aaa parameter using specific repeat count and alternate options: {}", &caps[1]);
}
}
{
lazy_static! {
static ref RE: Regex = Regex::new(r"(?i)AaA=Foo").unwrap();
}
if let Some(caps) = RE.captures(url) {
assert_eq!(&caps[0], "aaa=foo");
log::info!("Case insensitive match using (?i)AaA=Foo: {}", &caps[0]);
}
}
Ok(Response::new())
}