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 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622
use crate::constants; use crate::middleware::{Middleware, PostMiddleware, PreMiddleware}; use crate::route::Route; use crate::router::ErrHandler; use crate::router::Router; use hyper::{body::HttpBody, Method, Request, Response}; use std::future::Future; /// Builder for the [Router](./struct.Router.html) type. /// /// This `RouterBuilder<B, E>` type accepts two type parameters: `B` and `E`. /// /// * The `B` represents the response body type which will be used by route handlers and the middlewares and this body type must implement /// the [HttpBody](https://docs.rs/hyper/0.13.5/hyper/body/trait.HttpBody.html) trait. For an instance, `B` could be [hyper::Body](https://docs.rs/hyper/0.13.5/hyper/body/struct.Body.html) /// type. /// * The `E` represents any error type which will be used by route handlers and the middlewares. This error type must implement the [std::error::Error](https://doc.rust-lang.org/std/error/trait.Error.html). /// /// # Examples /// /// ```no_run /// use routerify::{Router, Middleware}; /// use hyper::{Response, Request, Body}; /// /// async fn home_handler(req: Request<Body>) -> Result<Response<Body>, hyper::Error> { /// Ok(Response::new(Body::from("home"))) /// } /// /// async fn upload_handler(req: Request<Body>) -> Result<Response<Body>, hyper::Error> { /// Ok(Response::new(Body::from("upload"))) /// } /// /// async fn some_pre_middleware_handler(req: Request<Body>) -> Result<Request<Body>, hyper::Error> { /// Ok(req) /// } /// /// # fn run() -> Router<Body, hyper::Error> { /// // Use Router::builder() method to create a new RouterBuilder instance. /// // We will use hyper::Body as response body type and hyper::Error as error type. /// let router: Router<Body, hyper::Error> = Router::builder() /// .get("/", home_handler) /// .post("/upload", upload_handler) /// .middleware(Middleware::pre(some_pre_middleware_handler)) /// .build() /// .unwrap(); /// # router /// # } /// # run(); /// ``` pub struct RouterBuilder<B, E> { inner: crate::Result<BuilderInner<B, E>>, } struct BuilderInner<B, E> { pre_middlewares: Vec<PreMiddleware<E>>, routes: Vec<Route<B, E>>, post_middlewares: Vec<PostMiddleware<B, E>>, err_handler: Option<ErrHandler<B>>, } impl<B: HttpBody + Send + Sync + Unpin + 'static, E: std::error::Error + Send + Sync + Unpin + 'static> RouterBuilder<B, E> { /// Creates a new `RouterBuilder` instance with default options. pub fn new() -> RouterBuilder<B, E> { RouterBuilder::default() } /// Creates a new [Router](./struct.Router.html) instance from the added configuration. pub fn build(self) -> crate::Result<Router<B, E>> { self.inner.map(|inner| Router { pre_middlewares: inner.pre_middlewares, routes: inner.routes, post_middlewares: inner.post_middlewares, err_handler: inner.err_handler, }) } fn and_then<F>(self, func: F) -> Self where F: FnOnce(BuilderInner<B, E>) -> crate::Result<BuilderInner<B, E>>, { RouterBuilder { inner: self.inner.and_then(func), } } } impl<B: HttpBody + Send + Sync + Unpin + 'static, E: std::error::Error + Send + Sync + Unpin + 'static> RouterBuilder<B, E> { /// Adds a new route with `GET` method and the handler at the specified path. /// /// # Examples /// /// ``` /// use routerify::Router; /// use hyper::{Response, Request, Body}; /// /// async fn home_handler(req: Request<Body>) -> Result<Response<Body>, hyper::Error> { /// Ok(Response::new(Body::from("home"))) /// } /// /// # fn run() -> Router<Body, hyper::Error> { /// let router = Router::builder() /// .get("/", home_handler) /// .build() /// .unwrap(); /// # router /// # } /// # run(); /// ``` pub fn get<P, H, R>(self, path: P, handler: H) -> Self where P: Into<String>, H: FnMut(Request<hyper::Body>) -> R + Send + Sync + 'static, R: Future<Output = Result<Response<B>, E>> + Send + 'static, { self.add(path, vec![Method::GET], handler) } /// Adds a new route with `GET` and `HEAD` methods and the handler at the specified path. /// /// # Examples /// /// ``` /// use routerify::Router; /// use hyper::{Response, Request, Body}; /// /// async fn home_handler(req: Request<Body>) -> Result<Response<Body>, hyper::Error> { /// Ok(Response::new(Body::from("home"))) /// } /// /// # fn run() -> Router<Body, hyper::Error> { /// let router = Router::builder() /// .get_or_head("/", home_handler) /// .build() /// .unwrap(); /// # router /// # } /// # run(); /// ``` pub fn get_or_head<P, H, R>(self, path: P, handler: H) -> Self where P: Into<String>, H: FnMut(Request<hyper::Body>) -> R + Send + Sync + 'static, R: Future<Output = Result<Response<B>, E>> + Send + 'static, { self.add(path, vec![Method::GET, Method::HEAD], handler) } /// Adds a new route with `POST` method and the handler at the specified path. /// /// # Examples /// /// ``` /// use routerify::Router; /// use hyper::{Response, Request, Body}; /// /// async fn file_upload_handler(req: Request<Body>) -> Result<Response<Body>, hyper::Error> { /// Ok(Response::new(Body::from("File uploader"))) /// } /// /// # fn run() -> Router<Body, hyper::Error> { /// let router = Router::builder() /// .post("/upload", file_upload_handler) /// .build() /// .unwrap(); /// # router /// # } /// # run(); /// ``` pub fn post<P, H, R>(self, path: P, handler: H) -> Self where P: Into<String>, H: FnMut(Request<hyper::Body>) -> R + Send + Sync + 'static, R: Future<Output = Result<Response<B>, E>> + Send + 'static, { self.add(path, vec![Method::POST], handler) } /// Adds a new route with `PUT` method and the handler at the specified path. /// /// # Examples /// /// ``` /// use routerify::Router; /// use hyper::{Response, Request, Body}; /// /// async fn file_upload_handler(req: Request<Body>) -> Result<Response<Body>, hyper::Error> { /// Ok(Response::new(Body::from("File uploader"))) /// } /// /// # fn run() -> Router<Body, hyper::Error> { /// let router = Router::builder() /// .put("/upload", file_upload_handler) /// .build() /// .unwrap(); /// # router /// # } /// # run(); /// ``` pub fn put<P, H, R>(self, path: P, handler: H) -> Self where P: Into<String>, H: FnMut(Request<hyper::Body>) -> R + Send + Sync + 'static, R: Future<Output = Result<Response<B>, E>> + Send + 'static, { self.add(path, vec![Method::PUT], handler) } /// Adds a new route with `DELETE` method and the handler at the specified path. /// /// # Examples /// /// ``` /// use routerify::Router; /// use hyper::{Response, Request, Body}; /// /// async fn delete_file_handler(req: Request<Body>) -> Result<Response<Body>, hyper::Error> { /// Ok(Response::new(Body::from("Delete file"))) /// } /// /// # fn run() -> Router<Body, hyper::Error> { /// let router = Router::builder() /// .delete("/delete-file", delete_file_handler) /// .build() /// .unwrap(); /// # router /// # } /// # run(); /// ``` pub fn delete<P, H, R>(self, path: P, handler: H) -> Self where P: Into<String>, H: FnMut(Request<hyper::Body>) -> R + Send + Sync + 'static, R: Future<Output = Result<Response<B>, E>> + Send + 'static, { self.add(path, vec![Method::DELETE], handler) } /// Adds a new route with `HEAD` method and the handler at the specified path. /// /// # Examples /// /// ``` /// use routerify::Router; /// use hyper::{Response, Request, Body}; /// /// async fn a_head_handler(req: Request<Body>) -> Result<Response<Body>, hyper::Error> { /// Ok(Response::new(Body::empty())) /// } /// /// # fn run() -> Router<Body, hyper::Error> { /// let router = Router::builder() /// .head("/fetch-data", a_head_handler) /// .build() /// .unwrap(); /// # router /// # } /// # run(); /// ``` pub fn head<P, H, R>(self, path: P, handler: H) -> Self where P: Into<String>, H: FnMut(Request<hyper::Body>) -> R + Send + Sync + 'static, R: Future<Output = Result<Response<B>, E>> + Send + 'static, { self.add(path, vec![Method::HEAD], handler) } /// Adds a new route with `TRACE` method and the handler at the specified path. /// /// # Examples /// /// ``` /// use routerify::Router; /// use hyper::{Response, Request, Body}; /// /// async fn trace_handler(req: Request<Body>) -> Result<Response<Body>, hyper::Error> { /// Ok(Response::new(Body::empty())) /// } /// /// # fn run() -> Router<Body, hyper::Error> { /// let router = Router::builder() /// .trace("/abc", trace_handler) /// .build() /// .unwrap(); /// # router /// # } /// # run(); /// ``` pub fn trace<P, H, R>(self, path: P, handler: H) -> Self where P: Into<String>, H: FnMut(Request<hyper::Body>) -> R + Send + Sync + 'static, R: Future<Output = Result<Response<B>, E>> + Send + 'static, { self.add(path, vec![Method::TRACE], handler) } /// Adds a new route with `CONNECT` method and the handler at the specified path. /// /// # Examples /// /// ``` /// use routerify::Router; /// use hyper::{Response, Request, Body}; /// /// async fn connect_handler(req: Request<Body>) -> Result<Response<Body>, hyper::Error> { /// Ok(Response::new(Body::empty())) /// } /// /// # fn run() -> Router<Body, hyper::Error> { /// let router = Router::builder() /// .connect("/abc", connect_handler) /// .build() /// .unwrap(); /// # router /// # } /// # run(); /// ``` pub fn connect<P, H, R>(self, path: P, handler: H) -> Self where P: Into<String>, H: FnMut(Request<hyper::Body>) -> R + Send + Sync + 'static, R: Future<Output = Result<Response<B>, E>> + Send + 'static, { self.add(path, vec![Method::CONNECT], handler) } /// Adds a new route with `PATCH` method and the handler at the specified path. /// /// # Examples /// /// ``` /// use routerify::Router; /// use hyper::{Response, Request, Body}; /// /// async fn update_data_handler(req: Request<Body>) -> Result<Response<Body>, hyper::Error> { /// Ok(Response::new(Body::from("Data updater"))) /// } /// /// # fn run() -> Router<Body, hyper::Error> { /// let router = Router::builder() /// .patch("/update-data", update_data_handler) /// .build() /// .unwrap(); /// # router /// # } /// # run(); /// ``` pub fn patch<P, H, R>(self, path: P, handler: H) -> Self where P: Into<String>, H: FnMut(Request<hyper::Body>) -> R + Send + Sync + 'static, R: Future<Output = Result<Response<B>, E>> + Send + 'static, { self.add(path, vec![Method::PATCH], handler) } /// Adds a new route with `OPTIONS` method and the handler at the specified path. /// /// # Examples /// /// ``` /// use routerify::Router; /// use hyper::{Response, Request, Body}; /// /// async fn options_handler(req: Request<Body>) -> Result<Response<Body>, hyper::Error> { /// Ok(Response::new(Body::empty())) /// } /// /// # fn run() -> Router<Body, hyper::Error> { /// let router = Router::builder() /// .options("/abc", options_handler) /// .build() /// .unwrap(); /// # router /// # } /// # run(); /// ``` pub fn options<P, H, R>(self, path: P, handler: H) -> Self where P: Into<String>, H: FnMut(Request<hyper::Body>) -> R + Send + Sync + 'static, R: Future<Output = Result<Response<B>, E>> + Send + 'static, { self.add(path, vec![Method::OPTIONS], handler) } /// Adds a new route with any method type and the handler at the `/*` path. It will accept any kind of request. It can be used to send /// response for any non-existing routes i.e. for `404` pages. /// /// # Examples /// /// ``` /// use routerify::Router; /// use hyper::{Response, Request, Body, StatusCode}; /// /// async fn home_handler(req: Request<Body>) -> Result<Response<Body>, hyper::Error> { /// Ok(Response::new(Body::from("home"))) /// } /// /// async fn handler_404(req: Request<Body>) -> Result<Response<Body>, hyper::Error> { /// Ok( /// Response::builder() /// .status(StatusCode::NOT_FOUND) /// .body(Body::from("NOT FOUND")) /// .unwrap() /// ) /// } /// /// # fn run() -> Router<Body, hyper::Error> { /// let router = Router::builder() /// .get("/home", home_handler) /// .any(handler_404) /// .build() /// .unwrap(); /// # router /// # } /// # run(); /// ``` pub fn any<H, R>(self, handler: H) -> Self where H: FnMut(Request<hyper::Body>) -> R + Send + Sync + 'static, R: Future<Output = Result<Response<B>, E>> + Send + 'static, { self.add("/*", constants::ALL_POSSIBLE_HTTP_METHODS.to_vec(), handler) } /// Adds a new route with the specified method(s) and the handler at the specified path. It can be used to define routes with multiple method types. /// /// # Examples /// /// ``` /// use routerify::Router; /// use hyper::{Response, Request, Body, StatusCode, Method}; /// /// async fn cart_checkout_handler(req: Request<Body>) -> Result<Response<Body>, hyper::Error> { /// Ok(Response::new(Body::from("You shopping cart is being checking out"))) /// } /// /// # fn run() -> Router<Body, hyper::Error> { /// let router = Router::builder() /// .add("/checkout", vec![Method::GET, Method::POST], cart_checkout_handler) /// .build() /// .unwrap(); /// # router /// # } /// # run(); /// ``` pub fn add<P, H, R>(self, path: P, methods: Vec<Method>, handler: H) -> Self where P: Into<String>, H: FnMut(Request<hyper::Body>) -> R + Send + Sync + 'static, R: Future<Output = Result<Response<B>, E>> + Send + 'static, { self.and_then(move |mut inner| { let route = Route::new(path, methods, handler)?; inner.routes.push(route); crate::Result::Ok(inner) }) } /// It mounts a router onto another router. It can be very useful when you want to write modular routing logic. /// /// # Examples /// /// ``` /// use routerify::Router; /// use hyper::{Response, Request, Body}; /// /// mod api { /// use routerify::Router; /// use hyper::{Response, Request, Body}; /// /// pub fn router() -> Router<Body, hyper::Error> { /// Router::builder() /// .get("/users", |req| async move { Ok(Response::new(Body::from("User list"))) }) /// .get("/books", |req| async move { Ok(Response::new(Body::from("Book list"))) }) /// .build() /// .unwrap() /// } /// } /// /// # fn run() -> Router<Body, hyper::Error> { /// let router = Router::builder() /// // Now, mount the api router at `/api` path. /// .scope("/api", api::router()) /// .build() /// .unwrap(); /// # router /// # } /// # run(); /// ``` /// /// Now, the app can handle requests on: `/api/users` and `/api/books` paths. pub fn scope<P>(self, path: P, mut router: Router<B, E>) -> Self where P: Into<String>, { let mut path = path.into(); if path.ends_with("/") { path = (&path[..path.len() - 1]).to_string(); } let mut builder = self; for pre_middleware in router.pre_middlewares.iter_mut() { let new_pre_middleware = PreMiddleware::new_with_boxed_handler( format!("{}{}", path.as_str(), pre_middleware.path.as_str()), pre_middleware .handler .take() .expect("No handler found in one of the pre-middlewares"), ); builder = builder.and_then(move |mut inner| { inner.pre_middlewares.push(new_pre_middleware?); crate::Result::Ok(inner) }); } for route in router.routes.iter_mut() { let new_route = Route::new_with_boxed_handler( format!("{}{}", path.as_str(), route.path.as_str()), route.methods.clone(), route.handler.take().expect("No handler found in one of the routes"), ); builder = builder.and_then(move |mut inner| { inner.routes.push(new_route?); crate::Result::Ok(inner) }); } for post_middleware in router.post_middlewares.iter_mut() { let new_post_middleware = PostMiddleware::new_with_boxed_handler( format!("{}{}", path.as_str(), post_middleware.path.as_str()), post_middleware .handler .take() .expect("No handler found in one of the post-middlewares"), ); builder = builder.and_then(move |mut inner| { inner.post_middlewares.push(new_post_middleware?); crate::Result::Ok(inner) }); } builder } } impl<B: HttpBody + Send + Sync + Unpin + 'static, E: std::error::Error + Send + Sync + Unpin + 'static> RouterBuilder<B, E> { /// Adds a single middleware. A pre middleware can be created by [`Middleware::pre`](./enum.Middleware.html#method.pre) method and a post /// middleware can be created by [`Middleware::post`](./enum.Middleware.html#method.post) method. /// /// # Examples /// /// ``` /// use routerify::{Router, Middleware}; /// use hyper::{Response, Request, Body}; /// use std::convert::Infallible; /// /// # fn run() -> Router<Body, Infallible> { /// let router = Router::builder() /// // Create and attach a pre middleware. /// .middleware(Middleware::pre(|req| async move { /* Do some operations */ Ok(req) })) /// // Create and attach a post middleware. /// .middleware(Middleware::post(|res| async move { /* Do some operations */ Ok(res) })) /// .build() /// .unwrap(); /// # router /// # } /// # run(); /// ``` pub fn middleware(self, m: Middleware<B, E>) -> Self { self.and_then(move |mut inner| { match m { Middleware::Pre(middleware) => { inner.pre_middlewares.push(middleware); } Middleware::Post(middleware) => { inner.post_middlewares.push(middleware); } } crate::Result::Ok(inner) }) } /// Adds a handler to handle any error raised by the routes or any middlewares. Please refer to [Error Handling](./index.html#error-handling) section /// for more info. pub fn err_handler<H, R>(self, mut handler: H) -> Self where H: FnMut(crate::Error) -> R + Send + Sync + 'static, R: Future<Output = Response<B>> + Send + 'static, { let handler: ErrHandler<B> = Box::new(move |err: crate::Error| Box::new(handler(err))); self.and_then(move |mut inner| { inner.err_handler = Some(handler); crate::Result::Ok(inner) }) } } impl<B: HttpBody + Send + Sync + Unpin + 'static, E: std::error::Error + Send + Sync + Unpin + 'static> Default for RouterBuilder<B, E> { fn default() -> RouterBuilder<B, E> { RouterBuilder { inner: Ok(BuilderInner { pre_middlewares: Vec::new(), routes: Vec::new(), post_middlewares: Vec::new(), err_handler: None, }), } } }