Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add server lifetimes #4

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
28 changes: 14 additions & 14 deletions src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,23 @@ use std::{
net::{self, TcpListener},
};

pub type RequestHandler = Box<dyn Send + (Fn(Request, Response) -> Response) + 'static>;
pub type RequestHandler<'a> = Box<dyn Send + (Fn(Request, Response) -> 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<HttpHandler>,
not_found_handler: RequestHandler,
pub struct HttpServer<'a> {
routes: Vec<HttpHandler<'a>>,
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(),
Expand All @@ -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()
Expand All @@ -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 {
Expand All @@ -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)
}

Expand All @@ -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)
}

Expand All @@ -90,21 +90,21 @@ 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)
}

/// Add a handler for 404 error
/// ```
/// 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)
Expand Down