-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.rs
90 lines (78 loc) · 3.2 KB
/
main.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#[macro_use]
extern crate quick_error;
mod rio;
use crate::rio::application::Application;
use crate::rio::configuration::{Configuration, ConfigurationError};
use crate::rio::logging::{Context, FastlyLogger};
use crate::rio::request_sender::{DirectRequestSender, RequestSender};
use fastly::{ConfigStore, Error, Request, Response};
#[fastly::main]
fn main(req: Request) -> Result<Response, Error> {
let start_time = std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.ok()
.map(|time| time.as_millis())
.unwrap_or(0);
let config_store = ConfigStore::open("redirectionio");
let req_sender = DirectRequestSender;
let fastly_logger = FastlyLogger::new(
config_store.get("log_endpoint"),
config_store.get("log_level"),
Context::new(req.clone_without_body()),
);
let config = match Configuration::new(
config_store.get("backend_name"),
config_store.get("token"),
config_store.get("instance_name"),
config_store.get("add_rule_ids_header"),
) {
Ok(config) => config,
Err(error) => {
return match error {
ConfigurationError::MissingBackendName => {
let message = format!("Fastly worker configuration error: {}.\n", error);
fastly_logger.log_error(message.clone(), None);
Ok(generate_synthetic_response(message, 500))
}
ConfigurationError::MissingToken(ref backend_name)
| ConfigurationError::MissingInstanceName(ref backend_name)
| ConfigurationError::MissingAddRuleIdsHeader(ref backend_name) => {
// The worked can not be configured: log an error and transparently forward the
// request to the backend with no changes
let message = format!("Fastly worker configuration error: {}.\n", error);
fastly_logger.log_error(message.clone(), None);
Ok(req_sender.send(req, backend_name.clone())?)
}
};
}
};
let application = Application::new(&config, &fastly_logger, &req_sender);
fastly_logger.log_info("Start worker".to_string(), None);
let rio_request = match application.create_rio_request(&req) {
Some(rio_request) => rio_request,
None => return Ok(req_sender.send(req, config.backend_name.clone())?),
};
let mut rio_action = match application.get_action(&rio_request) {
Some(rio_action) => rio_action,
None => return Ok(req_sender.send(req, config.backend_name.clone())?),
};
match application.proxy(req, &mut rio_action) {
Ok((response, backend_status_code)) => {
application.log(
&response,
backend_status_code,
&rio_request,
&mut rio_action,
start_time,
);
Ok(response)
}
Err(error) => Err(error),
}
}
fn generate_synthetic_response(error_message: String, status_code: u16) -> Response {
let mut response = Response::new();
response.set_body(error_message);
response.set_status(status_code);
return response;
}