1
0
Fork 0
mirror of https://github.com/SinTan1729/chhoto-url synced 2025-04-19 11:20:00 -05:00

Compare commits

..

5 commits

5 changed files with 20 additions and 7 deletions

View file

@ -24,6 +24,7 @@ docker-test: docker-local docker-stop
docker run -p ${PORT}:${PORT} --name chhoto-url -e password="${PASSWORD}" -e public_mode="${PUBLIC_MODE}" \ docker run -p ${PORT}:${PORT} --name chhoto-url -e password="${PASSWORD}" -e public_mode="${PUBLIC_MODE}" \
-e site_url="${SITE_URL}" -e db_url="${DB_URL}" -e redirect_method="${REDIRECT_METHOD}" -e port="${PORT}"\ -e site_url="${SITE_URL}" -e db_url="${DB_URL}" -e redirect_method="${REDIRECT_METHOD}" -e port="${PORT}"\
-e slug_style="${SLUG_STYLE}" -e slug_length="${SLUG_LENGTH}" -e cache_control_header="${CACHE_CONTROL_HEADER}"\ -e slug_style="${SLUG_STYLE}" -e slug_length="${SLUG_LENGTH}" -e cache_control_header="${CACHE_CONTROL_HEADER}"\
-e api_key="${API_KEY}"\
-d chhoto-url -d chhoto-url
docker logs chhoto-url -f docker logs chhoto-url -f

View file

@ -193,10 +193,13 @@ The server will send a confirmation.
In order to use API key validation, set the `api_key` environment variable. If this is not set, the API will default to cookie In order to use API key validation, set the `api_key` environment variable. If this is not set, the API will default to cookie
validation (see section above). If the API key is insecure, a warning will be outputted along with a generated API key which may be used. validation (see section above). If the API key is insecure, a warning will be outputted along with a generated API key which may be used.
Example Linux command for generating a secure API key: `tr -dc A-Za-z0-9 </dev/urandom | head -c 128`
To add a link: To add a link:
``` bash ``` bash
curl -X POST -H "X-API-Key: <YOUR_API_KEY>" -d '{"shortlink":"<shortlink>", "longlink":"<longlink>"}' http://localhost:4567/api/new curl -X POST -H "X-API-Key: <YOUR_API_KEY>" -d '{"shortlink":"<shortlink>", "longlink":"<longlink>"}' http://localhost:4567/api/new
``` ```
Send an empty `<shortlink>` if you want it to be auto-generated. The server will reply with the generated shortlink.
To get a list of all the currently available links: To get a list of all the currently available links:
``` bash ``` bash

View file

@ -43,15 +43,16 @@ async fn main() -> Result<()> {
if let Ok(key) = env::var("api_key") { if let Ok(key) = env::var("api_key") {
if !auth::is_key_secure() { if !auth::is_key_secure() {
eprintln!("API key is insecure! Please change it. Current key is: {}. Generated secure key which you may use: {}", key, auth::gen_key()) eprintln!("API key is insecure! Please change it. Current key is: {}. Generated secure key which you may use: {}", key, auth::gen_key())
} else {
eprintln!("Secure API key was provided.")
} }
} }
// Tell the user that the server has started, and where it is listening to, rather than simply outputting nothing // Tell the user that the server has started, and where it is listening to, rather than simply outputting nothing
eprintln!( eprintln!("Server has started at 0.0.0.0 on port {port}.");
"Server has started at 0.0.0.0 on port {}. Configured Site URL is: {}", if let Some(site_url) = env::var("site_url").ok().filter(|s| !s.trim().is_empty()) {
port, eprintln!("Configured Site URL is: {site_url}.");
env::var("site_url").unwrap_or(String::from("http://localhost")) }
);
// Actually start the server // Actually start the server
HttpServer::new(move || { HttpServer::new(move || {

View file

@ -61,7 +61,10 @@ pub async fn add_link(
.expect("Supplied port is not an integer"); .expect("Supplied port is not an integer");
let url = format!( let url = format!(
"{}:{}", "{}:{}",
env::var("site_url").unwrap_or(String::from("http://localhost")), env::var("site_url")
.ok()
.filter(|s| !s.trim().is_empty())
.unwrap_or(String::from("http://localhost")),
port port
); );
let response = CreatedURL { let response = CreatedURL {

View file

@ -62,7 +62,12 @@ pub fn is_api_ok(http: HttpRequest) -> Response {
} else { } else {
// If the API key isn't set, but an API Key header is provided // If the API key isn't set, but an API Key header is provided
if auth::api_header(&http).is_some() { if auth::api_header(&http).is_some() {
Response {success: false, error: true, reason: "An API key was provided, but the 'api_key' environment variable is not configured in the Chhoto URL instance".to_string(), pass: false} Response {
success: false,
error: true,
reason: "An API key was provided, but the 'api_key' environment variable is not configured in the Chhoto URL instance".to_string(),
pass: false
}
} else { } else {
Response { Response {
success: false, success: false,