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
use crate::config::Config; use crate::handler::{LazyClient, RocketHandler}; use lambda_http::lambda; use rocket::Rocket; /// 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, } /// A builder to create and configure a [RocketHandler](RocketHandler). pub struct RocketHandlerBuilder { rocket: Rocket, config: Config, } impl RocketHandlerBuilder { /// Create a new `RocketHandlerBuilder`. Alternatively, you can use [rocket.lambda()](crate::RocketExt::lambda). /// /// # Example /// /// ```rust /// use rocket_lamb::RocketHandlerBuilder; /// /// let builder = RocketHandlerBuilder::new(rocket::ignite()); /// ``` pub fn new(rocket: rocket::Rocket) -> RocketHandlerBuilder { RocketHandlerBuilder { rocket, config: Config::default(), } } /// 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()](RocketHandlerBuilder::launch) method. /// /// # Example /// /// ```rust,no_run /// use rocket_lamb::RocketExt; /// use lambda_http::lambda; /// /// let handler = rocket::ignite().lambda().into_handler(); /// lambda!(handler); /// ``` pub fn into_handler(self) -> RocketHandler { RocketHandler { client: LazyClient::Uninitialized(self.rocket), config: self.config, } } /// Starts handling Lambda events by polling for events using Lambda's Runtime APIs. /// /// This function does not return, as it will loop forever (unless it panics). /// /// # Panics /// /// This panics if the required Lambda runtime environment variables are not set, or if the `Rocket` used to create the builder was misconfigured. /// /// # Example /// /// ```rust,no_run /// use rocket_lamb::RocketExt; /// use lambda_http::lambda; /// /// rocket::ignite().lambda().launch(); /// ``` pub fn launch(self) -> ! { lambda!(self.into_handler()); 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](RocketHandlerBuilder::response_type). /// /// # Example /// /// ```rust /// use rocket_lamb::{RocketExt, ResponseType}; /// /// let builder = rocket::ignite().lambda(); /// assert_eq!(builder.get_default_response_type(), ResponseType::Text); /// assert_eq!(builder.get_response_type("text/plain"), ResponseType::Text); /// ``` pub fn get_default_response_type(&self) -> ResponseType { self.config.default_response_type } /// Sets the default `ResponseType`, which is used for any responses that have not had their Content-Type overriden with [response_type](RocketHandlerBuilder::response_type). /// /// # Example /// /// ```rust /// use rocket_lamb::{RocketExt, ResponseType}; /// /// let builder = rocket::ignite() /// .lambda() /// .default_response_type(ResponseType::Binary); /// assert_eq!(builder.get_default_response_type(), ResponseType::Binary); /// assert_eq!(builder.get_response_type("text/plain"), ResponseType::Binary); /// ``` pub fn default_response_type(mut self, response_type: ResponseType) -> Self { self.config.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 builder = rocket::ignite() /// .lambda() /// .response_type("TEXT/PLAIN", ResponseType::Binary); /// assert_eq!(builder.get_response_type("text/plain"), ResponseType::Binary); /// assert_eq!(builder.get_response_type("application/json"), ResponseType::Text); /// ``` pub fn get_response_type(&self, content_type: &str) -> ResponseType { self.config .response_types .get(&content_type.to_lowercase()) .copied() .unwrap_or(self.config.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 builder = rocket::ignite() /// .lambda() /// .response_type("TEXT/PLAIN", ResponseType::Binary); /// assert_eq!(builder.get_response_type("text/plain"), ResponseType::Binary); /// assert_eq!(builder.get_response_type("application/json"), ResponseType::Text); /// ``` pub fn response_type(mut self, content_type: &str, response_type: ResponseType) -> Self { self.config .response_types .insert(content_type.to_lowercase(), response_type); self } /// Sets whether or not the handler should determine the API Gateway base path and prepend it to the path of request URLs. /// /// By default, this setting is `true`. /// /// When using the default API Gateway URL `1234567890.execute-api.region.amazonaws.com/{stage}/`, then the base path would /// be `/{stage}`. If this setting is set to `true` (the default), then all mounted routes will be made available under /// `/{stage}`, and all incoming requests to Rocket will have `/{stage}` at the beginning of the URL path. This is necessary /// to make absolute URLs in responses (e.g. in the `Location` response header for redirects) function correctly when /// hosting the server using the default API Gateway URL. /// /// # Example /// /// ```rust /// use rocket_lamb::RocketExt; /// /// let builder = rocket::ignite() /// .lambda() /// .include_api_gateway_base_path(false); /// ``` pub fn include_api_gateway_base_path(mut self, setting: bool) -> Self { self.config.include_api_gateway_base_path = setting; self } }