Version 1.0

This commit is contained in:
Shav Kinderlehrer 2024-04-07 15:35:46 -04:00
parent 2405c9cf31
commit f080854b84
5 changed files with 12 additions and 13 deletions

2
Cargo.lock generated
View File

@ -194,7 +194,7 @@ checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
[[package]]
name = "chela"
version = "0.3.0"
version = "1.0.0"
dependencies = [
"axum",
"color-eyre",

View File

@ -1,6 +1,6 @@
[package]
name = "chela"
version = "0.3.1"
version = "1.0.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

View File

@ -43,9 +43,7 @@ pub async fn id(
Path(id): Path<String>,
) -> impl IntoResponse {
let mut show_request = false;
let ip = get_ip(headers.clone(), addr, state.clone())
.await
.unwrap_or_default();
let ip = get_ip(&headers, addr, &state).unwrap_or_default();
log!("Request for '{}' from {}", id.clone(), ip);
let mut use_id = id;
if use_id.ends_with('+') {
@ -96,7 +94,7 @@ pub async fn id(
async fn save_analytics(headers: HeaderMap, item: UrlRow, addr: SocketAddr, state: ServerState) {
let id = item.id;
let ip = get_ip(headers.clone(), addr, state.clone()).await;
let ip = get_ip(&headers, addr, &state);
let referer = match headers.get("referer") {
Some(it) => {
if let Ok(i) = it.to_str() {
@ -136,7 +134,7 @@ VALUES ($1,$2,$3,$4)
}
}
async fn get_ip(headers: HeaderMap, addr: SocketAddr, state: ServerState) -> Option<String> {
fn get_ip(headers: &HeaderMap, addr: SocketAddr, state: &ServerState) -> Option<String> {
if state.behind_proxy {
match headers.get("x-real-ip") {
Some(it) => {

View File

@ -98,7 +98,8 @@ async fn init_db() -> eyre::Result<Pool<Postgres>> {
CREATE TABLE IF NOT EXISTS chela.urls (
index BIGSERIAL PRIMARY KEY,
id TEXT NOT NULL UNIQUE,
url TEXT NOT NULL
url TEXT NOT NULL,
custom_id BOOLEAN NOT NULL
)
",
)

View File

@ -44,8 +44,8 @@ pub async fn create_link(
if let Some(index) = id.index {
res = sqlx::query(
"
INSERT INTO chela.urls (index,id,url)
VALUES ($1,$2,$3)
INSERT INTO chela.urls (index,id,url,custom_id)
VALUES ($1,$2,$3,false)
",
)
.bind(index)
@ -56,8 +56,8 @@ VALUES ($1,$2,$3)
} else {
res = sqlx::query(
"
INSERT INTO chela.urls (id,url)
VALUES ($1,$2)
INSERT INTO chela.urls (id,url,custom_id)
VALUES ($1,$2,true)
",
)
.bind(id.id.clone())
@ -102,7 +102,7 @@ VALUES ($1,$2)
async fn generate_id(form: CreateForm, state: ServerState) -> eyre::Result<NextId> {
if form.id.is_empty() {
let existing_row: Result<UrlRow, sqlx::Error> =
sqlx::query_as("SELECT * FROM chela.urls WHERE url = $1")
sqlx::query_as("SELECT * FROM chela.urls WHERE url = $1 AND custom_id = 'false'")
.bind(form.url.as_str())
.fetch_one(&state.db_pool)
.await;