feat: basic routing done
This commit is contained in:
@@ -11,8 +11,10 @@
|
|||||||
- [ ] Transversal utility
|
- [ ] Transversal utility
|
||||||
- [ ] Let programmer set the default not found response
|
- [ ] Let programmer set the default not found response
|
||||||
- [x] Manage basic defaults app and route headers
|
- [x] Manage basic defaults app and route headers
|
||||||
- [ ] Manage route specific headers
|
- [x] Manage route specific headers
|
||||||
- [ ] Allow middleware
|
- [ ] Allow middleware
|
||||||
|
- [ ] Middleware to redirect or block petitions
|
||||||
|
- [ ] Middleware to insert headers and data
|
||||||
- [ ] Study how to organize functions on structs or types if possible
|
- [ ] Study how to organize functions on structs or types if possible
|
||||||
|
|
||||||
## Improvements
|
## Improvements
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
|
use std::fs;
|
||||||
use std::io::prelude::*;
|
use std::io::prelude::*;
|
||||||
use std::net::{SocketAddr, TcpListener, TcpStream};
|
use std::net::{SocketAddr, TcpListener, TcpStream};
|
||||||
|
|
||||||
@@ -8,7 +9,7 @@ impl Default for HttpAppConfig {
|
|||||||
fn default() -> Self {
|
fn default() -> Self {
|
||||||
Self {
|
Self {
|
||||||
port: 3000,
|
port: 3000,
|
||||||
max_buffer_size_bytes: 5120,
|
max_request_size_bytes: 5120,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -23,12 +24,26 @@ impl Default for HttpApp<'_> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl HttpAppRouteResponse<'_> {
|
||||||
|
pub fn from_url(url: &str) -> Self {
|
||||||
|
let response_body = fs::read_to_string(url).unwrap();
|
||||||
|
Self {
|
||||||
|
body: response_body,
|
||||||
|
content_type: "text/html; charset=utf-8",
|
||||||
|
status: 200,
|
||||||
|
headers: HashMap::new(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl HttpApp<'_> {
|
impl HttpApp<'_> {
|
||||||
fn get_route(&self, _path: &str) -> Option<&HttpAppRoute> {
|
fn get_route(&self, path: &str) -> Option<&HttpAppRoute> {
|
||||||
self.routes.first() // TODO: search the real one
|
// self.routes.first() // TODO: search the real one
|
||||||
|
self.routes.iter().find(|&route| route.route.eq(path))
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn add_route(&mut self, route: HttpAppRoute) {
|
pub fn add_route(&mut self, route: HttpAppRoute) {
|
||||||
|
// TODO: check if already exists
|
||||||
self.routes.push(route);
|
self.routes.push(route);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -56,7 +71,10 @@ impl HttpApp<'_> {
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if petition.bytes().len() > self.config.max_buffer_size_bytes {
|
// FIXME: this does not cover marginal cases: bigger buffer than max_buffer
|
||||||
|
if self.config.max_request_size_bytes > 0
|
||||||
|
&& petition.bytes().len() > self.config.max_request_size_bytes
|
||||||
|
{
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -65,14 +83,13 @@ impl HttpApp<'_> {
|
|||||||
|
|
||||||
match petition {
|
match petition {
|
||||||
Ok(petition_parsed) => {
|
Ok(petition_parsed) => {
|
||||||
// let mut response_content = fs::read_to_string("./routes/index.html").unwrap();
|
|
||||||
if let Some(route) = self.get_route(petition_parsed.request.query.path) {
|
if let Some(route) = self.get_route(petition_parsed.request.query.path) {
|
||||||
let matched_route = (route.action)(petition_parsed);
|
let matched_route = (route.action)(petition_parsed);
|
||||||
return format_response(matched_route);
|
return format_response(matched_route);
|
||||||
} else {
|
} else {
|
||||||
// TODO: return not found
|
// TODO: return not found
|
||||||
return ProcessedResponse {
|
return ProcessedResponse {
|
||||||
data: "".to_string(),
|
data: "Error 404".to_string(),
|
||||||
status: 400,
|
status: 400,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,7 +5,7 @@ use std::collections::HashMap;
|
|||||||
* */
|
* */
|
||||||
pub struct HttpAppConfig {
|
pub struct HttpAppConfig {
|
||||||
pub port: u16,
|
pub port: u16,
|
||||||
pub max_buffer_size_bytes: usize,
|
pub max_request_size_bytes: usize, // 0 to not max
|
||||||
}
|
}
|
||||||
|
|
||||||
pub type HttpAppRouteFunction = Box<fn(HttpRequest) -> HttpAppRouteResponse>;
|
pub type HttpAppRouteFunction = Box<fn(HttpRequest) -> HttpAppRouteResponse>;
|
||||||
|
|||||||
Reference in New Issue
Block a user