1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
|
use std::net::SocketAddr;
use axum::extract::{ConnectInfo, Path};
use axum::http::HeaderMap;
use axum::http::StatusCode;
use axum::response::{Html, IntoResponse};
use axum::Extension;
use info_utils::prelude::*;
use crate::ServerState;
use crate::UrlRow;
pub async fn index() -> Html<&'static str> {
Html("hello, world!")
}
/// # Panics
/// Will panic if `parse()` fails
pub async fn id(
headers: HeaderMap,
ConnectInfo(addr): ConnectInfo<SocketAddr>,
Extension(state): Extension<ServerState>,
Path(id): Path<String>,
) -> impl IntoResponse {
let mut show_request = false;
log!("Request for '{}' from {}", id.clone(), addr.ip());
let mut use_id = id;
if use_id.ends_with('+') {
show_request = true;
use_id.pop();
}
let item: Result<UrlRow, sqlx::Error> =
sqlx::query_as("SELECT * FROM chela.urls WHERE id = $1")
.bind(use_id)
.fetch_one(&state.db_pool)
.await;
if let Ok(it) = item {
if url::Url::parse(&it.url).is_ok() {
if show_request {
return Html(format!(
r#"<pre>http://{}/{} -> <a href="{}"">{}</a></pre>"#,
state.host, it.id, it.url, it.url
))
.into_response();
}
log!("Redirecting {} -> {}", it.id, it.url);
save_analytics(headers, it.clone(), addr, state).await;
let mut response_headers = HeaderMap::new();
response_headers.insert("Cache-Control", "private, max-age=90".parse().unwrap());
response_headers.insert("Location", it.url.parse().unwrap());
return (
StatusCode::MOVED_PERMANENTLY,
response_headers,
Html(format!(
r#"Redirecting to <a href="{}">{}</a>"#,
it.url, it.url
)),
)
.into_response();
}
} else if let Err(err) = item {
warn!("{}", err);
return (
StatusCode::INTERNAL_SERVER_ERROR,
Html(format!("<pre>Internal error: {err}.</pre>")),
)
.into_response();
}
(StatusCode::NOT_FOUND, Html("<pre>Not found.</pre>")).into_response()
}
async fn save_analytics(headers: HeaderMap, item: UrlRow, addr: SocketAddr, state: ServerState) {
let id = item.id;
let ip = addr.ip().to_string();
let referer = match headers.get("referer") {
Some(it) => {
if let Ok(i) = it.to_str() {
Some(i)
} else {
None
}
}
None => None,
};
let user_agent = match headers.get("user-agent") {
Some(it) => {
if let Ok(i) = it.to_str() {
Some(i)
} else {
None
}
}
None => None,
};
let res = sqlx::query(
"
INSERT INTO chela.tracking (id,ip,referrer,user_agent)
VALUES ($1,$2,$3,$4)
",
)
.bind(id.clone())
.bind(ip.clone())
.bind(referer)
.bind(user_agent)
.execute(&state.db_pool)
.await;
if res.is_ok() {
log!("Saved analytics for '{id}' from {ip}");
}
}
pub async fn create_id(Extension(state): Extension<ServerState>) -> Html<String> {
Html(format!(
r#"
<!DOCTYPE html>
<html>
<head>
<title>{} URL Shortener</title>
</head>
<body>
<form action="/" method="post">
<label for="url">
URL to shorten:
<input type="url" name="url" required>
</label>
<label for="id">
ID (optional):
<input type="text" name="id">
</label>
<input type="submit" value="create">
</form>
</body>
</html>
"#,
state.host
))
}
|