Version 1.0
This commit is contained in:
parent
2405c9cf31
commit
f080854b84
2
Cargo.lock
generated
2
Cargo.lock
generated
@ -194,7 +194,7 @@ checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "chela"
|
name = "chela"
|
||||||
version = "0.3.0"
|
version = "1.0.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"axum",
|
"axum",
|
||||||
"color-eyre",
|
"color-eyre",
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "chela"
|
name = "chela"
|
||||||
version = "0.3.1"
|
version = "1.0.0"
|
||||||
edition = "2021"
|
edition = "2021"
|
||||||
|
|
||||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||||
|
@ -43,9 +43,7 @@ pub async fn id(
|
|||||||
Path(id): Path<String>,
|
Path(id): Path<String>,
|
||||||
) -> impl IntoResponse {
|
) -> impl IntoResponse {
|
||||||
let mut show_request = false;
|
let mut show_request = false;
|
||||||
let ip = get_ip(headers.clone(), addr, state.clone())
|
let ip = get_ip(&headers, addr, &state).unwrap_or_default();
|
||||||
.await
|
|
||||||
.unwrap_or_default();
|
|
||||||
log!("Request for '{}' from {}", id.clone(), ip);
|
log!("Request for '{}' from {}", id.clone(), ip);
|
||||||
let mut use_id = id;
|
let mut use_id = id;
|
||||||
if use_id.ends_with('+') {
|
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) {
|
async fn save_analytics(headers: HeaderMap, item: UrlRow, addr: SocketAddr, state: ServerState) {
|
||||||
let id = item.id;
|
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") {
|
let referer = match headers.get("referer") {
|
||||||
Some(it) => {
|
Some(it) => {
|
||||||
if let Ok(i) = it.to_str() {
|
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 {
|
if state.behind_proxy {
|
||||||
match headers.get("x-real-ip") {
|
match headers.get("x-real-ip") {
|
||||||
Some(it) => {
|
Some(it) => {
|
||||||
|
@ -98,7 +98,8 @@ async fn init_db() -> eyre::Result<Pool<Postgres>> {
|
|||||||
CREATE TABLE IF NOT EXISTS chela.urls (
|
CREATE TABLE IF NOT EXISTS chela.urls (
|
||||||
index BIGSERIAL PRIMARY KEY,
|
index BIGSERIAL PRIMARY KEY,
|
||||||
id TEXT NOT NULL UNIQUE,
|
id TEXT NOT NULL UNIQUE,
|
||||||
url TEXT NOT NULL
|
url TEXT NOT NULL,
|
||||||
|
custom_id BOOLEAN NOT NULL
|
||||||
)
|
)
|
||||||
",
|
",
|
||||||
)
|
)
|
||||||
|
10
src/post.rs
10
src/post.rs
@ -44,8 +44,8 @@ pub async fn create_link(
|
|||||||
if let Some(index) = id.index {
|
if let Some(index) = id.index {
|
||||||
res = sqlx::query(
|
res = sqlx::query(
|
||||||
"
|
"
|
||||||
INSERT INTO chela.urls (index,id,url)
|
INSERT INTO chela.urls (index,id,url,custom_id)
|
||||||
VALUES ($1,$2,$3)
|
VALUES ($1,$2,$3,false)
|
||||||
",
|
",
|
||||||
)
|
)
|
||||||
.bind(index)
|
.bind(index)
|
||||||
@ -56,8 +56,8 @@ VALUES ($1,$2,$3)
|
|||||||
} else {
|
} else {
|
||||||
res = sqlx::query(
|
res = sqlx::query(
|
||||||
"
|
"
|
||||||
INSERT INTO chela.urls (id,url)
|
INSERT INTO chela.urls (id,url,custom_id)
|
||||||
VALUES ($1,$2)
|
VALUES ($1,$2,true)
|
||||||
",
|
",
|
||||||
)
|
)
|
||||||
.bind(id.id.clone())
|
.bind(id.id.clone())
|
||||||
@ -102,7 +102,7 @@ VALUES ($1,$2)
|
|||||||
async fn generate_id(form: CreateForm, state: ServerState) -> eyre::Result<NextId> {
|
async fn generate_id(form: CreateForm, state: ServerState) -> eyre::Result<NextId> {
|
||||||
if form.id.is_empty() {
|
if form.id.is_empty() {
|
||||||
let existing_row: Result<UrlRow, sqlx::Error> =
|
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())
|
.bind(form.url.as_str())
|
||||||
.fetch_one(&state.db_pool)
|
.fetch_one(&state.db_pool)
|
||||||
.await;
|
.await;
|
||||||
|
Loading…
Reference in New Issue
Block a user