aboutsummaryrefslogtreecommitdiff
path: root/src/main.rs
diff options
context:
space:
mode:
authoromagdy7 <omar.professional8777@gmail.com>2024-06-06 22:22:58 +0300
committeromagdy7 <omar.professional8777@gmail.com>2024-06-06 22:22:58 +0300
commitf2cdc940374a2eef7cdae9a34c90a52e527a0874 (patch)
treee0c31a41251a42303514ee4f375f0b4507fdc162 /src/main.rs
parentfe378c20c645ca1cb6cc04e95c9afd4c2de5c0e8 (diff)
downloadtiny-server-f2cdc940374a2eef7cdae9a34c90a52e527a0874.tar.xz
tiny-server-f2cdc940374a2eef7cdae9a34c90a52e527a0874.zip
refactor: Changed the method struct signatrue and added a new general Endpoint struct for better ergonomic api
Diffstat (limited to 'src/main.rs')
-rw-r--r--src/main.rs170
1 files changed, 5 insertions, 165 deletions
diff --git a/src/main.rs b/src/main.rs
index 250f94b..1b08605 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -1,170 +1,10 @@
-#![allow(unused)]
+use http_server_starter_rust::handlers::*;
+use http_server_starter_rust::http_types::{get, post};
use http_server_starter_rust::router::Router;
-use itertools::Itertools;
-use nom::AsBytes;
-use std::collections::HashMap;
-use std::fs::File;
-use std::io::{self, Read, Write};
-use std::net::{IpAddr, Ipv4Addr, SocketAddr, TcpListener, TcpStream};
-use std::str::Utf8Error;
-use std::sync::{Arc, Mutex};
-use std::{str, thread, usize};
-
-use http_server_starter_rust::request::*;
-use http_server_starter_rust::response::*;
use http_server_starter_rust::server::*;
-use http_server_starter_rust::utils::*;
-use http_server_starter_rust::{extractor, http_types::*};
-
-fn handle_echo(request: &Request, ctx: Option<&HashMap<String, String>>) -> Response {
- let mut headers = HashMap::new();
- let mut echo_string = "".to_string();
- let route = match request.method() {
- Method::Get(route) | Method::Post(route) | Method::Put(route) => route,
- };
-
- let mut body = vec![];
-
- for ch in route.chars().skip(1).skip_while(|&ch| ch != '/').skip(1) {
- echo_string.push(ch);
- }
- if echo_string.chars().last().unwrap() == '/' {
- echo_string.pop();
- }
-
- if let Some(encoding) = request.get_tag("Accept-Encoding") {
- if encoding.contains("gzip") {
- headers.insert("Content-Encoding".to_string(), "gzip".to_string());
- match encode_gzip_string(echo_string.as_str()) {
- Ok(encoded_bytes) => {
- println!("In succses");
- let len = encoded_bytes.len();
- body = encoded_bytes;
- headers.insert("Content-Length".to_string(), len.to_string());
- }
- Err(err) => {
- println!("In error {}", &echo_string);
- println!("Error: {err}");
- }
- }
- } else {
- let len = echo_string.len();
- headers.insert("Content-Length".to_string(), len.to_string());
- body = echo_string.as_bytes().to_owned();
- }
- } else {
- let len = echo_string.len();
- headers.insert("Content-Length".to_string(), len.to_string());
- body = echo_string.as_bytes().to_owned();
- }
-
- headers.insert("Content-Type".to_string(), "text/plain".to_string());
-
- Response::new(
- "1.1".to_string(),
- StatusCode::Ok,
- Some(Headers(headers)),
- Some(body),
- )
-}
-
-fn handle_post_files(request: &Request, ctx: Option<&HashMap<String, String>>) -> Response {
- // Extract the route regardless of the variant
- let mut file = "".to_string();
- let route = match request.method() {
- Method::Get(route) | Method::Post(route) | Method::Put(route) => route,
- };
-
- let mut directory = ctx.unwrap().get(&"dir".to_string()).unwrap().clone();
- directory.pop(); // remove last slash
-
- for ch in route.chars().skip(1).skip_while(|&ch| ch != '/') {
- file.push(ch);
- }
- if file.chars().last().unwrap() == '/' {
- file.pop();
- }
- let len = file.len().to_string();
-
- let full_path = &(directory + &file);
- let bytes = request.body().as_ref().unwrap();
- let body = bytes.as_bytes();
-
- match save_bytes_to_file(body, full_path) {
- Ok(bytes) => Response::new("1.1".to_string(), StatusCode::Created, None, None),
- Err(err) => {
- println!("Error: {err}");
- Response::new("1.1".to_string(), StatusCode::NotFound, None, None)
- }
- }
-}
-
-fn handle_files(request: &Request, ctx: Option<&HashMap<String, String>>) -> Response {
- // Extract the route regardless of the variant
- let mut file = "".to_string();
- let route = match request.method() {
- Method::Get(route) | Method::Post(route) | Method::Put(route) => route,
- };
-
- let mut directory = ctx.unwrap().get(&"dir".to_string()).unwrap().clone();
- directory.pop(); // remove last slash
-
- for ch in route.chars().skip(1).skip_while(|&ch| ch != '/') {
- file.push(ch);
- }
- if file.chars().last().unwrap() == '/' {
- file.pop();
- }
- let len = file.len().to_string();
-
- let full_path = &(directory + &file);
-
- match read_file_as_bytes(full_path) {
- Ok(bytes) => {
- let mut headers = HashMap::new();
- headers.insert(
- "Content-Type".to_string(),
- "application/octet-stream".to_string(),
- );
- headers.insert("Content-Length".to_string(), bytes.len().to_string());
- let body = bytes;
- Response::new(
- "1.1".to_string(),
- StatusCode::Ok,
- Some(Headers(headers)),
- Some(body),
- )
- }
- Err(_) => Response::new("1.1".to_string(), StatusCode::NotFound, None, None),
- }
-}
-
-fn handle_user_agent(request: &Request, ctx: Option<&HashMap<String, String>>) -> Response {
- let mut headers = HashMap::new();
- if let Some(user_agent) = request.get_tag("User-Agent") {
- let len = user_agent.len().to_string();
- headers.insert("Content-Type".to_string(), "text/plain".to_string());
- headers.insert("Content-Length".to_string(), len);
- let body = user_agent.as_bytes().to_owned();
- Response::new(
- "1.1".to_string(),
- StatusCode::Ok,
- Some(Headers(headers)),
- Some(body),
- )
- } else {
- println!("User-Agent isn't present in headers");
- Response::new("1.1".to_string(), StatusCode::BadRequest, None, None)
- }
-}
-
-fn handle_success(request: &Request, ctx: Option<&HashMap<String, String>>) -> Response {
- Response::new("1.1".to_string(), StatusCode::Ok, None, None).into()
-}
-
-fn handle_not_found(request: Request, ctx: Option<&HashMap<String, String>>) -> Response {
- Response::new("1.1".to_string(), StatusCode::NotFound, None, None).into()
-}
+use std::collections::HashMap;
+use std::io::{self};
+use std::net::{IpAddr, Ipv4Addr, SocketAddr};
fn main() -> io::Result<()> {
// Collect the command-line arguments