added documentation
This commit is contained in:
parent
1cb9d8f5a5
commit
63a8e86313
36
documentation/leaderboard.md
Normal file
36
documentation/leaderboard.md
Normal file
@ -0,0 +1,36 @@
|
|||||||
|
This API endpoint retrieves the highest test data from each user and returns it as a JSON array.
|
||||||
|
|
||||||
|
# Endpoint
|
||||||
|
|
||||||
|
```
|
||||||
|
GET /api/leaderboard
|
||||||
|
```
|
||||||
|
|
||||||
|
# Request Parameters
|
||||||
|
|
||||||
|
This endpoint does not require any request parameters.
|
||||||
|
|
||||||
|
# Example Request
|
||||||
|
|
||||||
|
```bash
|
||||||
|
curl -X GET "https://example.com/api/leaderboard"
|
||||||
|
```
|
||||||
|
|
||||||
|
# Response
|
||||||
|
|
||||||
|
```json
|
||||||
|
[
|
||||||
|
{
|
||||||
|
"userName": "user_1",
|
||||||
|
"wpm": 85,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"userName": "user_2",
|
||||||
|
"score": 80,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"userName": "user_3",
|
||||||
|
"wpm": 73,
|
||||||
|
}
|
||||||
|
]
|
||||||
|
```
|
@ -0,0 +1,54 @@
|
|||||||
|
Post Test Data This API endpoint allows you to post test data, recording the results of a test taken by a user.
|
||||||
|
|
||||||
|
# Endpoint
|
||||||
|
|
||||||
|
```
|
||||||
|
POST /api/post_test
|
||||||
|
```
|
||||||
|
|
||||||
|
# Request Parameters
|
||||||
|
| Parameter | Type | Description |
|
||||||
|
| ---------- | :-----: | ---------------------------------------------------- |
|
||||||
|
| testType | String | Type of the test (e.g., "typing", "multiple choice") |
|
||||||
|
| testLength | Integer | Length of the test in number of items or questions |
|
||||||
|
| testTime | Integer | Duration of the test in seconds |
|
||||||
|
| testSeed | String | Seed for generating randomized test content |
|
||||||
|
| quoteId | String | Identifier for a specific quote, if applicable |
|
||||||
|
| wpm | Integer | Words per minute (typing speed) |
|
||||||
|
| accuracy | Integer | Accuracy of responses (e.g., percentage) |
|
||||||
|
| userId | String | Identifier of the user taking the test |
|
||||||
|
| secret | String | Secret key for authentication and authorization |
|
||||||
|
|
||||||
|
# Example Request
|
||||||
|
|
||||||
|
```bash
|
||||||
|
curl -X POST "https://example.com/api/post_test" \
|
||||||
|
-H "Content-Type: application/json" \
|
||||||
|
-d '{
|
||||||
|
"testType": "typing",
|
||||||
|
"testLength": 100,
|
||||||
|
"testTime": 600,
|
||||||
|
"testSeed": "random_seed_123",
|
||||||
|
"quoteId": "quote_456",
|
||||||
|
"wpm": 65.5,
|
||||||
|
"accuracy": 98.2,
|
||||||
|
"userId": "user_789",
|
||||||
|
"secret": "your_secret_key_here"
|
||||||
|
}'
|
||||||
|
```
|
||||||
|
|
||||||
|
# Example Response
|
||||||
|
|
||||||
|
Upon successful submission, you will receive a JSON response with the following structure:
|
||||||
|
|
||||||
|
```json
|
||||||
|
{
|
||||||
|
"status": "success",
|
||||||
|
"message": "Test results successfully recorded",
|
||||||
|
"testId": "test_123456789"
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
- `status`: Indicates the status of the request (either "success" or "error").
|
||||||
|
- `message`: Describes the outcome of the request.
|
||||||
|
- `testId`: Unique identifier assigned to the recorded test data.
|
@ -6,9 +6,9 @@
|
|||||||
//! - move structures into a different file
|
//! - move structures into a different file
|
||||||
//! - find a way to make logging in more secure (password hashes?)
|
//! - find a way to make logging in more secure (password hashes?)
|
||||||
|
|
||||||
use rocket::fairing::{Fairing, Info, Kind};
|
// use rocket::fairing::{Fairing, Info, Kind};
|
||||||
use rocket::http::Header;
|
// use rocket::http::Header;
|
||||||
use rocket::{Request, Response};
|
// use rocket::{Request, Response};
|
||||||
|
|
||||||
// pub struct CORS;
|
// pub struct CORS;
|
||||||
|
|
||||||
@ -40,10 +40,8 @@ use rocket::{
|
|||||||
Build, Rocket,
|
Build, Rocket,
|
||||||
};
|
};
|
||||||
|
|
||||||
mod servers;
|
|
||||||
mod typing;
|
mod typing;
|
||||||
|
|
||||||
use crate::servers::server::{server, server_info};
|
|
||||||
use crate::typing::leaderboard::leaderboard;
|
use crate::typing::leaderboard::leaderboard;
|
||||||
use crate::typing::sql::Database;
|
use crate::typing::sql::Database;
|
||||||
use crate::typing::test::{create_test, new_test};
|
use crate::typing::test::{create_test, new_test};
|
||||||
@ -79,7 +77,6 @@ async fn rocket() -> Rocket<Build> {
|
|||||||
new_test,
|
new_test,
|
||||||
],
|
],
|
||||||
)
|
)
|
||||||
.mount("/api", routes![server, server_info])
|
|
||||||
// hosts the fileserver
|
// hosts the fileserver
|
||||||
.mount("/typing", FileServer::from(relative!("websites/Typing")))
|
.mount("/typing", FileServer::from(relative!("websites/Typing")))
|
||||||
.manage(Database::new().await.unwrap())
|
.manage(Database::new().await.unwrap())
|
||||||
|
@ -1 +0,0 @@
|
|||||||
pub mod server;
|
|
@ -1,153 +0,0 @@
|
|||||||
use rocket::serde::{
|
|
||||||
json::{from_str, to_string, Json},
|
|
||||||
Deserialize, Serialize,
|
|
||||||
};
|
|
||||||
|
|
||||||
use std::{
|
|
||||||
fs::{read_dir, File},
|
|
||||||
io::{Read, Write},
|
|
||||||
};
|
|
||||||
|
|
||||||
#[derive(Deserialize, Serialize, Clone, Debug)]
|
|
||||||
#[serde(crate = "rocket::serde")]
|
|
||||||
struct Process {
|
|
||||||
name: String,
|
|
||||||
memory: String,
|
|
||||||
run_time: String,
|
|
||||||
id: String,
|
|
||||||
user_id: String,
|
|
||||||
virtual_memory: String,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Deserialize, Serialize, Clone, Debug)]
|
|
||||||
#[serde(crate = "rocket::serde")]
|
|
||||||
struct Disk {
|
|
||||||
name: String,
|
|
||||||
disk_type: String,
|
|
||||||
total_space: String,
|
|
||||||
available_space: String,
|
|
||||||
usage: String,
|
|
||||||
file_system: String,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Deserialize, Serialize, Clone, Debug)]
|
|
||||||
#[serde(crate = "rocket::serde")]
|
|
||||||
struct CPU {
|
|
||||||
core_count: String,
|
|
||||||
cache: String,
|
|
||||||
clock_speed: String,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Deserialize, Serialize)]
|
|
||||||
#[serde(crate = "rocket::serde")]
|
|
||||||
pub struct System {
|
|
||||||
key: String,
|
|
||||||
host_name: String,
|
|
||||||
uptime: String,
|
|
||||||
os: String,
|
|
||||||
total_ram: String,
|
|
||||||
used_ram: String,
|
|
||||||
available_ram: String,
|
|
||||||
ram_usage: String,
|
|
||||||
total_swap: String,
|
|
||||||
used_swap: String,
|
|
||||||
available_swap: String,
|
|
||||||
swap_usage: String,
|
|
||||||
disks: Vec<Disk>,
|
|
||||||
processes: Vec<Process>,
|
|
||||||
cpu: CPU,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[post("/servers", data = "<data>")]
|
|
||||||
pub fn server(data: Json<System>) {
|
|
||||||
if data.key == "0etnmXPSr95@FNy6A3U9Bw*ZupNIR85zI!hRFGIdj6SW$T" {
|
|
||||||
println!("Data From : {}", data.host_name);
|
|
||||||
}
|
|
||||||
|
|
||||||
let mut file = match File::create(format!("./server/{}.json", &data.host_name)) {
|
|
||||||
Err(why) => {
|
|
||||||
println!("Error: {why}");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
Ok(file) => file,
|
|
||||||
};
|
|
||||||
|
|
||||||
let data: System = System {
|
|
||||||
key: format!("null"),
|
|
||||||
host_name: format!("{}", data.host_name),
|
|
||||||
uptime: format!("{}", data.uptime),
|
|
||||||
os: format!("{}", data.os),
|
|
||||||
total_ram: format!("{}", data.total_ram),
|
|
||||||
used_ram: format!("{}", data.used_ram),
|
|
||||||
available_ram: format!("{}", data.available_ram),
|
|
||||||
ram_usage: format!("{}", data.ram_usage),
|
|
||||||
total_swap: format!("{}", data.total_swap),
|
|
||||||
used_swap: format!("{}", data.used_swap),
|
|
||||||
available_swap: format!("{}", data.available_swap),
|
|
||||||
swap_usage: format!("{}", data.swap_usage),
|
|
||||||
disks: data.disks.clone(),
|
|
||||||
processes: data.processes.clone(),
|
|
||||||
cpu: data.cpu.clone(),
|
|
||||||
};
|
|
||||||
|
|
||||||
let string = match to_string(&data) {
|
|
||||||
Err(why) => {
|
|
||||||
println!("Error: {why}");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
Ok(string) => string,
|
|
||||||
};
|
|
||||||
|
|
||||||
let write = file.write_all(string.as_bytes());
|
|
||||||
|
|
||||||
match write {
|
|
||||||
Err(why) => println!("Error {why}"),
|
|
||||||
Ok(_) => (),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[get("/server_info")]
|
|
||||||
pub fn server_info() -> Json<Vec<System>> {
|
|
||||||
let mut systems: Vec<System> = vec![];
|
|
||||||
let folder = match read_dir("./server/") {
|
|
||||||
Err(why) => {
|
|
||||||
println!("Error: {why}");
|
|
||||||
read_dir("./server/").unwrap()
|
|
||||||
}
|
|
||||||
Ok(dir) => dir,
|
|
||||||
};
|
|
||||||
let mut file: File;
|
|
||||||
let mut string;
|
|
||||||
|
|
||||||
for path in folder {
|
|
||||||
string = String::new();
|
|
||||||
let path = match path {
|
|
||||||
Err(ref why) => {
|
|
||||||
println!("Error: {why}");
|
|
||||||
path.unwrap()
|
|
||||||
}
|
|
||||||
Ok(path) => path,
|
|
||||||
}
|
|
||||||
.path();
|
|
||||||
|
|
||||||
file = match File::open(format!("{}", path.display())) {
|
|
||||||
Err(why) => {
|
|
||||||
println!("Error: {why}");
|
|
||||||
File::open(format!("{}", path.display())).unwrap()
|
|
||||||
}
|
|
||||||
Ok(file) => file,
|
|
||||||
};
|
|
||||||
|
|
||||||
match file.read_to_string(&mut string) {
|
|
||||||
Err(why) => println!("Error: {why}"),
|
|
||||||
Ok(_) => (),
|
|
||||||
};
|
|
||||||
|
|
||||||
match from_str(&string) {
|
|
||||||
Err(why) => println!("Error: {why}"),
|
|
||||||
Ok(string) => systems.push(string),
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
Json(systems)
|
|
||||||
}
|
|
@ -32,7 +32,7 @@ impl Database {
|
|||||||
|
|
||||||
/// Creates the necessary tables inside the database with
|
/// Creates the necessary tables inside the database with
|
||||||
/// correct normalised links between data for later querying
|
/// correct normalised links between data for later querying
|
||||||
pub async fn new_database(&self) -> Result<(), sqlx::Error> {
|
pub async fn _new_database(&self) -> Result<(), sqlx::Error> {
|
||||||
sqlx::query!("
|
sqlx::query!("
|
||||||
CREATE TABLE IF NOT EXISTS Users (
|
CREATE TABLE IF NOT EXISTS Users (
|
||||||
user_id INTEGER PRIMARY KEY,
|
user_id INTEGER PRIMARY KEY,
|
||||||
@ -182,5 +182,4 @@ pub struct Test {
|
|||||||
pub struct LeaderBoardTest {
|
pub struct LeaderBoardTest {
|
||||||
username: String,
|
username: String,
|
||||||
wpm: u8,
|
wpm: u8,
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user