diff --git a/src/main.rs b/src/main.rs index 497f027..d99e276 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,16 +1,15 @@ #![allow(non_snake_case)] #![allow(unused_braces)] -use rouille::Response; -use rouille::router; +use rouille::{post_input, try_or_400, router, Response}; use std::fs::File; use postgres::{Client, NoTls}; fn main() -> Result<(), Box>{ // 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 WHERE TABLE_TYPE='BASE TABLE' AND TABLE_NAME='person'", &[]); @@ -19,7 +18,7 @@ fn main() -> Result<(), Box>{ Ok(_) => { // Check if the table exists. if Table?.len() == 0 { - client.batch_execute(" + Client.batch_execute(" CREATE TABLE person ( id SERIAL PRIMARY KEY, name TEXT NOT NULL, @@ -30,14 +29,26 @@ fn main() -> Result<(), Box>{ Err(_) => () } - let name = "Ferris"; - let data = None::<&[u8]>; - client.execute( + let mut name = "Ferris"; + let mut data: Option<&[u8]> = None; + 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)", &[&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 name: &str = row.get(1); let data: Option<&[u8]> = row.get(2); @@ -45,22 +56,27 @@ fn main() -> Result<(), Box>{ println!("found person: {} {} {:?}", id, name, data); } - RouilleHandoff(); - - // Finish - Ok(()) -} - -fn RouilleHandoff() { + // 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. router!(Request, + // Actual Web Pages. (GET) ["/"] => { Response::from_file("text/html", File::open("src/index.html").unwrap()).with_status_code(200) }, (GET) ["/Profile"] => { 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) ["/favicon.ico"] => { Response::from_file("image/vnd.microsoft.icon", File::open("src/favicon.ico").unwrap()).with_status_code(200)