feat: basic organization
This commit is contained in:
@@ -1,45 +1,9 @@
|
|||||||
use std::fs;
|
|
||||||
use std::io::prelude::*;
|
|
||||||
use std::net::TcpStream;
|
|
||||||
|
|
||||||
mod generators;
|
mod generators;
|
||||||
mod parsers;
|
mod parsers;
|
||||||
|
mod server;
|
||||||
mod types;
|
mod types;
|
||||||
|
|
||||||
|
use generators::*;
|
||||||
use parsers::*;
|
use parsers::*;
|
||||||
|
use server::*;
|
||||||
use types::*;
|
use types::*;
|
||||||
|
|
||||||
pub fn process_petition(stream: &mut TcpStream) -> ProcessedResponse {
|
|
||||||
let mut buffer = [0; 1024]; // TODO: manage this size
|
|
||||||
let _amount = stream.read(&mut buffer);
|
|
||||||
let petition = String::from_utf8_lossy(&buffer[..]);
|
|
||||||
let petition = parse_request(&petition);
|
|
||||||
|
|
||||||
match petition {
|
|
||||||
Ok(petition_parsed) => {
|
|
||||||
let response_status = "200 OK";
|
|
||||||
|
|
||||||
let response_content = fs::read_to_string("./routes/index.html").unwrap();
|
|
||||||
|
|
||||||
let response: ProcessedResponse = ProcessedResponse {
|
|
||||||
data: format!(
|
|
||||||
"HTTP/1.1 {}\r\nContent-Length: {}\r\n\r\n{}",
|
|
||||||
response_status,
|
|
||||||
response_content.len(),
|
|
||||||
response_content
|
|
||||||
),
|
|
||||||
status: 200,
|
|
||||||
};
|
|
||||||
|
|
||||||
response
|
|
||||||
}
|
|
||||||
Err(error) => {
|
|
||||||
let response: ProcessedResponse = ProcessedResponse {
|
|
||||||
data: format!("HTTP/1.1 {}\r\nContent-Length: 0\r\n\r\n", error),
|
|
||||||
status: error,
|
|
||||||
};
|
|
||||||
|
|
||||||
response
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -2,21 +2,19 @@ use std::fs;
|
|||||||
use std::io::prelude::*;
|
use std::io::prelude::*;
|
||||||
use std::net::TcpStream;
|
use std::net::TcpStream;
|
||||||
|
|
||||||
mod generators;
|
use super::*;
|
||||||
mod parsers;
|
|
||||||
mod types;
|
|
||||||
|
|
||||||
use parsers::*;
|
struct HTTPServer<'a> {
|
||||||
use types::*;
|
config: HttpAppConfig,
|
||||||
|
routes: Vec<HttpAppRoute<'a>>,
|
||||||
struct HTTPServer {
|
|
||||||
config: ;
|
|
||||||
routes: ;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl HTTPServer {
|
impl HTTPServer<'_> {
|
||||||
|
fn get_route(&self, path: &str) -> Option<&HttpAppRoute> {
|
||||||
|
self.routes.first() // TODO: search the real one
|
||||||
|
}
|
||||||
|
|
||||||
fn process_petition(stream: &mut TcpStream) -> ProcessedResponse {
|
fn process_petition(&self, stream: &mut TcpStream) -> ProcessedResponse {
|
||||||
let mut buffer = [0; 1024]; // TODO: manage this size
|
let mut buffer = [0; 1024]; // TODO: manage this size
|
||||||
let _amount = stream.read(&mut buffer);
|
let _amount = stream.read(&mut buffer);
|
||||||
let petition = String::from_utf8_lossy(&buffer[..]);
|
let petition = String::from_utf8_lossy(&buffer[..]);
|
||||||
@@ -27,6 +25,13 @@ fn process_petition(stream: &mut TcpStream) -> ProcessedResponse {
|
|||||||
let response_status = "200 OK";
|
let response_status = "200 OK";
|
||||||
|
|
||||||
let response_content = fs::read_to_string("./routes/index.html").unwrap();
|
let response_content = fs::read_to_string("./routes/index.html").unwrap();
|
||||||
|
let route = self.get_route(petition_parsed.request.query.path);
|
||||||
|
|
||||||
|
if let Some(route) = route {
|
||||||
|
// TODO: call function and generate response
|
||||||
|
} else {
|
||||||
|
// TODO: return not found
|
||||||
|
}
|
||||||
|
|
||||||
let response: ProcessedResponse = ProcessedResponse {
|
let response: ProcessedResponse = ProcessedResponse {
|
||||||
data: format!(
|
data: format!(
|
||||||
@@ -49,6 +54,5 @@ fn process_petition(stream: &mut TcpStream) -> ProcessedResponse {
|
|||||||
response
|
response
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -21,6 +21,9 @@ fn principal() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// NOTE: example function:
|
||||||
|
// let response_content = fs::read_to_string("./routes/index.html").unwrap();
|
||||||
|
|
||||||
pub fn add(left: u64, right: u64) -> u64 {
|
pub fn add(left: u64, right: u64) -> u64 {
|
||||||
left + right
|
left + right
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user