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 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226
/*! A crate to allow running a [Rocket](https://rocket.rs/) webserver as an AWS Lambda Function with API Gateway, built on the [AWS Lambda Rust Runtime](https://github.com/awslabs/aws-lambda-rust-runtime). The function takes a request from an AWS API Gateway Proxy and converts it into a `LocalRequest` to pass to Rocket. Then it will convert the response from Rocket into the response body that API Gateway understands. This *should* also work with requests from an AWS Application Load Balancer, but this has not been tested. ## Usage ```rust,no_run #![feature(proc_macro_hygiene, decl_macro)] #[macro_use] extern crate rocket; use rocket_lamb::RocketExt; #[get("/")] fn hello() -> &'static str { "Hello, world!" } fn main() { rocket::ignite() .mount("/hello", routes![hello]) .lambda() // launch the Rocket as a Lambda .launch(); } ``` */ #[macro_use] extern crate failure; #[macro_use] mod error; mod handler; pub use handler::RocketHandler; use lambda_http::lambda; use rocket::error::LaunchError; use rocket::local::Client; use rocket::Rocket; use std::collections::HashMap; /// Used to determine how to encode response content. The default is `Text`. #[derive(PartialEq, Copy, Clone, Debug)] pub enum ResponseType { /// Send response content to API Gateway as a UTF-8 string. Text, /// Send response content to API Gateway Base64-encoded. Binary, } /// Extensions for `rocket::Rocket` to make it easier to crate Lambda handlers. pub trait RocketExt { /// Create a new `RocketLamb` from the given `Rocket`. /// /// # Example /// /// ```rust /// use rocket_lamb::RocketExt; /// /// let lamb = rocket::ignite().lambda(); /// ``` fn lambda(self) -> RocketLamb; } impl RocketExt for Rocket { fn lambda(self) -> RocketLamb { RocketLamb::new(self) } } /// A wrapper around a [rocket::Rocket] that can be used to handle Lambda events. pub struct RocketLamb { rocket: Rocket, default_response_type: ResponseType, response_types: HashMap<String, ResponseType>, } impl RocketLamb { /// Create a new `RocketLamb`. Alternatively, you can use [rocket.lambda()](RocketExt::lambda). /// /// # Example /// /// ```rust /// use rocket_lamb::RocketLamb; /// /// let lamb = RocketLamb::new(rocket::ignite()); /// ``` pub fn new(rocket: rocket::Rocket) -> RocketLamb { RocketLamb { rocket, default_response_type: ResponseType::Text, response_types: HashMap::new(), } } /// Creates a new `RocketHandler` from an instance of `Rocket`, which can be passed to the [lambda_http::lambda!](lambda_http::lambda) macro. /// /// Alternatively, you can use the [launch()](RocketLamb::launch) method. /// /// # Errors /// /// If launching the `Rocket` instance would fail, excepting network errors, the `LaunchError` is returned. /// /// # Example /// /// ```rust,no_run /// use rocket_lamb::RocketExt; /// use lambda_http::lambda; /// /// let handler = rocket::ignite().lambda().into_handler()?; /// lambda!(handler); /// # Ok::<(), rocket::error::LaunchError>(()) /// ``` pub fn into_handler(self) -> Result<RocketHandler, LaunchError> { let client = Client::untracked(self.rocket)?; Ok(RocketHandler { client, default_response_type: self.default_response_type, response_types: self.response_types, }) } /// Starts handling Lambda events. /// /// # Errors /// /// If launching the `Rocket` instance fails, the `LaunchError` is returned. /// /// # Panics /// /// This panics if the required Lambda runtime environment variables are not set. /// /// # Example /// /// ```rust,no_run /// use rocket_lamb::RocketExt; /// use lambda_http::lambda; /// /// rocket::ignite().lambda().launch(); /// ``` pub fn launch(self) -> LaunchError { match self.into_handler() { Ok(h) => lambda!(h), Err(e) => return e, }; unreachable!("lambda! should loop forever (or panic)") } /// Gets the default `ResponseType`, which is used for any responses that have not had their Content-Type overriden with [response_type](RocketLamb::response_type). /// /// # Example /// /// ```rust /// use rocket_lamb::{RocketExt, ResponseType}; /// /// let lamb = rocket::ignite().lambda(); /// assert_eq!(lamb.get_default_response_type(), ResponseType::Text); /// assert_eq!(lamb.get_response_type("text/plain"), ResponseType::Text); /// ``` pub fn get_default_response_type(&self) -> ResponseType { self.default_response_type } /// Sets the default `ResponseType`, which is used for any responses that have not had their Content-Type overriden with [response_type](RocketLamb::response_type). /// /// # Example /// /// ```rust /// use rocket_lamb::{RocketExt, ResponseType}; /// /// let lamb = rocket::ignite() /// .lambda() /// .default_response_type(ResponseType::Binary); /// assert_eq!(lamb.get_default_response_type(), ResponseType::Binary); /// assert_eq!(lamb.get_response_type("text/plain"), ResponseType::Binary); /// ``` pub fn default_response_type(mut self, response_type: ResponseType) -> Self { self.default_response_type = response_type; self } /// Gets the configured `ResponseType` for responses with the given Content-Type header. /// /// `content_type` values are treated case-insensitively. /// /// # Example /// /// ```rust /// use rocket_lamb::{RocketExt, ResponseType}; /// /// let lamb = rocket::ignite() /// .lambda() /// .response_type("TEXT/PLAIN", ResponseType::Binary); /// assert_eq!(lamb.get_response_type("text/plain"), ResponseType::Binary); /// assert_eq!(lamb.get_response_type("application/json"), ResponseType::Text); /// ``` pub fn get_response_type(&self, content_type: &str) -> ResponseType { self.response_types .get(&content_type.to_lowercase()) .map(|rt| *rt) .unwrap_or(self.get_default_response_type()) } /// Sets the `ResponseType` for responses with the given Content-Type header. /// /// `content_type` values are treated case-insensitively. /// /// # Example /// /// ```rust /// use rocket_lamb::{RocketExt, ResponseType}; /// /// let lamb = rocket::ignite() /// .lambda() /// .response_type("TEXT/PLAIN", ResponseType::Binary); /// assert_eq!(lamb.get_response_type("text/plain"), ResponseType::Binary); /// assert_eq!(lamb.get_response_type("application/json"), ResponseType::Text); /// ``` pub fn response_type(mut self, content_type: &str, response_type: ResponseType) -> Self { self.response_types .insert(content_type.to_lowercase(), response_type); self } }