1use std::{
2 fs,
3 io::{Read, Write},
4 net::TcpStream,
5 sync::{Arc, Mutex},
6};
7pub mod google;
8use http_scrap::Response;
9
10pub struct Render {
11 pub stream: Arc<Mutex<TcpStream>>,
12 pub error_page_path: String,
13}
14
15impl Render {
16 pub fn new(mut stream: Arc<Mutex<TcpStream>>, error_page: &str) -> Self {
17 Render {
19 stream,
20 error_page_path: (error_page).to_string(),
21 }
22 }
23 pub fn read_page(&mut self) {
24 let mut buffer = [0; 1024];
25 let mut stream = self.stream.lock().unwrap();
28 stream.read(&mut buffer);
29
30 let response = String::from_utf8_lossy(&buffer);
31 let response = Response::new(&response);
32
33 let path = response.path();
34
35 if !path.starts_with("/api") && !path.starts_with("/favicon") {
36 let file_path = format!("src/app/{}/page.html", response.path());
44 let path = fs::read_to_string(file_path);
45 match path {
46 Ok(path) => {
47 let path = format!(
48 "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\nContent-Length: {}\r\n\r\n{}",
49 path.len(),
50 path
51 );
52 stream.write_all(path.as_bytes());
53 stream.flush();
54 }
55 Err(_) => {
56 let path = fs::read_to_string("static/page.html");
57 let page = match path {
58 Ok(err) => {
59 let path = format!(
60 "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\nContent-Length: {}\r\n\r\n{}",
61 err.len(),
62 err
63 );
64 stream.write_all(path.as_bytes());
66 stream.flush();
67 }
68 Err(err) => {
69 let file = format!("<!DOCTYPE html><html lang='en'><head><meta charset='UTF-8' /><meta name='viewport' content='width=device-width, initial-scale=1.0' /><title>404 - Page Not Found</title><style>body {{font-family: Arial, sans-serif;background-color: #f0f0f0;text-align: center;padding: 50px;}}h1 {{font-size: 50px;color: #333;}}p {{font-size: 18px;color: #666;}}a {{color: #007bff;text-decoration: none;}}a:hover {{text-decoration: underline;}}</style></head><body><h1>404</h1><p>Oops! The page you're looking for doesn't exist.</p><p><a href='/'>Go back to the homepage</a></p></body></html>");
70 let response = format!(
71 "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\nContent-Length: {}\r\n\r\n{}",
72 file.len(),
73 file
74 );
75 stream.write_all(response.as_bytes());
78 stream.flush();
79 }
80 };
81 }
82 }
83 }
84 }
85}