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::*; use crate::handler::{LazyClient, RocketHandler}; use lambda_http::lambda; use rocket::Rocket; /// 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 } /// Determines whether the API Gateway base path is included in the URL processed by Rocket. /// The default is `RemountAndInclude`. /// /// When calling the API using the default API Gateway URL e.g. /// `{api-id}.execute-api.{region}.amazonaws.com/{stage}/`, the base path will be `/{stage}`. /// When calling the API using an API Gateway custom domain, a base path may be configured /// on the custom domain. /// /// This has no effect for Application Load Balancer requests, as these will never have a base path. /// /// The possible values are: /// - `RemountAndInclude` - Includes the base bath in the URL. The first request received will be used to determine /// the base path, and all mounted routes will be cloned and re-mounted at the base path. /// - `Include` - Includes the base bath in the URL. You must ensure that the `Rocket`'s routes have been /// mounted at the expected base path. /// - `Exclude` - Excludes the base bath from the URL. The URL processed by Rocket may not match the full /// path of the original client, which may cause absolute URLs in responses (e.g. in the /// `Location` response header for redirects) to not behave as expected. /// /// # Example /// /// ```rust /// use rocket_lamb::{BasePathBehaviour, RocketExt}; /// /// let builder = rocket::ignite() /// .lambda() /// .base_path_behaviour(BasePathBehaviour::Exclude); /// ``` pub fn base_path_behaviour(mut self, setting: BasePathBehaviour) -> Self { self.config.base_path_behaviour = setting; self } }