mirror of
https://github.com/SinTan1729/chhoto-url
synced 2025-02-05 13:52:33 -06:00
Changes the API to use JSON data and results
This commit is contained in:
parent
2c56c68637
commit
5c2886f651
3 changed files with 77 additions and 4 deletions
47
actix/Cargo.lock
generated
47
actix/Cargo.lock
generated
|
@ -515,6 +515,7 @@ dependencies = [
|
||||||
"actix-web-httpauth",
|
"actix-web-httpauth",
|
||||||
"env_logger",
|
"env_logger",
|
||||||
"nanoid",
|
"nanoid",
|
||||||
|
"passwords",
|
||||||
"rand",
|
"rand",
|
||||||
"regex",
|
"regex",
|
||||||
"rusqlite",
|
"rusqlite",
|
||||||
|
@ -1260,6 +1261,15 @@ dependencies = [
|
||||||
"windows-targets",
|
"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]]
|
[[package]]
|
||||||
name = "paste"
|
name = "paste"
|
||||||
version = "1.0.15"
|
version = "1.0.15"
|
||||||
|
@ -1317,6 +1327,12 @@ dependencies = [
|
||||||
"zerocopy",
|
"zerocopy",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "proc-macro-hack"
|
||||||
|
version = "0.5.20+deprecated"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "dc375e1527247fe1a97d8b7156678dfe7c1af2fc075c9a4db3690ecd2a148068"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "proc-macro2"
|
name = "proc-macro2"
|
||||||
version = "1.0.89"
|
version = "1.0.89"
|
||||||
|
@ -1365,6 +1381,37 @@ dependencies = [
|
||||||
"getrandom",
|
"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]]
|
[[package]]
|
||||||
name = "redox_syscall"
|
name = "redox_syscall"
|
||||||
version = "0.5.7"
|
version = "0.5.7"
|
||||||
|
|
|
@ -25,6 +25,14 @@ struct Response {
|
||||||
reason: String,
|
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
|
// Define the routes
|
||||||
|
|
||||||
// Add new links
|
// Add new links
|
||||||
|
@ -36,9 +44,24 @@ pub async fn add_link(req: String, data: web::Data<AppState>, session: Session,
|
||||||
if result.success {
|
if result.success {
|
||||||
let out = utils::add_link(req, &data.db);
|
let out = utils::add_link(req, &data.db);
|
||||||
if out.0 {
|
if out.0 {
|
||||||
HttpResponse::Created().body(out.1)
|
let port = env::var("port")
|
||||||
|
.unwrap_or(String::from("4567"))
|
||||||
|
.parse::<u16>()
|
||||||
|
.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 {
|
} else {
|
||||||
HttpResponse::Conflict().body(out.1)
|
let response = Response {
|
||||||
|
success: false,
|
||||||
|
error: true,
|
||||||
|
reason: out.1
|
||||||
|
};
|
||||||
|
HttpResponse::Conflict().json(response)
|
||||||
}
|
}
|
||||||
} else if result.error {
|
} else if result.error {
|
||||||
HttpResponse::Unauthorized().json(result)
|
HttpResponse::Unauthorized().json(result)
|
||||||
|
|
|
@ -25,7 +25,10 @@ services:
|
||||||
|
|
||||||
- password=TopSecretPass
|
- 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
|
# Pass the redirect method, if needed. TEMPORARY and PERMANENT
|
||||||
# are accepted values, defaults to PERMANENT.
|
# are accepted values, defaults to PERMANENT.
|
||||||
|
|
Loading…
Reference in a new issue