Skip to content

Commit

Permalink
Finish version 0.1.1, clean stuff upp and add image serving
Browse files Browse the repository at this point in the history
  • Loading branch information
MSKatKing committed Feb 24, 2024
1 parent 1d18056 commit 6b666f8
Show file tree
Hide file tree
Showing 19 changed files with 350 additions and 102 deletions.
2 changes: 2 additions & 0 deletions .cargo/config.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[target.x86_64-unknown-linux-musl]
linker = "rust-lld"
28 changes: 0 additions & 28 deletions Cargo.lock

This file was deleted.

4 changes: 3 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
workspace = { members = ["thread_helper"] }
[package]
name = "backend_web_server"
version = "0.1.0"
version = "0.1.1"
edition = "2021"
authors = ["MSKatKing"]

[[bin]]
name = "backend_web_server"
path = "src/main.rs"



# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
Expand Down
54 changes: 37 additions & 17 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use std::{fs, io, thread};
use std::io::{BufRead, BufReader, Write};
use std::fs::File;
use std::io::{BufRead, BufReader, Read, Write};
use std::net::{TcpListener, TcpStream};
use thread_helper::ThreadPool;
use lazy_static::lazy_static;
Expand All @@ -9,18 +10,25 @@ lazy_static!{
let page = fs::read_to_string("website/404.html").ok()?;
Some(format!("HTTP/1.1 404 NOT FOUND\r\nContent-Len: {}\r\n\r\n{page}", page.len()))
};

static ref SERVER_ERR_PAGE: Option<String> = {
let page = fs::read_to_string("website/500.html").ok()?;
Some(format!("HTTP/1.1 500 Internal Server Error\r\nContent-Len: {}\r\n\r\n{page}", page.len()))
};
}

enum ConnectionError {
TCPReadFailed,
HTMLNotFound
SourceNotFound,
InternalServerErr,
}

impl ConnectionError {
fn get_html_err_msg(&self) -> &[u8] {
match self {
ConnectionError::TCPReadFailed => "HTTP/1.1 400 BAD REQUEST".as_bytes(),
ConnectionError::HTMLNotFound => ERR_PAGE.as_ref().map_or_else(|| "HTTP/1.1 404 NOT FOUND".as_bytes(), |s| s.as_bytes()),
ConnectionError::SourceNotFound => ERR_PAGE.as_ref().map_or_else(|| "HTTP/1.1 404 NOT FOUND".as_bytes(), |s| s.as_bytes()),
ConnectionError::InternalServerErr => SERVER_ERR_PAGE.as_ref().map_or_else(|| "HTTP/1.1 500 Internal Server Error".as_bytes(), |s| s.as_bytes()),
}
}
}
Expand All @@ -33,25 +41,26 @@ fn main() {
Err(_) => {
println!("Error! Unable to find the website! (It should be in a folder \"/website\" in the same folder this file is)");
finish_wait();
std::process::exit(404);
}
}

let listener = TcpListener::bind("127.0.0.1:8080").unwrap();
let listener = TcpListener::bind("127.0.0.1:8080").map_err(|_| {
println!("Error! Unable to bind to port 127.0.0.1:8080!");
finish_wait();
}).unwrap();
let pool = ThreadPool::new(9);

let input_thread = thread::spawn(move || {
let mut input = String::new();
loop {
io::stdin().read_line(&mut input).expect("Failed to read line from stdin");
io::stdin().read_line(&mut input).map_or_else(|_| 0, |s| s);
if input.trim() == "stop" {
println!("Stopping the web server...");
break;
}
input.clear();
}
finish_wait();
std::process::exit(0);
});

println!("Successfully started! Listening on: 127.0.0.1:8080...");
Expand All @@ -62,15 +71,13 @@ fn main() {
Err(_) => continue,
};

//println!("Connection established! ({})", stream.peer_addr().unwrap().to_string());

pool.execute(move || {
let result = handle_connection(&mut stream);
match result {
Ok(response) => stream.write_all(response.as_bytes()).unwrap_or(()),
Err(e) => stream.write_all(e.get_html_err_msg()).unwrap_or(()),
}
stream.flush().unwrap();
Ok(response) => stream.write(response.as_bytes()).unwrap_or(0),
Err(e) => stream.write(e.get_html_err_msg()).unwrap_or(0),
};
stream.flush().unwrap_or(());
});
}

Expand All @@ -96,20 +103,32 @@ fn handle_connection(mut stream: &mut TcpStream) -> Result<String, ConnectionErr
}.ok_or(ConnectionError::TCPReadFailed)?;

if path.contains(".css") {
let css_contents = fs::read_to_string(format!("website{path}")).ok().ok_or(ConnectionError::HTMLNotFound)?;
let css_contents = fs::read_to_string(format!("website{path}")).ok().ok_or(ConnectionError::SourceNotFound)?;
return Ok(format!("HTTP/1.1 200 OK\r\nContent-Type: text/css\r\nContent-Length: {}\r\n\r\n{css_contents}", css_contents.len()))
}
if path.contains(".png") {
let img_contents = fs::read_to_string(format!("website{path}")).ok().ok_or(ConnectionError::HTMLNotFound)?;
return Ok(format!("HTTP/1.1 200 OK\r\nContent-Type: image/png\r\nContent-Length: {}\r\n\r\n{img_contents}", img_contents.len()))
let mut img_contents = vec![];
File::open(format!("website{}", path)).ok().ok_or(ConnectionError::SourceNotFound)?.read_to_end(&mut img_contents).ok().ok_or(ConnectionError::SourceNotFound)?;

stream.write_all(format!("HTTP/1.1 200 OK\r\nContent-Type: image/png\r\nContent-Length: {}\r\n\r\n", img_contents.len()).as_bytes()).unwrap_or(());
stream.write(&img_contents).unwrap_or(0);
return Ok("".to_string());
}
if path.contains(".ico") {
let mut img_contents = vec![];
File::open(format!("website{}", path)).ok().ok_or(ConnectionError::SourceNotFound)?.read_to_end(&mut img_contents).ok().ok_or(ConnectionError::SourceNotFound)?;

stream.write_all(format!("HTTP/1.1 200 OK\r\nContent-Type: image/vnd.microsoft.icon\r\nContent-Length: {}\r\n\r\n", img_contents.len()).as_bytes()).unwrap_or(());
stream.write(&img_contents).unwrap_or(0);
return Ok("".to_string());
}

if path == "/" {
path = "/home".to_string();
}

let status_line = "HTTP/1.1 200 OK";
let html_contents = fs::read_to_string(format!("website{path}.html")).ok().ok_or(ConnectionError::HTMLNotFound)?;
let html_contents = fs::read_to_string(format!("website{path}.html")).ok().ok_or(ConnectionError::SourceNotFound)?;

Ok(format!("{status_line}\r\nContent-Type: text/html\r\nContent-Length: {}\r\n\r\n{html_contents}", html_contents.len()))
}
Expand All @@ -118,4 +137,5 @@ fn finish_wait() {
println!("Press enter to continue...");
let mut temp = String::new();
io::stdin().read_line(&mut temp).unwrap();
std::process::exit(0);
}
2 changes: 0 additions & 2 deletions thread_helper/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,6 @@ impl Drop for ThreadPool {

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn it_works() {
}
Expand Down
15 changes: 15 additions & 0 deletions website/404.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
.body {
position: fixed;
top: 7%;
width: 100%;
background: white;
}

.fader {
position: fixed;
left: 0;
bottom: 10%;
width: 100%;
height: 60px;
background-image: linear-gradient(white,#73A0C7);
}
63 changes: 39 additions & 24 deletions website/404.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,42 @@
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Simple 404 Error Page</title>
<link rel="stylesheet" href="404.css">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/css/bootstrap.min.css">
</head><body>
<section class="page_404">
<div class="container">
<div class="row">
<div class="col-sm-12 ">
<div class="col-sm-10 col-sm-offset-1 text-center">
<div class="four_zero_four_bg">
<h1 class="text-center ">404</h1>
</div> <div class="contant_box_404">
<h3 class="h2">
Look like you're lost
</h3> <p>the page you are looking for not available!</p> <a href="/">Go to Home</a>
</div>
</div>
</div>
</div>
</div>
</section>
</body></html>
<title>Error 404: Not Found</title>
<link rel="icon" type="image/png" href="images/favicon.png">
<link rel="stylesheet" type="text/css" href="/global.css">
<link rel="stylesheet" type="text/css" href="/404.css">
<link rel="stylesheet" type="text/css" href="/__templates__/header.css">
<link rel="stylesheet" type="text/css" href="/__templates__/footer.css">
</head>
<body>
<div class="header">
<nav>
<div class="logo-container"><a href="/"><img src="/images/favicon.png" class="logo"></a></div>
<a href="/" class="logo-title">BlackNinja745 Studios</a>
<ul class="nav-links">
<li><a href="/about-us" class="red_under">About</a></li>
<li><a href="/youtube" class="red_under">YouTube</a></li>
<li><a href="/minecraft" class="red_under">Minecraft</a></li>
<li><a href="/development" class="red_under">Development</a></li>
</ul>
</nav>
<hr class="separator">
</div>


<h1 id="title" class="centered">Uh oh!</h1>
<h3 class="centered">Error 404: Page Not Found</h3>
<img src="/images/err404.png" class="centered">
<h2 class="centered">Not expecting this? Neither were we.</h2>
<p class="centered">This error occurs when you try to access something that doesn't exist!</p>
<p class="centered">If you typed this link into the search bar, ensure spelling is correct</p>
<p class="centered">If you clicked a link that brought you here, that link is expired!</p>


<hr class="separator">
<div class="footer">
<p class="centered">Copyright © 2024 BlackNinja745 Studios</p>
<a href="/about-us">About Us</a>
</div>
</body>
</html>
17 changes: 17 additions & 0 deletions website/__templates__/footer.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
.footer {
position: absolute;
bottom: 0;
width: 100%;
padding: 0 8%;
height: 5rem;
background: #080008;
}

#footer_copyright {
color: white;
font-family: "poppins", sans-serif;
}

#footer_contact-us {
font-family: "poppins", sans-serif;
}
5 changes: 5 additions & 0 deletions website/__templates__/footer.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<hr class="separator">
<div class="footer">
<a href="mailto:bn745studiosmanagement@gmail.com">Contact Us</a>
<p class="centered">Copyright © 2024 BlackNinja745 Studios</p>
</div>
88 changes: 88 additions & 0 deletions website/__templates__/header.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}

.header {
height: 6%;
width: 100%;
padding: 0 8%;
top: 0;
left: 0;
background: #080008;
position: relative;
}

.header a {
font-family: "poppins", sans-serif;
}

nav {
display: flex;
align-items: center;
justify-content: space-between;
}

.logo {
width: 3vw;
}

.logo-title {
margin: 0;
left: -17vw;
font-size: 1.8vw;
padding: 0.4vw 0;
font-family: "poppins", sans-serif;
box-sizing: border-box;
background-color: rgba(0, 0, 0, 0);
text-decoration: none;
color: #fff;
position: relative;
}

.logo-container {
padding: 0;
margin: 0;
width: 0;
}

.nav-links {
padding: 1.6vw 0;
}

.nav-links li {
display: inline-block;
margin: 0 1vw;
}

.nav-links li a {
font-size: 1vw;
text-decoration: none;
color: #fff;
background-color: transparent;
padding: 0.4vw 0;
position: relative;
}

.separator {
height: 0.05vw;
margin: 0.1vw 0;
background: -webkit-gradient(linear, 0 0, 100% 0, from(rgba(0, 0, 0, 0)), color-stop(0.5, #333333), to(rgba(0, 0, 0, 0)));
background: -webkit-linear-gradient(left, rgba(0, 0, 0, 0), #333333, rgba(0, 0, 0, 0));
background: -moz-linear-gradient(left, rgba(0, 0, 0, 0), #333333, rgba(0, 0, 0, 0));
background: -o-linear-gradient(left, rgba(0, 0, 0, 0), #333333, rgba(0, 0, 0, 0));
background: linear-gradient(left, rgba(0, 0, 0, 0), #333333, rgba(0, 0, 0, 0));
border: 0;
}

.separator:after {
display: block;
content: '';
height: 3vw; /* Relative height */
background-image: -webkit-gradient(radial, 50% 0%, 0, 50% 0%, 116, color-stop(0%, #cccccc), color-stop(100%, rgba(255, 255, 255, 0)));
background-image: -webkit-radial-gradient(center top, farthest-side, #555555 0%, rgba(255, 255, 255, 0) 100%);
background-image: -moz-radial-gradient(center top, farthest-side, #555555 0%, rgba(255, 255, 255, 0) 100%);
background-image: -o-radial-gradient(center top, farthest-side, #555555 0%, rgba(255, 255, 255, 0) 100%);
background-image: radial-gradient(farthest-side at center top, #555555 0%, rgba(255, 255, 255, 0) 100%);
}
Loading

0 comments on commit 6b666f8

Please sign in to comment.