Compare commits

..

15 Commits

Author SHA1 Message Date
calcu1on
86d58c3f8a Adding all sorts of goodies. 2025-04-27 11:34:30 -04:00
calcu1on
45375e0fa3 Adding current working progress to hashmap for dynamic routes. 2025-04-26 10:13:56 -04:00
calcu1on
967de436f6 Adding package file to theme and using scss instead of css. 2025-04-21 08:13:05 -04:00
calcu1on
3bd38b2d80 Cleanuo 2025-04-20 23:46:48 -04:00
calcu1on
99ea2cf3ad Rename project and add handling for images and assets. 2025-04-20 23:43:18 -04:00
calcu1on
92075763db Commiting changes 2025-04-11 16:39:17 -04:00
calcu1on
4e3306c5dd Merge pull request #7 from calcu1on/issue-6
Initial start of the route handling for DAM assets.
2025-01-05 19:53:56 -05:00
calcu1on
f47ebb8633 more updates. 2025-01-05 19:56:42 -05:00
calcu1on
3c66a6b6bc Initial start of the route handling for DAM assets. 2025-01-05 11:26:21 -05:00
calcu1on
d1ac420503 Merge pull request #5 from calcu1on/better-404
better handlign of something.
2024-12-22 21:57:49 -05:00
calcu1on
c5e38f29a1 better handlign of something. 2024-12-22 21:59:40 -05:00
calcu1on
56cca06199 API Key Handling and some more dynamic functions. 2024-12-22 21:43:52 -05:00
calcu1on
4b019c85d1 Merge pull request #4 from calcu1on/issue-2
API Key Handling and some more dynamic functions.
2024-12-22 21:42:27 -05:00
calcu1on
7c58f9db51 Switch to json 404. 2024-12-22 20:37:04 -05:00
calcu1on
411efe5276 Merge pull request #2 from calcu1on/issue-1
Switch to json 404.
2024-12-22 20:35:22 -05:00
25 changed files with 687 additions and 35096 deletions

2
.gitignore vendored
View File

@@ -2,6 +2,7 @@
# will have compiled files and executables
debug/
target/
.DS_STORE
# Remove Cargo.lock from gitignore if creating an executable, leave it for libraries
# More information here https://doc.rust-lang.org/cargo/guide/cargo-toml-vs-cargo-lock.html
@@ -19,6 +20,7 @@ Cargo.lock
# and can be added to the global gitignore or merged into this file. For a more nuclear
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
#.idea/
keys.txt
# Added by cargo

View File

@@ -1,6 +1,10 @@
[package]
name = "hermes"
name = "batflip"
version = "0.1.0"
edition = "2021"
[dependencies]
base64 = "0.22.1"
rand = "0.8"
regex = "1.11.1"
sqlite = "0.36.1"

File diff suppressed because it is too large Load Diff

View File

@@ -1,9 +0,0 @@
<!Doctype html>
<html>
<head>
<title>Page Not Found | Hermes</title>
</head>
<body>
<p>The requested page could not be found.</p>
</body>
</html>

1
public/assets/main.js Normal file
View File

@@ -0,0 +1 @@
console.log("main.js loaded");

168
public/assets/style.css Normal file
View File

@@ -0,0 +1,168 @@
@import url("https://fonts.googleapis.com/css2?family=Bebas+Neue&display=swap");
@import url("https://fonts.googleapis.com/css2?family=Anton&family=Bebas+Neue&display=swap");
:root {
--blue: #002D62;
--red: #C8102E;
--green: #4CAF50;
--background: #F5F5F5;
--primary-text: #1A1A1A;
--secondary-text: #555555;
--success: #2E8B57;
--warning: #FFA500;
--error: #D72638;
--primary-font: "Bebas Neue", sans-serif;
--secondary-font: "Anton", sans-serif;
}
/* 1. Use a more-intuitive box-sizing model */
*, *::before, *::after {
box-sizing: border-box;
}
/* 2. Remove default margin */
* {
margin: 0;
}
/* 3. Enable keyword animations */
@media (prefers-reduced-motion: no-preference) {
html {
interpolate-size: allow-keywords;
}
}
body {
/* 4. Add accessible line-height */
line-height: 1.5;
/* 5. Improve text rendering */
-webkit-font-smoothing: antialiased;
}
/* 6. Improve media defaults */
img, picture, video, canvas, svg {
display: block;
max-width: 100%;
}
/* 7. Inherit fonts for form controls */
input, button, textarea, select {
font: inherit;
}
/* 8. Avoid text overflows */
p, h1, h2, h3, h4, h5, h6 {
overflow-wrap: break-word;
}
/* 9. Improve line wrapping */
p {
text-wrap: pretty;
}
h1, h2, h3, h4, h5, h6 {
text-wrap: balance;
}
/*
10. Create a root stacking context
*/
#root, #__next {
isolation: isolate;
}
header {
background: var(--blue);
padding: 20px;
}
header #site-name {
font-family: var(--secondary-font);
font-size: 2rem;
letter-spacing: 1px;
}
header a {
color: var(--background);
}
h1, h2, h3, h4, h5, h6 {
font-family: var(--primary-font);
}
h1 {
font-size: 3rem;
font-weight: 700;
}
h2 {
font-size: 2.5rem;
font-weight: 700;
}
h3 {
font-size: 2rem;
font-weight: 500;
}
h4 {
font-size: 1.5rem;
font-weight: 500;
}
h5 {
font-size: 1.2rem;
font-weight: 500;
}
h6 {
font-size: 1rem;
font-weight: 500;
}
.styled-table {
border-collapse: collapse;
margin: 25px 0;
font-size: 0.9em;
font-family: sans-serif;
min-width: 400px;
box-shadow: 0 0 20px rgba(0, 0, 0, 0.15);
}
.styled-table thead tr {
background-color: #009879;
color: #ffffff;
text-align: left;
}
.styled-table th,
.styled-table td {
padding: 12px 15px;
}
.styled-table tbody tr {
border-bottom: 1px solid #dddddd;
}
.styled-table tbody tr:nth-of-type(even) {
background-color: #f3f3f3;
}
.styled-table tbody tr:last-of-type {
border-bottom: 2px solid #009879;
}
.styled-table tbody tr.active-row {
font-weight: bold;
color: #009879;
}
body {
background: var(--background);
color: var(--primary-text);
padding: 20px;
font-size: 1.2rem;
}
img {
max-width: 100%;
display: block;
}
/*# sourceMappingURL=style.css.map */

View File

@@ -0,0 +1 @@
{"version":3,"sourceRoot":"","sources":["../../theme/styles.scss","../../theme/variables.scss","../../theme/reset.scss","../../theme/header.scss","../../theme/headings.scss","../../theme/tables.scss"],"names":[],"mappings":"AAAQ;AACA;ACAR;EACE;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;;;ACZF;AACA;EACE;;;AAGF;AACA;EACE;;;AAGF;AACA;EACE;IACE;;;AAIJ;AACE;EACA;AACA;EACA;;;AAGF;AACA;EACE;EACA;;;AAGF;AACA;EACE;;;AAGF;AACA;EACE;;;AAGF;AACA;EACE;;;AAEF;EACE;;;AAGF;AAAA;AAAA;AAGA;EACE;;;ACpDF;EACE;EACA;;AAEA;EACE;EACA;EACA;;AAGF;EACE;;;ACXJ;EACE;;;AAGF;EACE;EACA;;;AAIF;EACE;EACA;;;AAGF;EACE;EACA;;;AAGF;EACE;EACA;;;AAIF;EACE;EACA;;;AAIF;EACE;EACA;;;AClCF;EACI;EACA;EACA;EACA;EACA;EACA;;;AAGJ;EACI;EACA;EACA;;;AAGJ;AAAA;EAEI;;;AAGJ;EACI;;;AAGJ;EACI;;;AAGJ;EACI;;;AAGJ;EACI;EACA;;;AL1BJ;EACE;EACA;EACA;EACA;;;AAKF;EACE;EACA","file":"style.css"}

View File

@@ -0,0 +1,5 @@
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<link rel="stylesheet" href="./assets/style.css" />
<script src="https://cdn.jsdelivr.net/npm/@tailwindcss/browser@4"></script>
<script src="./assets/main.js"></script>

BIN
public/images/6.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 256 KiB

BIN
public/images/fenway.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 594 KiB

32
public/pages/404.html Normal file
View File

@@ -0,0 +1,32 @@
<!Doctype html>
<html>
<head>
<title>Page Not Found</title>
<link rel="stylesheet" href="./assets/style.css" />
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<script src="https://cdn.jsdelivr.net/npm/@tailwindcss/browser@4"></script>
<script src="./assets/main.js"></script>
</head>
<body>
<header id="site-header">
<div id="site-name">
<a href="/">BatFlip</a>
</div>
<div id="site-menu">
<ul>
<li><a href="/scorecards">Past Scorecards</a></li>
<li><a href="/add-scorecard">Add New</a></li>
</ul>
</div>
</header>
<div id="main-content">
<h1>Sorry, this page cannot be found.</h1>
<h2>Sorry, this page cannot be found.</h2>
<h3>Sorry, this page cannot be found.</h3>
<h4>Sorry, this page cannot be found.</h4>
<h5>Sorry, this page cannot be found.</h5>
<h6>Sorry, this page cannot be found.</h6>
</div>
</body>
</html>

View File

@@ -0,0 +1,38 @@
<!DOCTYPE html>
<html>
<head>
<title>Add New Scorecard</title>
{% header %}
</head>
<body>
<header id="site-header">
<div id="site-name">
<a href="/">BatFlip</a>
</div>
<div id="site-menu">
<ul>
<li><a href="/scorecards">Past Scorecards</a></li>
</ul>
</div>
</header>
<div id="main-content">
<table id="home-scorecard" class="styled-table">
<thead>
<tr>
<th>Name</th>
<th>Date</th>
<th>Score</th>
<th>Actions</th>
</tr>
</thead
<tbody>
<tr>
<td><input type="text" name="name" placeholder="Name" /></td>
<td><input type="text" name="date" placeholder="Date" /></td>
<td><input type="text" name="score" placeholder="Score" /></td>
<td><input type="submit" value="Add" /></td>
</tbody>
</table>
</div>
</body>
</html>

24
public/pages/index.html Normal file
View File

@@ -0,0 +1,24 @@
<!DOCTYPE html>
<html>
<head>
<title>BatFlip Scoring App</title>
{% header %}
</head>
<body>
<header id="site-header">
<div id="site-name">
<a href="/">BatFlip</a>
</div>
<div id="site-menu">
<ul>
<li><a href="/scorecards">Past Scorecards</a></li>
<li><a href="/add-scorecard">Add New</a></li>
</ul>
</div>
</header>
<div id="main-content">
<h1>Live Baseball Scoring App, written in Rust</h1>
<img src="./images/fenway.jpg" alt="some image" />
</div>
</body>
</html>

View File

@@ -0,0 +1,22 @@
<!DOCTYPE html>
<html>
<head>
<title>Scorecards</title>
{% header %}
</head>
<body>
<header id="site-header">
<div id="site-name">
<a href="/">BatFlip</a>
</div>
<div id="site-menu">
<ul>
<li><a href="/add-scorecard">Add New</a></li>
</ul>
</div>
</header>
<div id="main-content">
<h1>Scorecards Go Here.</h1>
</div>
</body>
</html>

58
src/connection.rs Normal file
View File

@@ -0,0 +1,58 @@
use std::{
fs,
io::{prelude::*, BufReader},
net::TcpStream,
};
use batflip::*;
#[allow(dead_code)]
struct IncomingRequest {
req_type: String,
path: String
// headers: Vec<String>
}
// Handle the incoming connection.
pub fn handle_connection(mut stream: TcpStream) {
// Create a new buffer reader with the stream data.
let buf_reader = BufReader::new(&stream);
// Store each line in a vector.
let http_request: Vec<_> = buf_reader
.lines()
.map(|result| result.unwrap())
.take_while(|line| !line.is_empty())
.collect();
// Get the first line of the stream which contains request data.
let request_line: String = http_request.first().expect("No request line found").to_string();
let mut response;
// Split the request data on spaces into a vector.
let req_vec: Vec<&str> = request_line.split(" ").collect();
let inc_request = IncomingRequest {
req_type: req_vec[0].to_string(),
path: req_vec[1].to_string(),
};
// Handle 404.
if inc_request.path.is_empty() {
response = return_response("404");
stream.write_all(response.as_bytes()).unwrap()
}
// Handle images, else pages or assets.
if inc_request.path.contains("images") {
let mut public_route: String = "./public".to_string();
public_route.push_str(&inc_request.path);
let image_data = fs::read(&public_route).unwrap();
let status_line = "HTTP/1.1 200 OK".to_string();
let response = format!(
"{}\r\nContent-Length: {}\r\n\r\n",
status_line,
image_data.len()
);
stream.write_all(response.as_bytes()).unwrap();
stream.write_all(&image_data).unwrap()
} else {
response = return_response(&inc_request.path);
stream.write_all(response.as_bytes()).unwrap()
}
}

136
src/lib.rs Normal file
View File

@@ -0,0 +1,136 @@
use std::fs;
use std::{
sync::{mpsc, Arc, Mutex},
thread,
};
use regex::Regex;
#[allow(dead_code)]
pub struct ThreadPool {
workers: Vec<Worker>,
sender: mpsc::Sender<Job>,
}
type Job = Box<dyn FnOnce() + Send + 'static>;
impl ThreadPool {
pub fn new(size: usize) -> ThreadPool {
assert!(size > 0);
let (sender, receiver) = mpsc::channel();
let receiver = Arc::new(Mutex::new(receiver));
let mut workers = Vec::with_capacity(size);
for id in 0..size {
workers.push(Worker::new(id, Arc::clone(&receiver)));
}
ThreadPool { workers, sender }
}
pub fn process<F>(&self, f: F)
where
F: FnOnce() + Send + 'static,
{
let job = Box::new(f);
self.sender.send(job).unwrap();
}
}
#[allow(dead_code)]
struct Worker {
id: usize,
thread: thread::JoinHandle<()>,
}
impl Worker {
fn new(id: usize, receiver: Arc<Mutex<mpsc::Receiver<Job>>>) -> Worker {
let thread = thread::spawn(move || loop {
let job = receiver.lock().unwrap().recv().unwrap();
// println!("Worker {id} got a job; executing.");
job();
});
Worker { id, thread }
}
}
// Create the response for a request.
pub fn return_response(path_or_code: &str) -> String {
let status_line: String;
let contents: String;
match path_or_code {
"404" => {
status_line = "HTTP/1.1 404 Not Found".to_string();
contents = fs::read_to_string("./public/pages/404.html").unwrap();
},
&_ => {
status_line = "HTTP/1.1 200 OK".to_string();
contents = get_route_data(path_or_code);
}
}
let length = contents.len();
format!("{status_line}\r\nContent-Length: {length}\r\n\r\n{contents}")
}
// Generate the data for the given route.
pub fn get_route_data(route: &str) -> String {
match route {
"/" => {
let file_contents = fs::read_to_string("./public/pages/index.html").unwrap();
hot_swap(&file_contents)
},
"/scorecards" => {
let file_contents = fs::read_to_string("./public/pages/scorecards.html").unwrap();
hot_swap(&file_contents)
},
"/add-scorecard" => {
let file_contents = fs::read_to_string("./public/pages/add-scorecard.html").unwrap();
hot_swap(&file_contents)
},
_ if route.contains("assets") => {
let mut public_route: String = "./public".to_string();
public_route.push_str(&route);
return fs::read_to_string(&public_route).unwrap()
},
&_ => {
return fs::read_to_string("./public/pages/404.html").unwrap();
}
}
}
// Swap dynamic sections of html template.
pub fn hot_swap(source_html: &str) -> String {
let mut updated_html: String = String::from(source_html);
let re = Regex::new(r"\{%.(?<name>.*).%\}").unwrap();
for section in re.captures_iter(source_html) {
let comp_content = retrieve_component(&section["name"]);
updated_html = updated_html.replace(&section[0], &comp_content);
}
updated_html
}
// Retrieve html markup from component.
pub fn retrieve_component(component: &str) -> String {
let mut comp_path = String::from("./public/components/");
let html_ext = String::from(".html");
comp_path.push_str(&component);
comp_path.push_str(&html_ext);
fs::read_to_string(&comp_path).unwrap()
}
// Validate if a given path exists.
pub fn path_exists(path: &str) -> bool {
fs::metadata(path).is_ok()
}
#[allow(dead_code)]
pub fn get_header(headers: Vec<String>, needle: &str) -> String {
let mut value = String::new();
for header in headers {
let split_header: Vec<&str> = header.split(":").collect();
let header_key: String = split_header.first().expect("Nothing here").to_string();
if header_key == needle {
value = split_header[1].to_string();
}
}
value
}

View File

@@ -1,92 +1,17 @@
use std::{
fs,
io::{prelude::*, BufReader},
net::{TcpListener, TcpStream},
};
#[allow(dead_code)]
struct IncomingRequest {
req_type: String,
path: String
}
use std::net::TcpListener;
use batflip::ThreadPool;
// use std::thread;
pub mod connection;
// Listen on port and return response.
fn main() {
let listener = TcpListener::bind("164.92.85.112:7878").unwrap();
let listener = TcpListener::bind("127.0.0.1:6942").unwrap();
let pool = ThreadPool::new(4);
for stream in listener.incoming() {
let stream = stream.unwrap();
handle_connection(stream);
pool.process(move || {
connection::handle_connection(stream);
});
}
}
#[allow(unused_assignments)]
fn handle_connection(mut stream: TcpStream) {
let buf_reader = BufReader::new(&stream);
let http_request: Vec<_> = buf_reader
.lines()
.map(|result| result.unwrap())
.take_while(|line| !line.is_empty())
.collect();
let request_line: String = http_request.first().expect("Not a string").to_string();
let req_vec: Vec<&str> = request_line.split(" ").collect();
let inc_request = IncomingRequest {
req_type: req_vec[0].to_string(),
path: req_vec[1].to_string(),
};
let api_key: String = get_api_key(http_request);
let mut response = String::new();
// If not a GET request, or missing API key, throw 404.
if inc_request.req_type != "GET" || api_key.is_empty() {
response = return_404();
}
else {
// Check that the API key is in fact fact valuid.
// Not really checking right now.
response = return_200();
}
// If not present, return 404.
// Perhaps move both of those conditions to a new function.
stream.write_all(response.as_bytes()).unwrap();
}
// get the apy key from the headers.
fn get_api_key(headers: Vec<String>) -> String {
let mut api_key = String::new();
for header in headers {
// split header, strip whitespace, check if exists.
let split_header: Vec<&str> = header.split(":").collect();
let header_key: String = split_header.first().expect("Nothing here").to_string();
if header_key == "x-api-key" {
api_key = split_header[1].to_string();
}
}
api_key
}
// Return a 404.
fn return_404() -> String {
let status_line = "HTTP/1.1 404 Not Found";
let contents = fs::read_to_string("./public/404.html").unwrap();
let length = contents.len();
let response =
format!("{status_line}\r\nContent-Length: {length}\r\n\r\n{contents}");
response
}
// Return a 200 with the json.
fn return_200() -> String {
let status_line = "HTTP/1.1 200 OK";
let contents = fs::read_to_string("./public/200.json").unwrap();
let length = contents.len();
let response =
format!("{status_line}\r\nContent-Length: {length}\r\n\r\n{contents}");
response
}

14
theme/header.scss Normal file
View File

@@ -0,0 +1,14 @@
header {
background: var(--blue);
padding: 20px;
#site-name {
font-family: var(--secondary-font);
font-size: 2rem;
letter-spacing: 1px;
}
a {
color: var(--background);
}
}

36
theme/headings.scss Normal file
View File

@@ -0,0 +1,36 @@
h1, h2, h3, h4, h5, h6 {
font-family: var(--primary-font);
}
h1 {
font-size: 3rem;
font-weight: 700;
}
h2 {
font-size: 2.5rem;
font-weight: 700;
}
h3 {
font-size: 2rem;
font-weight: 500;
}
h4 {
font-size: 1.5rem;
font-weight: 500;
}
h5 {
font-size: 1.2rem;
font-weight: 500;
}
h6 {
font-size: 1rem;
font-weight: 500;
}

11
theme/package.json Normal file
View File

@@ -0,0 +1,11 @@
{
"name": "batflip",
"version": "1.0.0",
"description": "Theme for Batflip scoring.",
"main": "index.js",
"scripts": {
"build": "sass styles.scss ../public/assets/style.css"
},
"author": "Dan Chadwick",
"license": "MIT"
}

54
theme/reset.scss Normal file
View File

@@ -0,0 +1,54 @@
/* 1. Use a more-intuitive box-sizing model */
*, *::before, *::after {
box-sizing: border-box;
}
/* 2. Remove default margin */
* {
margin: 0;
}
/* 3. Enable keyword animations */
@media (prefers-reduced-motion: no-preference) {
html {
interpolate-size: allow-keywords;
}
}
body {
/* 4. Add accessible line-height */
line-height: 1.5;
/* 5. Improve text rendering */
-webkit-font-smoothing: antialiased;
}
/* 6. Improve media defaults */
img, picture, video, canvas, svg {
display: block;
max-width: 100%;
}
/* 7. Inherit fonts for form controls */
input, button, textarea, select {
font: inherit;
}
/* 8. Avoid text overflows */
p, h1, h2, h3, h4, h5, h6 {
overflow-wrap: break-word;
}
/* 9. Improve line wrapping */
p {
text-wrap: pretty;
}
h1, h2, h3, h4, h5, h6 {
text-wrap: balance;
}
/*
10. Create a root stacking context
*/
#root, #__next {
isolation: isolate;
}

21
theme/styles.scss Normal file
View File

@@ -0,0 +1,21 @@
@import url('https://fonts.googleapis.com/css2?family=Bebas+Neue&display=swap');
@import url('https://fonts.googleapis.com/css2?family=Anton&family=Bebas+Neue&display=swap');
@import "variables";
@import "reset";
@import "header";
@import "headings";
@import "tables";
body {
background: var(--background);
color: var(--primary-text);
padding: 20px;
font-size: 1.2rem;
}
img {
max-width: 100%;
display: block;
}

36
theme/tables.scss Normal file
View File

@@ -0,0 +1,36 @@
.styled-table {
border-collapse: collapse;
margin: 25px 0;
font-size: 0.9em;
font-family: sans-serif;
min-width: 400px;
box-shadow: 0 0 20px rgba(0, 0, 0, 0.15);
}
.styled-table thead tr {
background-color: #009879;
color: #ffffff;
text-align: left;
}
.styled-table th,
.styled-table td {
padding: 12px 15px;
}
.styled-table tbody tr {
border-bottom: 1px solid #dddddd;
}
.styled-table tbody tr:nth-of-type(even) {
background-color: #f3f3f3;
}
.styled-table tbody tr:last-of-type {
border-bottom: 2px solid #009879;
}
.styled-table tbody tr.active-row {
font-weight: bold;
color: #009879;
}

14
theme/variables.scss Normal file
View File

@@ -0,0 +1,14 @@
// Color Scheme
:root {
--blue: #002D62;
--red: #C8102E;
--green: #4CAF50;
--background: #F5F5F5;
--primary-text: #1A1A1A;
--secondary-text: #555555;
--success: #2E8B57;
--warning: #FFA500;
--error: #D72638;
--primary-font: "Bebas Neue", sans-serif;
--secondary-font: "Anton", sans-serif;
}

0
todo.md Normal file
View File