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
// use std::process;
// use colored::Colorize;
use std::collections::HashMap;
use super::route::Route;
// use pillow_env::Env;
use pillow_http::{handler::Handler, middlewares::Middleware, Request, Response};
/*
/// Instance of Router
pub struct Router {
pub addr: String,
middlewares: Vec<Middleware>,
routes: Routes,
}
impl Router {
pub fn new() -> Router {
Router {
addr: String::from("127.0.0.1"),
middlewares: Vec::new(),
routes: Routes::new(),
}
}
}
/*
impl Router {
pub fn get_struct<T: Handler + std::fmt::Debug>(&mut self, uri: &str, controller: T) {
println!("{}", uri);
println!("{:#?}", controller);
}
}
impl Router {
/// Add a global middleware
///
/// # Arguments
///
/// * `controller` - Function for middleware
pub fn add_middleware<F>(&mut self, controller: F)
where
F: Fn(&Request, &Response) + 'static,
{
self.middlewares.push(Middleware::new(controller))
}
}
*/*/
/// The Main router in your app
pub struct MainRouter {
routes: HashMap<pillow_http::http_methods::HttpMethods, Vec<Route>>,
}
impl MainRouter {
/// Instance of a router
pub fn new() -> Self {
Self {
routes: HashMap::new(),
}
}
/// Reference of routes
pub fn routes(&self) -> &HashMap<pillow_http::http_methods::HttpMethods, Vec<Route>> {
&self.routes
}
fn get_routes_from_method(
&self,
method: &pillow_http::http_methods::HttpMethods,
) -> Option<&Vec<Route>> {
self.routes.get(&method)
}
fn get_option_index(&self, uri: &pillow_http::Uri, routes_vec: &Vec<Route>) -> Option<usize> {
routes_vec.iter().position(|route| route.uri() == uri)
}
pub(crate) fn routing(&self, request: &Request) -> Response {
let option_routes_vec = self.get_routes_from_method(request.method());
let routes_vec = match option_routes_vec {
Some(routes) => routes,
None => panic!("Routes empty"),
};
let option_index = self.get_option_index(request.uri(), &routes_vec);
let mut response = Response::new_empty();
match option_index {
Some(index) => {
let route_m = &routes_vec[index];
response = route_m.use_controller(request.clone())
}
None => {
let routes_params: Vec<_> = routes_vec
.iter()
.filter(|route| route.has_parameters())
.collect();
for route in routes_params {
let path: Vec<_> = route
.regex_complete
.split(&route.uri().0.as_str())
.collect();
let path_param: Vec<_> = route
.regex_words
.find_iter(&request.uri().0.as_str())
.collect();
if request.uri().0.starts_with(path[0]) {
let route_m = route;
response = route_m.use_controller(request.clone());
}
}
}
}
response
}
}
impl MainRouter {
/// Method POST
/// # Arguments
///
/// * `uri` - Path of route
/// * `controller` - Callback function
///
/// # Examples
///
/// ```
/// use pillow::http::{MainRouter, Response};
///
///
/// #[tokio::main]
/// async fn main (){
/// let mut router = MainRouter::new();
///
/// router.get("/", |_, | Response::view("index"));
/// }
/// ```
pub fn get<F>(&mut self, uri: &str, controller: F)
where
F: Fn(Request) -> Response + Sync + Send + 'static,
{
let uri = uri.to_string();
self.routes
.entry(pillow_http::http_methods::HttpMethods::GET)
.or_insert(Vec::new())
.push(Route::new(uri, controller));
}
/// Method POST
/// # Arguments
///
/// * `uri` - Path of route
/// * `controller` - Callback function
///
/// # Examples
///
/// ```
/// use pillow::http::{MainRouter, Response};
///
///
/// #[tokio::main]
/// async fn main (){
/// let mut router = MainRouter::new();
///
/// router.post("/", |_, | Response::view("index"));
/// }
/// ```
pub fn post<F>(&mut self, uri: &str, controller: F)
where
F: Fn(Request) -> Response + Sync + Send + 'static,
{
let uri = uri.to_string();
let method = pillow_http::http_methods::HttpMethods::POST;
self.routes
.entry(method)
.or_insert(Vec::new())
.push(Route::new(uri, controller));
}
/// Method PUT
/// # Arguments
///
/// * `uri` - Path of route
/// * `controller` - Callback function
///
/// # Examples
///
/// ```
/// use pillow::http::{MainRouter, Response};
///
///
/// #[tokio::main]
/// async fn main (){
/// let mut router = MainRouter::new();
///
/// router.put("/", |_, | Response::view("index"));
/// }
/// ```
pub fn put<F>(&mut self, uri: &str, controller: F)
where
F: Fn(Request) -> Response + Sync + Send + 'static,
{
let uri = uri.to_string();
let method = pillow_http::http_methods::HttpMethods::PUT;
self.routes
.entry(method)
.or_insert(Vec::new())
.push(Route::new(uri, controller));
}
/// Method DELETE
/// # Arguments
///
/// * `uri` - Path of route
/// * `controller` - Callback function
///
/// # Examples
///
/// ```
/// use pillow::http::{MainRouter, Response};
///
///
/// #[tokio::main]
/// async fn main (){
/// let mut router = MainRouter::new();
///
/// router.delete("/", |_, | Response::view("index"));
/// }
/// ```
pub fn delete<F>(&mut self, uri: &str, controller: F)
where
F: Fn(Request) -> Response + Sync + Send + 'static,
{
let uri = uri.to_string();
let method = pillow_http::http_methods::HttpMethods::DELETE;
self.routes
.entry(method)
.or_insert(Vec::new())
.push(Route::new(uri, controller));
}
}