initial commit

This commit is contained in:
Zastian Pretorius
2024-05-08 13:55:26 +01:00
commit c31b86fa01
12 changed files with 2118 additions and 0 deletions

1
.gitignore vendored Normal file
View File

@@ -0,0 +1 @@
/target

1661
Cargo.lock generated Normal file

File diff suppressed because it is too large Load Diff

13
Cargo.toml Normal file
View File

@@ -0,0 +1,13 @@
[package]
name = "methoddata"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
actix-files = "0.6.2"
actix-web = "4.3.1"
isahc = "1.7.2"
serde_json = "1.0.83"
serde = { version = "1.0", features = ["derive"] }

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

@@ -0,0 +1,42 @@
<!DOCTYPE html>
<html>
<head>
<title>Mrfluffy-dev</title>
<script src="/pages/loadSrc.js"></script>
</head>
<body onload="loadSidebar()">
<div id="sidebar">
<!-- Sidebar content will be loaded dynamically here -->
</div>
<div id="toggle-btn" onclick="toggleSidebar()">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" width="24" height="24">
<path fill="currentColor" d="M3 13h18v-2H3v2zm0 5h18v-2H3v2zm0-10v2h18V8H3z"/>
</svg>
</div>
<div id="content">
<h1>Star Wars</h1>
<div class="parent">
<div id="div1">
<!-- make a text input that's value will be used for loadPeople -->
<input type="text" id="person1" placeholder="Enter a person">
<button onclick="loadPeople(document.getElementById('person1').value,1)">Load People</button>
<div id="people1">
<!-- People data will be loaded here -->
</div>
</div>
<div id="div2">
<!-- make a text input that's value will be used for loadPeople -->
<input type="text" id="person2" placeholder="Enter a person">
<button onclick="loadPeople(document.getElementById('person2').value,2)">Load People</button>
<div id="people2">
<!-- People data will be loaded here -->
</div>
</div>
</div>
</div>
</body>
</html>

17
public/pages/loadSrc.js Normal file
View File

@@ -0,0 +1,17 @@
function loadSrc() {
var link = document.createElement('link');
link.rel = 'stylesheet';
link.href = '/pages/style.css';
document.head.appendChild(link);
var script = document.createElement('script');
script.src = '/pages/sidebar.js';
document.head.appendChild(script);
var script = document.createElement('script');
script.src = '/pages/people.js';
document.head.appendChild(script);
}
// Call the function to load src.html
loadSrc();

26
public/pages/people.js Normal file
View File

@@ -0,0 +1,26 @@
async function loadPeople(name,id) {
try {
// fix name from spaces to dashes
name = name.replace(' ', '+');
const response = await fetch(`http://localhost:4200/api/${name}`);
const data = await response.json();
const peopleElement = document.getElementById('people'+id);
// Clear any existing content
peopleElement.innerHTML = '';
// Create a div element for the person
const personElement = document.createElement('div');
// Set the text content using the person's name, height, mass, hair color, skin color, eye color, birth year, and gender
personElement.innerHTML = `Name: ${data.name}<br>
Height: ${data.height}<br>
Mass: ${data.mass}<br>
Hair Color: ${data.hair_color}<br>
Skin Color: ${data.skin_color}<br>
Eye Color: ${data.eye_color}<br>
Birth Year: ${data.birth_year}<br>
Gender: ${data.gender}`;
// Append the person element to the people element
peopleElement.appendChild(personElement);
} catch (error) {
console.error('Error loading people:', error);
}
}

View File

@@ -0,0 +1,3 @@
<h2>Sidebar</h2>
<li class="sideContent"><a href="/">Home</a></li>
<li class="sideContent"><a href="/test">Test</a></li>

20
public/pages/sidebar.js Normal file
View File

@@ -0,0 +1,20 @@
function toggleSidebar() {
const sidebar = document.getElementById('sidebar');
const toggleBtn = document.getElementById('toggle-btn');
const content = document.getElementById('content');
sidebar.classList.toggle('collapsed');
toggleBtn.classList.toggle('collapsed');
content.classList.toggle('collapsed');
}
function loadSidebar() {
const xhr = new XMLHttpRequest();
xhr.open('GET', '/pages/sidebar.html', true);
xhr.onreadystatechange = function () {
if (xhr.readyState === 4 && xhr.status === 200) {
document.getElementById('sidebar').innerHTML = xhr.responseText;
}
};
xhr.send();
}

141
public/pages/style.css Normal file
View File

@@ -0,0 +1,141 @@
body {
margin: 0;
padding: 0;
font-family: Arial, sans-serif;
background-color: #282a36; /* Dracula theme background color */
color: #f8f8f2; /* Dracula theme text color */
}
/*give h1 Dracula green*/
h1 {
color: #50fa7b;
}
/*give h2 Dracula orange*/
h2 {
color: #ffb86c;
}
/*give h3 Dracula yello*/
h3 {
color: #f1fa8c;
}
/*give code Dracula red*/
code {
color: #ff5555;
}
/*give a Dracula pink*/
a {
color: #ff79c6;
}
/*give pre Dracula red*/
pre {
color: #ff5555;
background-color: #1e1f2a;
}
#sidebar {
position: fixed;
width: 200px;
height: 100vh;
background-color: #1e1f2a; /* Dracula theme background color */
color: #f8f8f2; /* Dracula theme text color */
transition: transform 0.3s ease;
}
.sideContent {
/* make the background color darker then the sidebar */
padding: 7px;
background-color: #282a36;
/* rounded corners */
border-radius: 0 15px 15px 0;
}
.sideContent:hover {
background-color: #21222c; /* New background color when hovering */
}
.sideContent a {
color: #ff79c6; /* Default link color */
}
.sideContent:hover a {
color: #8a2be2; /* New link color when hovering */
}
#sidebar.collapsed {
transform: translateX(-200px);
}
#content {
margin-left: 200px;
padding: 20px;
}
#content.collapsed {
/* animation: content-collapse 0.3s ease; */
margin-left: 30px;
}
#toggle-btn {
position: fixed;
top: 10px;
left: 175px;
cursor: pointer;
color: #f8f8f2; /* Dracula theme text color */
transition: transform 0.3s ease;
}
#toggle-btn.collapsed {
transform: translateX(-170px);
}
#toggle-btn:hover {
color: #6272a4; /* Dracula theme hover color */
}
.parent {
display: flex;
justify-content: space-between;
padding: 20px;
}
#div1, #div2 {
width: 45%; /* Adjust as needed */
overflow: auto;
border: 1px solid #ccc;
padding: 20px;
border-radius: 5px;
}
input[type="text"] {
width: 80%;
padding: 10px;
margin: 10px 0;
border-radius: 5px;
border: 1px solid #ccc;
}
#people1, #people2 {
font-family: 'Arial', sans-serif; /* Change as needed */
color: #ff79c6; /* Change as needed */
line-height: 1.6;
font-size: 32px; /* Change as needed */
}
button {
padding: 10px 20px;
border: none;
background-color: #4CAF50; /* Green */
color: white;
cursor: pointer;
border-radius: 5px;
}
button:hover {
background-color: #45a049; /* Darker green */
}

View File

@@ -0,0 +1,24 @@
<!DOCTYPE html>
<html>
<head>
<title>Mrfluffy-Dev</title>
<script src="/pages/loadSrc.js"></script>
</head>
<body onload="loadSidebar()">
<div id="sidebar">
<!-- Sidebar content will be loaded dynamically here -->
</div>
<div id="toggle-btn" onclick="toggleSidebar()">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" width="24" height="24">
<path fill="currentColor" d="M3 13h18v-2H3v2zm0 5h18v-2H3v2zm0-10v2h18V8H3z"/>
</svg>
</div>
<div id="content">
<h1>Welcome to my website!</h1>
<p>This is the main content area. You can place your text, images, and other elements here.</p>
</div>
</body>
</html>

25
shell.nix Normal file
View File

@@ -0,0 +1,25 @@
let
pkgs = import <nixpkgs> {};
in
pkgs.mkShell {
buildInputs = with pkgs; [
dbus
openssl
cargo
rustc
rust-analyzer
rustup
gcc
rustfmt
gf
];
nativeBuildInputs = with pkgs; [
pkg-config
];
dbus = pkgs.dbus;
shellHook = ''
export TEMPDIR=/tmp
echo "run shell shit here"
echo "your mom"
'';
}

145
src/main.rs Normal file
View File

@@ -0,0 +1,145 @@
use actix_web::{web, get, App, HttpServer, HttpResponse, Responder};
use isahc::config::Configurable;
use isahc::{ReadResponseExt, Request, RequestExt};
use serde::{Serialize};
#[derive(Debug, Serialize)]
struct People {
name: String,
height: String,
mass: String,
hair_color: String,
skin_color: String,
eye_color: String,
birth_year: String,
gender: String,
}
fn search_people(query: String) -> People {
use serde_json::Value;
let req = Request::builder()
.uri(format!(
"https://swapi.dev/api/people/?search={}",
query
))
.redirect_policy(isahc::config::RedirectPolicy::Follow)
.header("user-agent", "uwu")
.body(())
.unwrap();
let mut response = match req.send() {
Ok(response) => response,
Err(e) => {
panic!("Error sending HTTP request: {}", e);
}
};
let json_str = match response.text() {
Ok(text) => text,
Err(e) => {
panic!("Error retrieving response body: {}", e);
}
};
let json: Value = match serde_json::from_str(&json_str) {
Ok(value) => value,
Err(e) => {
panic!("Error parsing JSON: {}", e);
}
};
let mut person = People {
name: String::new(),
height: String::new(),
mass: String::new(),
hair_color: String::new(),
skin_color: String::new(),
eye_color: String::new(),
birth_year: String::new(),
gender: String::new(),
};
if let Some(results) = json["results"].as_array() {
for people in results.iter() {
person.name =
people["name"]
.as_str()
.unwrap()
.to_string();
person.height =
people["height"]
.as_str()
.unwrap()
.to_string();
person.mass =
people["mass"]
.as_str()
.unwrap()
.to_string();
person.hair_color =
people["hair_color"]
.as_str()
.unwrap()
.to_string();
person.skin_color =
people["skin_color"]
.as_str()
.unwrap()
.to_string();
person.eye_color =
people["eye_color"]
.as_str()
.unwrap()
.to_string();
person.birth_year =
people["birth_year"]
.as_str()
.unwrap()
.to_string();
person.gender =
people["gender"]
.as_str()
.unwrap()
.to_string();
}
} else {
let status = response.status();
if status.is_client_error() || status.is_server_error() {
panic!("HTTP request failed with status code {}", status);
} else {
panic!("JSON response is missing 'results' field");
}
}
person
}
#[get("/api/{name}")]
async fn index(name: web::Path<String>) -> impl Responder {
let name = name.into_inner();
let person = search_people(name);
//print all attributes of the person
println!("name: {} height: {} mass: {} hair_color: {} skin color: {} eye_color: {} birth_year: {} gender: {}",
person.name, person.height, person.mass, person.hair_color, person.skin_color, person.eye_color, person.birth_year, person.gender);
HttpResponse::Ok().json(person)
}
#[actix_web::main]
async fn main() -> std::io::Result<()> {
HttpServer::new(|| {
App::new()
.service(index)
.service(actix_files::Files::new("/", "public").index_file("pages/index.html"))
})
.bind(("0.0.0.0", 4200))?
.run()
.await
}