added database and json post
This commit is contained in:
parent
6797e6a952
commit
94904481a6
3
.gitignore
vendored
3
.gitignore
vendored
@ -1,2 +1,3 @@
|
|||||||
/target
|
/target
|
||||||
Cargo.lock
|
Cargo.lock
|
||||||
|
/database/database.sqlite
|
@ -6,4 +6,5 @@ edition = "2021"
|
|||||||
[dependencies]
|
[dependencies]
|
||||||
|
|
||||||
[dependencies.rocket]
|
[dependencies.rocket]
|
||||||
version = "0.5.0-rc.2"
|
version = "0.5.0-rc.2"
|
||||||
|
features = ["json"]
|
40
src/main.rs
40
src/main.rs
@ -1,14 +1,46 @@
|
|||||||
|
// relevant macros and imports for rocket.rs
|
||||||
#[macro_use] extern crate rocket;
|
#[macro_use] extern crate rocket;
|
||||||
use rocket::{Rocket, Build, fs::{FileServer, relative}};
|
use rocket::{Rocket, Build, fs::{FileServer, relative}, serde::{Deserialize, json::Json}};
|
||||||
|
|
||||||
#[get("/")]
|
#[get("/")]
|
||||||
fn index() -> String {
|
fn test() -> String {
|
||||||
format!("Hello world")
|
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 = "<test>")]
|
||||||
|
fn post_test(test: Json<PostTest<'_>>) {
|
||||||
|
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]
|
#[launch]
|
||||||
fn rocket() -> Rocket<Build> {
|
fn rocket() -> Rocket<Build> {
|
||||||
rocket::build()
|
rocket::build()
|
||||||
// .mount("/", routes![index])
|
.mount("/test", routes![test]) // testing only, should return "Hello world"
|
||||||
.mount("/", FileServer::from(relative!("website")))
|
.mount("/api", routes![post_test])
|
||||||
|
.mount("/", FileServer::from(relative!("website"))) // hosts the fileserver
|
||||||
}
|
}
|
@ -5,8 +5,13 @@
|
|||||||
<meta http-equiv="X-UA-Compatible" content="IE=edge">
|
<meta http-equiv="X-UA-Compatible" content="IE=edge">
|
||||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||||
<title>Document</title>
|
<title>Document</title>
|
||||||
|
|
||||||
|
<!-- Main Script Files -->
|
||||||
|
<script src="./index.js" type="text/javascript" defer="true"></script>
|
||||||
|
|
||||||
|
<!-- API Script Files -->
|
||||||
</head>
|
</head>
|
||||||
<body>
|
<body>
|
||||||
Hello Fileserver
|
<button id="button">Submit JSON</button>
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
21
website/index.js
Normal file
21
website/index.js
Normal file
@ -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));
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user