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

use std::thread;
use std::{thread, fs};
use std::sync::{mpsc, Mutex, Arc}; use std::sync::{mpsc, Mutex, Arc};


enum Message { enum Message {
pub fn get_content(&self) -> &String { pub fn get_content(&self) -> &String {
&self.content &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)] #[derive(Copy, Clone)]
self.path = p; self.path = p;
} }


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


self.action = a; self.action = a;
} }


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

+ 8
- 12
src/main.rs View File

None => { None => {
println!("This wasn't even a well-formed header"); println!("This wasn't even a well-formed header");
r.set_header("HTTP/1.1 404 NOT FOUND\r\n\r\n"); 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");
} }
} }
} else { } else {
println!("It didn't start with GET!"); println!("It didn't start with GET!");
r.set_header("HTTP/1.1 404 NOT FOUND\r\n\r\n"); 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); // println!("Sending {}: {}", r.header, r.path);
// println!("Request: {}", String::from_utf8_lossy(&buffer[..])); // 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) { fn route(c: &str, mut r: &mut Response) {


let mut routes = vec![]; let mut routes = vec![];
if c == "" { if c == "" {
println!("Asked to send root!"); println!("Asked to send root!");
r.set_header("HTTP/1.1 200 OK\r\n\r\n"); 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/") { } else if c.starts_with("api/") {
let mut iter_routes = routes.into_iter(); 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 { match valid_route {
Some(rt) => { Some(rt) => {
(rt.get_action())(&c, &mut r); (rt.get_action())(&c, &mut r);
} else { } else {
println!("Asked to send {} and I could not find it", &c); println!("Asked to send {} and I could not find it", &c);
r.set_header("HTTP/1.1 404 NOT FOUND\r\n\r\n"); 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");
} }


} }
if Path::new(&cs).exists() { if Path::new(&cs).exists() {
println!("Asked to send {}", &cs); println!("Asked to send {}", &cs);
r.set_header("HTTP/1.1 200 OK\r\n\r\n"); r.set_header("HTTP/1.1 200 OK\r\n\r\n");
response_add_file(&cs, &mut r);
r.add_file_contents(&cs);
} else { } else {
println!("Asked to send {} and I couldn't find it", &cs); println!("Asked to send {} and I couldn't find it", &cs);
r.set_header("HTTP/1.1 404 NOT FOUND\r\n\r\n"); 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 { } else {
println!("Asked to send {}", &c); println!("Asked to send {}", &c);
r.set_header("HTTP/1.1 200 OK\r\n\r\n"); 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