aboutsummaryrefslogtreecommitdiff
path: root/src/router.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/router.rs')
-rw-r--r--src/router.rs76
1 files changed, 43 insertions, 33 deletions
diff --git a/src/router.rs b/src/router.rs
index 7b1313b..ff9a893 100644
--- a/src/router.rs
+++ b/src/router.rs
@@ -1,6 +1,6 @@
use crate::{
extractor::build_regex_from_path,
- http_types::{Method, StatusCode},
+ http_types::{get, post, put, Endpoint, Method, StatusCode},
request::Request,
response::Response,
};
@@ -8,7 +8,7 @@ use regex::Regex;
use std::collections::HashMap;
type Handler = fn(&Request, Option<&HashMap<String, String>>) -> Response;
-type Routes = HashMap<Method, Handler>;
+type Routes = HashMap<Endpoint, Handler>;
pub struct Router {
routes: Routes,
@@ -27,53 +27,63 @@ impl Router {
}
// Add a route to the router
- pub fn route(&mut self, method: Method, handler: Handler) -> &mut Self {
- use Method::*;
- match method {
- Get(route) => {
+ pub fn route(&mut self, endpoint: Endpoint, handler: Handler) -> &mut Self {
+ use Method as M;
+ match (endpoint.method, endpoint.route) {
+ (M::GET, route) => {
let re = build_regex_from_path(&route);
- let meth = Get(re.to_string());
- self.routes.insert(meth, handler);
+ let epoint = get(re.as_str());
+ self.routes.insert(epoint, handler);
+ }
+ (M::POST, route) => {
+ let re = build_regex_from_path(&route);
+ let epoint = post(re.as_str());
+ self.routes.insert(epoint, handler);
}
- Post(route) => {
+ (M::PUT, route) => {
let re = build_regex_from_path(&route);
- let meth = Post(re.to_string());
+ let meth = put(re.as_str());
self.routes.insert(meth, handler);
}
- Put(_) => todo!(),
}
self
}
// Handle incoming requests
pub fn handle(&self, request: &Request, ctx: Option<&HashMap<String, String>>) -> Response {
- use Method::*;
- match &request.method {
- Get(request_method) => {
- for (method, handler) in self.routes() {
- if let Get(method_string) = method {
- let re = Regex::new(method_string).unwrap();
- // dbg!(&re, request_method);
- if re.is_match(request_method) {
- return handler(request, ctx);
- }
+ use Method as M;
+ for (endpoint, handler) in self.routes() {
+ let repoint = &request.endpoint;
+ match (
+ &repoint.method,
+ &repoint.route,
+ &endpoint.method,
+ &endpoint.route,
+ ) {
+ (M::GET, request_route, M::GET, endpoint_regex) => {
+ let re = Regex::new(&endpoint_regex).unwrap();
+ // dbg!(&re, request_method);
+ if re.is_match(&request_route) {
+ return handler(request, ctx);
}
}
- Response::new("1.1".to_string(), StatusCode::NotFound, None, None).into()
- }
- Post(request_method) => {
- for (method, handler) in self.routes() {
- if let Post(method_string) = method {
- let re = Regex::new(method_string).unwrap();
- // dbg!(&re, request_method);
- if re.is_match(request_method) {
- return handler(request, ctx);
- }
+ (M::POST, request_route, M::POST, endpoint_regex) => {
+ let re = Regex::new(&endpoint_regex).unwrap();
+ // dbg!(&re, request_method);
+ if re.is_match(&request_route) {
+ return handler(request, ctx);
+ }
+ }
+ (M::PUT, request_route, M::PUT, endpoint_regex) => {
+ let re = Regex::new(&endpoint_regex).unwrap();
+ // dbg!(&re, request_method);
+ if re.is_match(&request_route) {
+ return handler(request, ctx);
}
}
- Response::new("1.1".to_string(), StatusCode::NotFound, None, None).into()
+ _ => {}
}
- Put(_) => todo!(),
}
+ Response::new("1.1".to_string(), StatusCode::NotFound, None, None)
}
}