Skip to content
Snippets Groups Projects
Commit e79ae7fe authored by Korben Tompkin's avatar Korben Tompkin :mortar_board:
Browse files

Merge branch 'server-implementation' into 'main'

feat(server): Server implementation

See merge request !3
parents f0c8eadf 5e1a02cb
No related branches found
No related tags found
1 merge request!3feat(server): Server implementation
......@@ -2,4 +2,7 @@
static/syn*
*/*.key
\ No newline at end of file
venv
*.json
*.log
This diff is collapsed.
......@@ -35,4 +35,4 @@ fn main() -> Result<()>{
},
}
Ok(())
}
\ No newline at end of file
}
......@@ -5,6 +5,9 @@ edition = "2021"
[dependencies]
axum = "0.8.1"
hex = "0.4.3"
serde = "1.0.217"
serde_json = "1.0.138"
sqlx = { version = "0.8.3", features = ["runtime-tokio"] }
tiger = "0.2.1"
tokio = { version = "1.43.0", features = ["macros", "rt-multi-thread"] }
......@@ -4,11 +4,18 @@ use axum::{
routing::{get, post},
Router,
};
use hex;
use serde::{Deserialize, Serialize};
use serde_json::{json, Value};
use std::{collections::HashMap, fs};
use tiger::{Digest, Tiger};
use tokio;
#[derive(Serialize, Deserialize)]
struct ED {
db: HashMap<String, String>,
}
#[tokio::main]
async fn main() {
let app = Router::new()
......@@ -21,19 +28,58 @@ async fn main() {
axum::serve(listener, app).await.unwrap();
}
/// Initializes the database from a json payload.
///
/// Dumps the contents into `db.json`.
async fn init(Json(payload): Json<Value>) -> Result<(), StatusCode> {
match payload.as_str() {
Some(db) => {
if let Ok(_) = fs::write("./db/database.json", db) {
Ok(())
match fs::write("./db.json", payload.to_string()) {
Ok(_) => Ok(()),
Err(x) => {
println!("{:#?}", x);
Err(StatusCode::INTERNAL_SERVER_ERROR)
}
}
}
/// Searches for database entries matching the `key` path parameter.
///
/// Hashes the key with a counter until no more results are found.
///
/// Returns json in the form:
///
/// ```json
/// {
/// ctr: "ciphertext"
/// }
/// ```
async fn search(Path(key): Path<String>) -> Result<Json<Value>, StatusCode> {
// Read the database from db.json
let data_string = match fs::read_to_string("./db.json") {
Ok(x) => x,
Err(_) => String::from(""),
};
// Parse the database
let ed: ED = ED {
db: {
if let Ok(db) = serde_json::from_str(&data_string) {
db
} else {
Err(StatusCode::INTERNAL_SERVER_ERROR)
return Err(StatusCode::INTERNAL_SERVER_ERROR);
}
}
None => Err(StatusCode::INTERNAL_SERVER_ERROR),
},
};
let mut n = 0;
let mut ids: HashMap<u32, &String> = HashMap::new();
// The actual search
while let Some(ciphertext) = ed.db.get(&hash(format!("{}{}", key, n))) {
ids.insert(n, ciphertext);
n += 1;
}
Ok(Json(json!(ids)))
}
async fn search(Path(query): Path<String>) -> Result<Json<Value>, StatusCode> {
Ok(Json(json!({query: ""})))
fn hash(plaintext: String) -> String {
let mut hf = Tiger::new();
hf.update(plaintext.as_bytes());
hex::encode(hf.finalize().as_slice())
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment