Browse Source

Add add_file_contents()

master
Noelle 4 years ago
parent
commit
df4c961008
2 changed files with 15 additions and 15 deletions
  1. 7
    3
      src/lib.rs
  2. 8
    12
      src/main.rs

+ 7
- 3
src/lib.rs View File

@@ -1,4 +1,4 @@
use std::thread;
use std::{thread, fs};
use std::sync::{mpsc, Mutex, Arc};

enum Message {
@@ -137,6 +137,10 @@ impl Response {
pub fn get_content(&self) -> &String {
&self.content
}
pub fn add_file_content(&mut self, filename: &str) {
self.set_content(fs::read_to_string(p).unwrap().as_str());
}
}

#[derive(Copy, Clone)]
@@ -157,7 +161,7 @@ impl Route {
self.path = p;
}

pub fn get_path(&mut self) -> &'static str {
pub fn get_path(&self) -> &'static str {
&self.path
}

@@ -165,7 +169,7 @@ impl Route {
self.action = a;
}

pub fn get_action(&mut self) -> fn(&&str, &mut Response) {
pub fn get_action(&self) -> fn(&&str, &mut Response) {
self.action
}
}

+ 8
- 12
src/main.rs View File

@@ -47,7 +47,7 @@ fn handle_connection(mut stream: TcpStream) {
None => {
println!("This wasn't even a well-formed header");
r.set_header("HTTP/1.1 404 NOT FOUND\r\n\r\n");
response_add_file("404.html", &mut r);
r.add_file_content("404.html");
}
}
@@ -55,7 +55,7 @@ fn handle_connection(mut stream: TcpStream) {
} else {
println!("It didn't start with GET!");
r.set_header("HTTP/1.1 404 NOT FOUND\r\n\r\n");
response_add_file("404.html", &mut r);
r.add_file_content("404.html");
}
// println!("Sending {}: {}", r.header, r.path);
@@ -69,10 +69,6 @@ fn handle_connection(mut stream: TcpStream) {
// println!("Request: {}", String::from_utf8_lossy(&buffer[..]));
}

fn response_add_file(p: &str, r: &mut Response) {
r.set_content(fs::read_to_string(p).unwrap().as_str());
}

fn route(c: &str, mut r: &mut Response) {

let mut routes = vec![];
@@ -86,10 +82,10 @@ fn route(c: &str, mut r: &mut Response) {
if c == "" {
println!("Asked to send root!");
r.set_header("HTTP/1.1 200 OK\r\n\r\n");
response_add_file("index.html", &mut r);
r.add_file_content("index.html");
} else if c.starts_with("api/") {
let mut iter_routes = routes.into_iter();
let valid_route = iter_routes.find(|&&x| x.get_path() == c);
let valid_route = iter_routes.find(|x| x.get_path() == c);
match valid_route {
Some(rt) => {
(rt.get_action())(&c, &mut r);
@@ -103,7 +99,7 @@ fn route(c: &str, mut r: &mut Response) {
} else {
println!("Asked to send {} and I could not find it", &c);
r.set_header("HTTP/1.1 404 NOT FOUND\r\n\r\n");
response_add_file("404.html", &mut r);
r.add_file_contents("404.html");
}

}
@@ -118,16 +114,16 @@ fn route_basic(c: &&str, mut r: &mut Response) {
if Path::new(&cs).exists() {
println!("Asked to send {}", &cs);
r.set_header("HTTP/1.1 200 OK\r\n\r\n");
response_add_file(&cs, &mut r);
r.add_file_contents(&cs);
} else {
println!("Asked to send {} and I couldn't find it", &cs);
r.set_header("HTTP/1.1 404 NOT FOUND\r\n\r\n");
response_add_file("404.html", &mut r);
r.add_file_contents("404.html");
}
} else {
println!("Asked to send {}", &c);
r.set_header("HTTP/1.1 200 OK\r\n\r\n");
response_add_file(&c, &mut r);
r.add_file_contents(&c);
}
}


Loading…
Cancel
Save