From 63a8e863139dc51f404964f135b5dd085f1efec5 Mon Sep 17 00:00:00 2001 From: ArloFilley Date: Wed, 20 Mar 2024 17:42:11 +0000 Subject: [PATCH] added documentation --- documentation/leaderboard.md | 36 +++++++++ documentation/post_test.md | 54 +++++++++++++ src/main.rs | 9 +-- src/servers/mod.rs | 1 - src/servers/server.rs | 153 ----------------------------------- src/typing/sql.rs | 5 +- 6 files changed, 95 insertions(+), 163 deletions(-) create mode 100644 documentation/leaderboard.md delete mode 100644 src/servers/mod.rs delete mode 100644 src/servers/server.rs diff --git a/documentation/leaderboard.md b/documentation/leaderboard.md new file mode 100644 index 0000000..4fb4974 --- /dev/null +++ b/documentation/leaderboard.md @@ -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, + } +] +``` \ No newline at end of file diff --git a/documentation/post_test.md b/documentation/post_test.md index e69de29..b39ba7c 100644 --- a/documentation/post_test.md +++ b/documentation/post_test.md @@ -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. diff --git a/src/main.rs b/src/main.rs index bedf8b1..0628e1e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -6,9 +6,9 @@ //! - move structures into a different file //! - find a way to make logging in more secure (password hashes?) -use rocket::fairing::{Fairing, Info, Kind}; -use rocket::http::Header; -use rocket::{Request, Response}; +// use rocket::fairing::{Fairing, Info, Kind}; +// use rocket::http::Header; +// use rocket::{Request, Response}; // pub struct CORS; @@ -40,10 +40,8 @@ use rocket::{ Build, Rocket, }; -mod servers; mod typing; -use crate::servers::server::{server, server_info}; use crate::typing::leaderboard::leaderboard; use crate::typing::sql::Database; use crate::typing::test::{create_test, new_test}; @@ -79,7 +77,6 @@ async fn rocket() -> Rocket { new_test, ], ) - .mount("/api", routes![server, server_info]) // hosts the fileserver .mount("/typing", FileServer::from(relative!("websites/Typing"))) .manage(Database::new().await.unwrap()) diff --git a/src/servers/mod.rs b/src/servers/mod.rs deleted file mode 100644 index bfe15ae..0000000 --- a/src/servers/mod.rs +++ /dev/null @@ -1 +0,0 @@ -pub mod server; \ No newline at end of file diff --git a/src/servers/server.rs b/src/servers/server.rs deleted file mode 100644 index a319fcf..0000000 --- a/src/servers/server.rs +++ /dev/null @@ -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, - processes: Vec, - cpu: CPU, -} - -#[post("/servers", data = "")] -pub fn server(data: Json) { - 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> { - let mut systems: Vec = 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) -} diff --git a/src/typing/sql.rs b/src/typing/sql.rs index 68b0815..022107c 100644 --- a/src/typing/sql.rs +++ b/src/typing/sql.rs @@ -32,7 +32,7 @@ impl Database { /// Creates the necessary tables inside the database with /// 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!(" CREATE TABLE IF NOT EXISTS Users ( user_id INTEGER PRIMARY KEY, @@ -182,5 +182,4 @@ pub struct Test { pub struct LeaderBoardTest { username: String, wpm: u8, -} - +} \ No newline at end of file