diff --git a/.gitignore b/.gitignore index 869df07..0e32a45 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ /target -Cargo.lock \ No newline at end of file +Cargo.lock +/database/database.sqlite \ No newline at end of file diff --git a/Cargo.toml b/Cargo.toml index 0fcacf0..f311e6a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -6,4 +6,5 @@ edition = "2021" [dependencies] [dependencies.rocket] -version = "0.5.0-rc.2" \ No newline at end of file +version = "0.5.0-rc.2" +features = ["json"] \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index 730f991..a822350 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,14 +1,46 @@ +// relevant macros and imports for rocket.rs #[macro_use] extern crate rocket; -use rocket::{Rocket, Build, fs::{FileServer, relative}}; +use rocket::{Rocket, Build, fs::{FileServer, relative}, serde::{Deserialize, json::Json}}; #[get("/")] -fn index() -> String { +fn test() -> String { format!("Hello world") } + +#[derive(Deserialize)] +#[serde(crate = "rocket::serde")] +struct PostTest<'r> { + test_type: &'r str, + test_length: i64, + test_time: i32, + test_seed: i64, + quote_id: i32, + wpm: i16, + accuracy: i8, + user_id: i32 +} + +#[post("/post_test", data = "")] +fn post_test(test: Json>) { + println!( + "{}\n{}\n{}\n{}\n{}\n{}\n{}\n{}", + test.test_type, + test.test_length, + test.test_time, + test.test_seed, + test.quote_id, + test.wpm, + test.accuracy, + test.user_id + ) + +} + #[launch] fn rocket() -> Rocket { rocket::build() - // .mount("/", routes![index]) - .mount("/", FileServer::from(relative!("website"))) + .mount("/test", routes![test]) // testing only, should return "Hello world" + .mount("/api", routes![post_test]) + .mount("/", FileServer::from(relative!("website"))) // hosts the fileserver } \ No newline at end of file diff --git a/website/index.html b/website/index.html index 36780bd..456a1ea 100644 --- a/website/index.html +++ b/website/index.html @@ -5,8 +5,13 @@ Document + + + + + - Hello Fileserver + \ No newline at end of file diff --git a/website/index.js b/website/index.js new file mode 100644 index 0000000..fd91224 --- /dev/null +++ b/website/index.js @@ -0,0 +1,21 @@ +const url = "http://localhost:8000/api/post_test/" +const data = { + test_type: "words", + test_length: 500, + test_time: 100, + test_seed: 123040004, + quote_id: 0, + wpm: 60, + accuracy: 100, +} +const xhr = new XMLHttpRequest(); +const button = document.getElementById("button"); + +button.addEventListener("click", (e) => { + send(); +}); + +function send() { + xhr.open("POST", url); + xhr.send(JSON.stringify(data)); +} \ No newline at end of file