From 5c2886f65101f888ec1a98cd3129b430f8c2d2d8 Mon Sep 17 00:00:00 2001 From: Solninja A Date: Tue, 31 Dec 2024 19:11:47 +1000 Subject: [PATCH] Changes the API to use JSON data and results --- actix/Cargo.lock | 47 +++++++++++++++++++++++++++++++++++++++++++ actix/src/services.rs | 27 +++++++++++++++++++++++-- compose.yaml | 7 +++++-- 3 files changed, 77 insertions(+), 4 deletions(-) diff --git a/actix/Cargo.lock b/actix/Cargo.lock index 69d364f..a7d72cb 100644 --- a/actix/Cargo.lock +++ b/actix/Cargo.lock @@ -515,6 +515,7 @@ dependencies = [ "actix-web-httpauth", "env_logger", "nanoid", + "passwords", "rand", "regex", "rusqlite", @@ -1260,6 +1261,15 @@ dependencies = [ "windows-targets", ] +[[package]] +name = "passwords" +version = "3.1.16" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "11407193a7c2bd14ec6b0ec3394da6fdcf7a4d5dcbc8c3cc38dfb17802c8d59c" +dependencies = [ + "random-pick", +] + [[package]] name = "paste" version = "1.0.15" @@ -1317,6 +1327,12 @@ dependencies = [ "zerocopy", ] +[[package]] +name = "proc-macro-hack" +version = "0.5.20+deprecated" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dc375e1527247fe1a97d8b7156678dfe7c1af2fc075c9a4db3690ecd2a148068" + [[package]] name = "proc-macro2" version = "1.0.89" @@ -1365,6 +1381,37 @@ dependencies = [ "getrandom", ] +[[package]] +name = "random-number" +version = "0.1.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7fc8cdd49be664772ffc3dbfa743bb8c34b78f9cc6a9f50e56ae878546796067" +dependencies = [ + "proc-macro-hack", + "rand", + "random-number-macro-impl", +] + +[[package]] +name = "random-number-macro-impl" +version = "0.1.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f5135143cb48d14289139e4615bffec0d59b4cbfd4ea2398a3770bd2abfc4aa2" +dependencies = [ + "proc-macro-hack", + "quote", + "syn", +] + +[[package]] +name = "random-pick" +version = "1.2.16" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c179499072da789afe44127d5f4aa6012de2c2f96ef759990196b37387a2a0f8" +dependencies = [ + "random-number", +] + [[package]] name = "redox_syscall" version = "0.5.7" diff --git a/actix/src/services.rs b/actix/src/services.rs index 172612c..cbfbf26 100644 --- a/actix/src/services.rs +++ b/actix/src/services.rs @@ -25,6 +25,14 @@ struct Response { reason: String, } +// Needs to return the short URL to make it easier for programs leveraging the API +#[derive(Serialize)] +struct CreatedURL { + success: bool, + error: bool, + shorturl: String, +} + // Define the routes // Add new links @@ -36,9 +44,24 @@ pub async fn add_link(req: String, data: web::Data, session: Session, if result.success { let out = utils::add_link(req, &data.db); if out.0 { - HttpResponse::Created().body(out.1) + let port = env::var("port") + .unwrap_or(String::from("4567")) + .parse::() + .expect("Supplied port is not an integer"); + let url = format!("{}:{}", env::var("site_url").unwrap_or(String::from("http://localhost")), port); + let response = CreatedURL { + success: true, + error: false, + shorturl: format!("{}/{}", url, out.1) + }; + HttpResponse::Created().json(response) } else { - HttpResponse::Conflict().body(out.1) + let response = Response { + success: false, + error: true, + reason: out.1 + }; + HttpResponse::Conflict().json(response) } } else if result.error { HttpResponse::Unauthorized().json(result) diff --git a/compose.yaml b/compose.yaml index 256b19d..8e60c6f 100644 --- a/compose.yaml +++ b/compose.yaml @@ -24,8 +24,11 @@ services: # - site_url=https://www.example.com - password=TopSecretPass - - - api_key=test + + # This needs to be set in order to use programs that use the JSON interface of Chhoto URL. + # You will get a warning if this is insecure, and a generated value will be outputted + # You may use that value if you can't think of a secure key + # - api_key=SECURE_API_KEY # Pass the redirect method, if needed. TEMPORARY and PERMANENT # are accepted values, defaults to PERMANENT.