From c0247e9e98724cdd87540b7473121f946a9aece0 Mon Sep 17 00:00:00 2001 From: Arlo Filley Date: Sun, 6 Nov 2022 23:07:13 +0000 Subject: [PATCH] added api routes for creation of new users --- Rocket.toml | 28 ++++++++++++++++ src/main.rs | 43 ++++++++++++++++++++---- src/sql.rs | 82 +++++++++++++++++++++++++++++++++++++--------- website/api/api.js | 49 +++++++++++++++++++++++++-- 4 files changed, 178 insertions(+), 24 deletions(-) create mode 100644 Rocket.toml diff --git a/Rocket.toml b/Rocket.toml new file mode 100644 index 0000000..c255985 --- /dev/null +++ b/Rocket.toml @@ -0,0 +1,28 @@ +[default] +address = "192.168.0.131" +port = 8000 +workers = 4 +max_blocking = 512 +keep_alive = 5 +ident = "Rocket" +log_level = "normal" +temp_dir = "/tmp" +cli_colors = true +## NOTE: Don't (!) use this key! Generate your own! +secret_key = "abcdefghijklmnop" + +[default.limits] +form = "64 kB" +json = "1 MiB" +msgpack = "2 MiB" +"file/jpg" = "5 MiB" + +# [default.tls] +# certs = "path/to/cert-chain.pem" +# key = "path/to/key.pem" + +[default.shutdown] +ctrlc = true +signals = ["term", "hup"] +grace = 5 +mercy = 5 \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index b3fc655..781eb7e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,18 +1,30 @@ +pub mod sql; + // relevant macros and imports for rocket.rs #[macro_use] extern crate rocket; use rocket::{ Rocket, Build, - fs::{FileServer, relative}, - serde::{Deserialize, json::Json} + fs::{ + FileServer, + relative + }, + serde::{ + Deserialize, + json::Json + } }; -pub mod sql; - #[get("/")] fn test() -> String { sql::create_database() .expect("couldn't create database"); + + sql::create_user("arlo", "passwod123") + .expect("couldn't create user"); + + sql::create_user("arlo","password123") + .expect("couldn't create user"); String::from("Hello World!") } @@ -30,7 +42,9 @@ struct PostTest<'r> { } #[post("/post_test", data = "")] -fn post_test(test: Json>) { +fn post_test( + test: Json> +) { sql::post_test( test.test_type, test.test_length, @@ -43,10 +57,27 @@ fn post_test(test: Json>) { ).expect("error in posting test to tests table"); } +#[derive(Deserialize)] +#[serde(crate = "rocket::serde")] +struct User<'r> { + username: &'r str, + password: &'r str +} + +#[post("/create_user", data = "")] +fn create_user( + user: Json> +) { + sql::create_user( + user.username, + user.password + ).expect("Error: Couldn't create new user"); +} + #[launch] fn rocket() -> Rocket { rocket::build() .mount("/test", routes![test]) // testing only, should return "Hello world" - .mount("/api", routes![post_test]) + .mount("/api", routes![post_test, create_user]) .mount("/typing", FileServer::from(relative!("website"))) // hosts the fileserver } \ No newline at end of file diff --git a/src/sql.rs b/src/sql.rs index 733e526..df97d7f 100644 --- a/src/sql.rs +++ b/src/sql.rs @@ -11,7 +11,7 @@ pub fn create_database() -> Result<()> { connection.execute( "CREATE TABLE IF NOT EXISTS users ( user_id INTEGER PRIMARY KEY, - username UNIQUE TEXT NOT NULL, + username TEXT UNIQUE NOT NULL, password TEXT NOT NULL )", () @@ -36,25 +36,77 @@ pub fn create_database() -> Result<()> { Ok(()) } -pub fn post_test(test_type: &str, test_length: u32, test_time: u32, test_seed: i64, quote_id: i32, wpm: u8, accuracy: u8, user_id: u32) --> Result<()> { +pub fn post_test( + test_type: &str, + test_length: u32, + test_time: u32, + test_seed: i64, + quote_id: i32, + wpm: u8, + accuracy: u8, + user_id: u32 +) -> Result<()> { let connection = get_connection(); connection.execute( "INSERT INTO tests ( - test_type, - test_length, - test_time, - test_seed, - quote_id, - wpm, - accuracy, - user_id + test_type, + test_length, + test_time, + test_seed, + quote_id, + wpm, + accuracy, + user_id ) - VALUES - (?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8) - ", (test_type, test_length, test_time, test_seed, quote_id, - wpm, accuracy,user_id))?; + VALUES( + ?1, + ?2, + ?3, + ?4, + ?5, + ?6, + ?7, + ?8 + ) + ", + ( + test_type, + test_length, + test_time, + test_seed, + quote_id, + wpm, + accuracy, + user_id + ) + )?; + + Ok(()) +} + +pub fn create_user( + username: &str, + password: &str +) -> Result<()> { + let connection = get_connection(); + + connection.execute( + " + INSERT INTO users ( + username, + password + ) + VALUES ( + ?1, + ?2 + ) + ", + ( + username, + password + ) + )?; Ok(()) } \ No newline at end of file diff --git a/website/api/api.js b/website/api/api.js index 44835b2..11013b2 100644 --- a/website/api/api.js +++ b/website/api/api.js @@ -18,7 +18,16 @@ class API { * @param {int} accuracy * @param {int} userId */ - postTest(pTestType, pTestLength, pTestTime, pTestSeed, pQuoteId, pWpm, pAccuracy, pUserId) { + postTest( + pTestType, + pTestLength, + pTestTime, + pTestSeed, + pQuoteId, + pWpm, + pAccuracy, + pUserId + ) { const data = { 'test_type': pTestType, 'test_length': pTestLength, @@ -31,8 +40,14 @@ class API { } const xhr = new XMLHttpRequest(); - xhr.open("POST", this.url+"post_test"); - xhr.send(JSON.stringify(data)); + xhr.open( + "POST", + this.url+"post_test" + ); + + xhr.send( + JSON.stringify(data) + ); } /** @@ -132,4 +147,32 @@ class API { this.postTest(testType, testLength, testTime, testSeed, quoteId, wpm, accuracy, userId); } + + /** + * takes a validated name and password and sends + * a post request to make a user with the given + * username and password + * @param {String} username + * @param {String} password + * @returns + */ + createUser( + username, + password + ) { + const user = { + username: username, + password: password + }; + + const xhr = new XMLHttpRequest(); + xhr.open( + "POST", + `${this.url}create_user/` + ) + + xhr.send( + JSON.stringify(user) + ); + } } \ No newline at end of file