mirror of
https://github.com/SinTan1729/chhoto-url
synced 2025-02-05 05:42:32 -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",
|
||||
"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"
|
||||
|
|
|
@ -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<AppState>, 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::<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 {
|
||||
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)
|
||||
|
|
|
@ -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.
|
||||
|
|
Loading…
Reference in a new issue