Compare commits

...
Sign in to create a new pull request.

20 commits
master ... 0.2

Author SHA1 Message Date
5ac9bce74b Signing up is now limited. And fixed an odd bug? 2025-06-28 16:33:31 -07:00
949cf74a31 Delete src/API/mod.rs 2025-06-24 05:38:13 +00:00
26ae559037 Merge stupid mistake into 0.2 2025-06-23 19:28:24 -07:00
c60fb18003 You can now sign up and sign in! 2025-06-23 12:33:19 -07:00
a0652f93c6 you can now login 2025-06-22 23:10:27 -07:00
c929cdceb1 Basic login system. Kind of. 2025-06-22 22:16:39 -07:00
1f6176de2e Base types implemented. Let's get the client working! 2025-06-21 21:11:35 -07:00
8906579a63 Core types listed 2025-06-20 20:05:04 -07:00
c66a8ae60e SQL database is now accessable. CSS and JS implemented 2025-06-20 19:26:04 -07:00
be4063d9d5 working with things 2025-06-20 17:31:24 -07:00
8e9ed6bac8 Organization and fixing up the drillevator 2025-06-20 17:11:01 -07:00
099850f750 SQL finally works 2025-06-20 16:18:14 -07:00
e9f02f3ee0 synchronous not working?? 2025-06-19 20:15:44 -07:00
dad35afd6d database now creatable. 2025-06-19 18:28:21 -07:00
9ec7e0001f we up posting our gres 2025-06-19 08:02:53 -07:00
b65fee9d5d sure 2025-06-18 22:13:14 -07:00
610a8853bb last minute thing 2025-06-17 23:05:11 -07:00
0a19ee5c33 favicon 2025-06-17 22:54:35 -07:00
0ec60694c7 placed a backend. I think this works? 2025-06-17 22:25:45 -07:00
11e8cc5ad5 Basic server 2025-06-17 19:43:56 -07:00
8 changed files with 150 additions and 121 deletions

View file

@ -1,90 +0,0 @@
use postgres::{Client, NoTls};
pub fn Login(Username: &str, Password: &str) -> Result<Vec<String>, Box<dyn std::error::Error>>{
let mut Client = Client::connect("host=/var/run/postgresql,localhost user=postgres password=Password dbname=ActivityPub", NoTls)?;
let Table = Client.query("SELECT 1
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_TYPE='BASE TABLE'
AND TABLE_NAME='person'", &[]);
// Check if the table doesn't exists. Or does?
match Table {
Ok(_) => {
// Check if the table exists.
if Table?.len() == 0 {
Client.batch_execute("
CREATE TABLE person (
id SERIAL PRIMARY KEY,
username TEXT NOT NULL,
password TEXT NOT NULL
)")?;
}
},
Err(_) => ()
}
if Client.query("SELECT username, password FROM person WHERE username = $1", &[&Username])?.len() != 0 {
let mut Response: Vec<String> = Vec::new();
if Client.query("SELECT username, password FROM person WHERE password = $1", &[&Password])?.len() != 0 {
let Result = Client.query("SELECT username, password FROM person WHERE password = $1", &[&Password])?[0].clone();
Response.push(Result.get(0));
Response.push(Result.get(1));
return Ok(Response);
} else {
return Ok(vec!["Password Not Correct.".to_string()]);
}
} else {
return Ok(vec!["Username Not Found.".to_string()]);
}
}
pub fn GetAccountWithID(ID: i32) -> Result<String, Box<dyn std::error::Error>>{
let mut Client = Client::connect("host=/var/run/postgresql,localhost user=postgres password=Password dbname=ActivityPub", NoTls)?;
let Table = Client.query("SELECT 1
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_TYPE='BASE TABLE'
AND TABLE_NAME='person'", &[]);
// Check if the table doesn't exists. Or does?
match Table {
Ok(_) => {
// Check if the table exists.
if Table?.len() == 0 {
Client.batch_execute("
CREATE TABLE person (
id SERIAL PRIMARY KEY,
username TEXT NOT NULL,
password TEXT NOT NULL
)")?;
}
},
Err(_) => ()
}
let Result: String = Client.query("SELECT username FROM person WHERE id = $1", &[&ID]).unwrap()[0].get(0);
return Ok(Result.to_string());
}
pub fn MakeAccount(User: String, Pass: String) -> Result<String, Box<dyn std::error::Error>>{
let mut Client = Client::connect("host=/var/run/postgresql,localhost user=postgres password=Password dbname=ActivityPub", NoTls)?;
let Table = Client.query("SELECT 1
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_TYPE='BASE TABLE'
AND TABLE_NAME='person'", &[]);
// Check if the table doesn't exists. Or does?
match Table {
Ok(_) => {
// Check if the table exists.
if Table?.len() == 0 {
Client.batch_execute("
CREATE TABLE person (
id SERIAL PRIMARY KEY,
username TEXT NOT NULL,
password TEXT NOT NULL
)")?;
}
},
Err(_) => ()
}
Client.execute("INSERT INTO person (username, password) VALUES ($1, $2)", &[&User, &Pass])?;
return Ok("Account Created!".to_string());
}

54
src/Extra/mod.rs Normal file
View file

@ -0,0 +1,54 @@
use postgres::{Client, NoTls};
fn CheckTableIsHere(Table: &str) -> Result<(), Box<dyn std::error::Error>> {
let mut Client = Client::connect("host=/var/run/postgresql,localhost user=postgres password=Password dbname=ActivityPub", NoTls)?;
let LeTable = Client.query("SELECT 1
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_TYPE='BASE TABLE'
AND TABLE_NAME=$1", &[&Table]);
// Check if the table exists.
if LeTable.unwrap().len() == 0 {
let TempString = "CREATE TABLE ".to_string() + &Table.to_string() + " (id SERIAL PRIMARY KEY, username TEXT NOT NULL, password TEXT NOT NULL)";
Client.batch_execute(&TempString)?;
}
Ok(())
}
pub fn Signin(Username: &str, Password: &str) -> Result<Vec<String>, Box<dyn std::error::Error>>{
let _ = CheckTableIsHere("person");
let mut Client = Client::connect("host=/var/run/postgresql,localhost user=postgres password=Password dbname=ActivityPub", NoTls)?;
// Check to see if the username or password is incorrect.
if Client.query("SELECT username, password FROM person WHERE username = $1", &[&Username])?.len() != 0 {
let mut Response: Vec<String> = Vec::new();
if Client.query("SELECT username, password FROM person WHERE password = $1", &[&Password])?.len() != 0 {
let Result = Client.query("SELECT username, password FROM person WHERE password = $1", &[&Password])?[0].clone();
Response.push(Result.get(0));
Response.push(Result.get(1));
return Ok(Response);
} else {
return Ok(vec!["Password Not Correct.".to_string()]);
}
} else {
return Ok(vec!["Username Not Found.".to_string()]);
}
}
pub fn Signup(Username: &str, Password: &str) -> Result<Vec<String>, Box<dyn std::error::Error>>{
let _ = CheckTableIsHere("person");
let mut Client = Client::connect("host=/var/run/postgresql,localhost user=postgres password=Password dbname=ActivityPub", NoTls)?;
if Client.query("SELECT username, password FROM person WHERE username = $1", &[&Username])?.len() == 0 {
let mut Response: Vec<String> = Vec::new();
Client.execute("INSERT INTO person (username, password) VALUES ($1, $2)", &[&Username, &Password])?;
Response.push((&Username).to_string());
Response.push((&Password).to_string());
return Ok(Response);
} else {
return Ok(vec!["Username Already Taken.".to_string()]);
}
}

View file

@ -10,8 +10,6 @@
<h1>Hello!</h1>
<p>Hi from Rust.</p>
<a href="/Profile">View your profile.</a>
<script src="script.js"></script>
</body>
</html>

View file

@ -8,9 +8,9 @@
</head>
<body>
<h1>Profile!</h1>
<p id="Account">Log in. Or don't. Up to you.</p>
<form action="" method="get">
<p id="Account">Sign in. Or don't. Up to you.</p>
<a href="/Profile/Signup">Sign Up!</a>
<form action="" method="GET" id="Login">
<div>
<label>Username</label>
<input type="text" name="Username" required />
@ -20,13 +20,13 @@
<input type="text" name="Password" required />
</div>
<div>
<input type="submit" value="Log on" />
<input type="submit" value="Sign In" />
</div>
</form>
<p id="Information"></p>
<script src="profile.js"></script>
<script src="/profile.js"></script>
</body>
</html>

View file

@ -2,7 +2,7 @@ let Infomation = document.getElementById("Information");
let Account = document.getElementById("Account");
let req = new XMLHttpRequest();
req.open('GET', document.location, true);
req.open('GET', document.location.href + "&Verify=true", true);
req.send(null);
req.onload = function() {
let headers = req.getAllResponseHeaders();

32
src/HTTP/signup.html Normal file
View file

@ -0,0 +1,32 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<link rel="icon" href="/favicon.ico" type="image/webp" />
<link rel="stylesheet" href="/style.css" />
<title>Profile</title>
</head>
<body>
<h1>Profile!</h1>
<p id="Account">Sign Up. Or don't. Up to you.</p>
<a href="/Profile">Sign In!</a>
<form action="" method="GET" id="Signup">
<div>
<label>Username</label>
<input type="text" name="Username" required />
</div>
<div>
<label>Password</label>
<input type="text" name="Password" required />
</div>
<div>
<input type="submit" value="Sign Up" />
</div>
</form>
<p id="Information"></p>
<script src="/signup.js"></script>
</body>
</html>

26
src/HTTP/signup.js Normal file
View file

@ -0,0 +1,26 @@
let Infomation = document.getElementById("Information");
let Account = document.getElementById("Account");
let req = new XMLHttpRequest();
req.open('GET', document.location.href + "&Verify=true", true);
req.send(null);
req.onload = function() {
let headers = req.getAllResponseHeaders();
const arr = headers.trim().split(/[\r\n]+/);
// Create a map of header names to values
const headerMap = new Map();
arr.forEach((line) => {
const parts = line.split(": ");
headerMap.set(parts[0], parts[1]);
});
if (headerMap.get("profile") != null) {
if (headerMap.get("profile") == "Username Already Taken.") {
Infomation.innerHTML = "Username already taken! Please use a different username.";
} else {
Account.innerHTML = headerMap.get("profile");
Infomation.innerHTML = "Account Created! Please head back to the sign in to... sign in.";
}
}
};

View file

@ -1,12 +1,11 @@
#![allow(non_snake_case)]
#![allow(unused_braces)]
use rouille::{post_input, try_or_400, router, Response};
use rouille::{router, Response};
use std::fs::File;
mod ActivityPub;
mod API;
mod Extra;
fn main(){
// Never leave the server. CTRL + C if you have issues.
rouille::start_server("127.0.0.1:8080", move |Request| {
// Router. Go to the correct pages, else hit the sack.
@ -16,35 +15,42 @@ fn main(){
Response::from_file("text/html", File::open("src/HTTP/index.html").unwrap()).with_status_code(200)
},
(GET) ["/Profile"] => {
// Signing in.
let Username = Request.get_param("Username");
let Password = Request.get_param("Password").unwrap();
let Password = Request.get_param("Password");
let Verify = Request.get_param("Verify");
match Verify {
Some(x) => (),
None => return Response::from_file("text/html", File::open("src/HTTP/profile.html").unwrap()).with_status_code(200),
}
match Username {
Some(x) => {
let Account: Vec<String> = API::Login(&x, &Password).unwrap();
let Account: Vec<String> = Extra::Signin(&x, &Password.unwrap()).unwrap();
Response::from_file("text/html", File::open("src/HTTP/profile.html").unwrap()).with_status_code(200).with_additional_header("profile", Account[0].clone())
},
None => {
Response::from_file("text/html", File::open("src/HTTP/profile.html").unwrap()).with_status_code(200)
}
},
}
},
// API stuff.
(GET) ["/API/Profile:Get"] => {
// Content-type: application/x-www-form-urlencoded
let Profile = Request.get_param("id").unwrap().parse::<i32>().unwrap();
let Things = API::GetAccountWithID(Profile).unwrap();
let Text: String = "Got Account: ".to_string() + &Things.to_string();
Response::text(Text).with_status_code(200)
},
(POST) ["/API/Profile:Create"] => {
// Content-type: application/x-www-form-urlencoded
let Profile = try_or_400!(post_input!(Request, {
Username: String,
Password: String,
}));
let Things = API::MakeAccount(Profile.Username.to_string(), Profile.Password.to_string()).unwrap();
let Text: String = Things.to_string();
Response::text(Text).with_status_code(201)
(GET) ["/Profile/Signup"] => {
// Signing up.
let Username = Request.get_param("Username");
let Password = Request.get_param("Password");
let Verify = Request.get_param("Verify");
match Verify {
Some(x) => (),
None => return Response::from_file("text/html", File::open("src/HTTP/signup.html").unwrap()).with_status_code(200),
}
match Username {
Some(x) => {
let Account: Vec<String> = Extra::Signup(&x, &Password.unwrap()).unwrap();
Response::from_file("text/html", File::open("src/HTTP/signup.html").unwrap()).with_status_code(200).with_additional_header("profile", Account[0].clone())
},
None => {
Response::from_file("text/html", File::open("src/HTTP/signup.html").unwrap()).with_status_code(200)
},
}
},
// Get specific images. Because the browser said so.
(GET) ["/favicon.ico"] => {
@ -56,6 +62,9 @@ fn main(){
(GET) ["/profile.js"] => {
Response::from_file("text/javascript", File::open("src/HTTP/profile.js").unwrap()).with_status_code(200)
},
(GET) ["/signup.js"] => {
Response::from_file("text/javascript", File::open("src/HTTP/signup.js").unwrap()).with_status_code(200)
},
// Catch-all. Fuck you.
_ => {
Response::from_file("text/html", File::open("src/HTTP/404.html").unwrap()).with_status_code(404)