added api routes for creation of new users
This commit is contained in:
parent
b7e85e8c69
commit
c0247e9e98
28
Rocket.toml
Normal file
28
Rocket.toml
Normal file
@ -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
|
43
src/main.rs
43
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 = "<test>")]
|
||||
fn post_test(test: Json<PostTest<'_>>) {
|
||||
fn post_test(
|
||||
test: Json<PostTest<'_>>
|
||||
) {
|
||||
sql::post_test(
|
||||
test.test_type,
|
||||
test.test_length,
|
||||
@ -43,10 +57,27 @@ fn post_test(test: Json<PostTest<'_>>) {
|
||||
).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 = "<user>")]
|
||||
fn create_user(
|
||||
user: Json<User<'_>>
|
||||
) {
|
||||
sql::create_user(
|
||||
user.username,
|
||||
user.password
|
||||
).expect("Error: Couldn't create new user");
|
||||
}
|
||||
|
||||
#[launch]
|
||||
fn rocket() -> Rocket<Build> {
|
||||
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
|
||||
}
|
66
src/sql.rs
66
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,8 +36,16 @@ 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(
|
||||
@ -51,10 +59,54 @@ pub fn post_test(test_type: &str, test_length: u32, test_time: u32, test_seed: i
|
||||
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(())
|
||||
}
|
@ -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)
|
||||
);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user