synchronous not working??

This commit is contained in:
CatAClock 2025-06-19 20:15:44 -07:00
parent dad35afd6d
commit e9f02f3ee0

View file

@ -1,16 +1,15 @@
#![allow(non_snake_case)] #![allow(non_snake_case)]
#![allow(unused_braces)] #![allow(unused_braces)]
use rouille::Response; use rouille::{post_input, try_or_400, router, Response};
use rouille::router;
use std::fs::File; use std::fs::File;
use postgres::{Client, NoTls}; use postgres::{Client, NoTls};
fn main() -> Result<(), Box<dyn std::error::Error>>{ fn main() -> Result<(), Box<dyn std::error::Error>>{
// TODO: have the user fill in user, password, database name manually. // TODO: have the user fill in user, password, database name manually.
let mut client = Client::connect("host=/var/run/postgresql,localhost user=postgres password=Password dbname=ActivityPub", NoTls)?; let mut Client = Client::connect("host=/var/run/postgresql,localhost user=postgres password=Password dbname=ActivityPub", NoTls)?;
let Table = client.query("SELECT 1 let Table = Client.query("SELECT 1
FROM INFORMATION_SCHEMA.TABLES FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_TYPE='BASE TABLE' WHERE TABLE_TYPE='BASE TABLE'
AND TABLE_NAME='person'", &[]); AND TABLE_NAME='person'", &[]);
@ -19,7 +18,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>>{
Ok(_) => { Ok(_) => {
// Check if the table exists. // Check if the table exists.
if Table?.len() == 0 { if Table?.len() == 0 {
client.batch_execute(" Client.batch_execute("
CREATE TABLE person ( CREATE TABLE person (
id SERIAL PRIMARY KEY, id SERIAL PRIMARY KEY,
name TEXT NOT NULL, name TEXT NOT NULL,
@ -30,14 +29,26 @@ fn main() -> Result<(), Box<dyn std::error::Error>>{
Err(_) => () Err(_) => ()
} }
let name = "Ferris"; let mut name = "Ferris";
let data = None::<&[u8]>; let mut data: Option<&[u8]> = None;
client.execute( Client.execute(
"INSERT INTO person (name, data) VALUES ($1, $2)",
&[&name, &data],
)?;
name = "Dope";
data = Some(&[8u8, 9u8]);
Client.execute(
"INSERT INTO person (name, data) VALUES ($1, $2)",
&[&name, &data],
)?;
name = "pingas";
data = Some(&[27u8]);
Client.execute(
"INSERT INTO person (name, data) VALUES ($1, $2)", "INSERT INTO person (name, data) VALUES ($1, $2)",
&[&name, &data], &[&name, &data],
)?; )?;
for row in client.query("SELECT id, name, data FROM person", &[])? { for row in Client.query("SELECT id, name, data FROM person", &[])? {
let id: i32 = row.get(0); let id: i32 = row.get(0);
let name: &str = row.get(1); let name: &str = row.get(1);
let data: Option<&[u8]> = row.get(2); let data: Option<&[u8]> = row.get(2);
@ -45,22 +56,27 @@ fn main() -> Result<(), Box<dyn std::error::Error>>{
println!("found person: {} {} {:?}", id, name, data); println!("found person: {} {} {:?}", id, name, data);
} }
RouilleHandoff(); // Never leave the server. CTRL + C if you have issues.
// Finish
Ok(())
}
fn RouilleHandoff() {
rouille::start_server("127.0.0.1:8080", move |Request| { rouille::start_server("127.0.0.1:8080", move |Request| {
// Router. Go to the correct pages, else hit the sack. // Router. Go to the correct pages, else hit the sack.
router!(Request, router!(Request,
// Actual Web Pages.
(GET) ["/"] => { (GET) ["/"] => {
Response::from_file("text/html", File::open("src/index.html").unwrap()).with_status_code(200) Response::from_file("text/html", File::open("src/index.html").unwrap()).with_status_code(200)
}, },
(GET) ["/Profile"] => { (GET) ["/Profile"] => {
Response::from_file("text/html", File::open("src/profile.html").unwrap()).with_status_code(200) Response::from_file("text/html", File::open("src/profile.html").unwrap()).with_status_code(200)
}, },
// API stuff.
(POST) ["/API/Profile"] => {
// Content-type: multipart/form-data
let Profile = try_or_400!(post_input!(Request, {
ID: u32,
}));
let Que: u32 = Client.query("SELECT name FROM persons WHERE id = $1", &[&Profile.ID]).unwrap()[0].get(0);
let Text: String = "hello world! Your output is: ".to_string() + &Que.to_string();
Response::text(Text)
},
// Get specific images. Because the browser said so. // Get specific images. Because the browser said so.
(GET) ["/favicon.ico"] => { (GET) ["/favicon.ico"] => {
Response::from_file("image/vnd.microsoft.icon", File::open("src/favicon.ico").unwrap()).with_status_code(200) Response::from_file("image/vnd.microsoft.icon", File::open("src/favicon.ico").unwrap()).with_status_code(200)