From fd0a869335d58e9b2b3f200df8082d214ca1de53 Mon Sep 17 00:00:00 2001 From: David Llewellyn-Jones Date: Sun, 18 Jun 2023 16:07:11 +0100 Subject: [PATCH] Add server lifetimes Adds lifetimes to the server so that passed or captured parameters don't have to be static (they just have to live as long as the server). --- src/lib.rs | 4 ++-- src/server.rs | 28 ++++++++++++++-------------- 2 files changed, 16 insertions(+), 16 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 654bc91..b62c780 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -72,12 +72,12 @@ pub use server::HttpServer; pub use server::RequestHandler; /// Create new instance of HttpServer -pub fn new() -> HttpServer { +pub fn new<'a>() -> HttpServer<'a> { HttpServer::new() } /// Create new instance of HttpServer with predefined body -pub fn create_server(default_repsonse: Response) -> HttpServer { +pub fn create_server<'a>(default_repsonse: Response) -> HttpServer<'a> { let mut ret = HttpServer::new(); ret.default_repsonse = default_repsonse; ret diff --git a/src/server.rs b/src/server.rs index 59ddad1..89b0db9 100644 --- a/src/server.rs +++ b/src/server.rs @@ -6,23 +6,23 @@ use std::{ net::{self, TcpListener}, }; -pub type RequestHandler = Box Response) + 'static>; +pub type RequestHandler<'a> = Box Response) + 'a>; // first parameter = http path + http method // second parameter = function handler -struct HttpHandler(crate::HttpRoute, RequestHandler); +struct HttpHandler<'a>(crate::HttpRoute, RequestHandler<'a>); /// Storing basics informations about server and handlers /// Represents http server -pub struct HttpServer { - routes: Vec, - not_found_handler: RequestHandler, +pub struct HttpServer<'a> { + routes: Vec>, + not_found_handler: RequestHandler<'a>, pub(crate) default_repsonse: Response, to_close: bool, } -impl Default for HttpServer { +impl<'a> Default for HttpServer<'a> { fn default() -> Self { Self { routes: Vec::new(), @@ -36,7 +36,7 @@ impl Default for HttpServer { } } -impl HttpServer { +impl<'a> HttpServer<'a> { /// Create new instance of HttpServer pub fn new() -> Self { Default::default() @@ -46,8 +46,8 @@ impl HttpServer { pub fn route( mut self, method: HttpMethod, - path: &'static str, - handler: RequestHandler, + path: &'a str, + handler: RequestHandler<'a>, ) -> Self { self.routes.push(HttpHandler( crate::HttpRoute { @@ -66,7 +66,7 @@ impl HttpServer { /// database.get_user(request.params.get("id").unwrap()).into() /// })) /// ``` - pub fn get(self, path: &'static str, handler: RequestHandler) -> Self { + pub fn get(self, path: &'a str, handler: RequestHandler<'a>) -> Self { self.route(HttpMethod::GET, path, handler) } @@ -79,7 +79,7 @@ impl HttpServer { /// default_repsonse /// })) /// ``` - pub fn post(self, path: &'static str, handler: RequestHandler) -> Self { + pub fn post(self, path: &'a str, handler: RequestHandler<'a>) -> Self { self.route(HttpMethod::POST, path, handler) } @@ -90,7 +90,7 @@ impl HttpServer { /// .post("/endpoint", Box::new(|request, default_response| "Gate POST were obtained".into())) /// .any("/endpoint", Box::new(|request, default_response| "Another gate were obtained".into())) /// ``` - pub fn any(self, path: &'static str, handler: RequestHandler) -> Self { + pub fn any(self, path: &'a str, handler: RequestHandler<'a>) -> Self { self.route(HttpMethod::Any, path, handler) } @@ -98,13 +98,13 @@ impl HttpServer { /// ``` /// server.not_found(Box::new(|_, _| "Not found!".into())); /// ``` - pub fn not_found(mut self, handler: RequestHandler) -> Self { + pub fn not_found(mut self, handler: RequestHandler<'a>) -> Self { self.not_found_handler = handler; self } } -impl HttpServer { +impl<'a> HttpServer<'a> { /// Launch server on port 80 pub fn run(&self) { self.launch(80)