rusty_render/
lib.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
use std::{
    fs,
    io::{Read, Write},
    net::TcpStream,
};

use http_scrap::Response;

pub struct Render<'a> {
    pub stream: &'a TcpStream,
    pub error_page_path: String,
}

impl<'a> Render<'a> {
    pub fn new(mut stream: &'a TcpStream, error_page: &str) -> Self {
        Render {
            stream,
            error_page_path: (error_page).to_string(),
        }
    }
    pub fn read_page(&mut self) {
        let mut buffer = [0; 1024];
        // println!("{:?}", self.stream);
        self.stream.read(&mut buffer);
        let response = String::from_utf8_lossy(&buffer);
        let response = Response::new(&response);
        let file_path = format!("/src/app/{}/page.tsx", response.path());
        let path = fs::read_to_string(file_path);
        match path {
            Ok(path) => {
                let path = format!(
                    "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\nContent-Length: {}\r\n\r\n{}",
                    path.len(),
                    path
                );
                self.stream.write_all(path.as_bytes());
                self.stream.flush();
            }
            Err(_) => {
                let path = fs::read_to_string("static/page.html");
                let page = match path {
                    Ok(err) => {
                        let path = format!(
                        "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\nContent-Length: {}\r\n\r\n{}",
                        err.len(),
                        err
                    );
                        // path
                        self.stream.write_all(path.as_bytes());
                        self.stream.flush();
                    }
                    Err(err) => {
                        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>");
                        let response = format!(
                            "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\nContent-Length: {}\r\n\r\n{}",
                            file.len(),
                            file
                        );
                        println!("{}", response);
                        // response
                        self.stream.write_all(response.as_bytes());
                        self.stream.flush();
                    }
                };
            }
        }
    }
}