mirror of
https://github.com/YGGverse/btracker-gemini.git
synced 2026-03-31 09:05:30 +00:00
optimize duplicated constructions
This commit is contained in:
parent
96c3df1b41
commit
b31d28ebee
1 changed files with 34 additions and 60 deletions
66
src/main.rs
66
src/main.rs
|
|
@ -75,7 +75,7 @@ fn handle(
|
||||||
peer: SocketAddr,
|
peer: SocketAddr,
|
||||||
connection: Result<TlsStream<TcpStream>, HandshakeError<TcpStream>>,
|
connection: Result<TlsStream<TcpStream>, HandshakeError<TcpStream>>,
|
||||||
) {
|
) {
|
||||||
debug!("New peer connected: `{peer}`");
|
debug!("Incoming connection from: `{peer}`");
|
||||||
match connection {
|
match connection {
|
||||||
Ok(mut stream) => {
|
Ok(mut stream) => {
|
||||||
// server should work with large files without memory overload,
|
// server should work with large files without memory overload,
|
||||||
|
|
@ -153,15 +153,10 @@ fn response(
|
||||||
stream: &mut TlsStream<TcpStream>,
|
stream: &mut TlsStream<TcpStream>,
|
||||||
) {
|
) {
|
||||||
use route::Route;
|
use route::Route;
|
||||||
|
|
||||||
debug!("Incoming request from `{peer}` to `{}`", request.url.path());
|
debug!("Incoming request from `{peer}` to `{}`", request.url.path());
|
||||||
|
send(
|
||||||
let route = Route::from_url(&request.url);
|
&match Route::from_url(&request.url) {
|
||||||
|
Route::List { page } => match list(config, public, request.url.query(), page) {
|
||||||
// try index page, including optional page value
|
|
||||||
match route {
|
|
||||||
Route::List { page } => send(
|
|
||||||
&match list(config, public, request.url.query(), page) {
|
|
||||||
Ok(data) => response::success::Default {
|
Ok(data) => response::success::Default {
|
||||||
data: data.as_bytes(),
|
data: data.as_bytes(),
|
||||||
meta: response::success::default::Meta {
|
meta: response::success::default::Meta {
|
||||||
|
|
@ -177,31 +172,12 @@ fn response(
|
||||||
.into_bytes()
|
.into_bytes()
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
stream,
|
Route::Search => response::Input::Default(response::input::Default {
|
||||||
|result| {
|
|
||||||
if let Err(e) = result {
|
|
||||||
error!("Internal server error on handle peer `{peer}` request: `{e}`")
|
|
||||||
}
|
|
||||||
},
|
|
||||||
),
|
|
||||||
Route::Search => send(
|
|
||||||
&response::Input::Default(response::input::Default {
|
|
||||||
message: Some("Keyword, file, hash...".into()),
|
message: Some("Keyword, file, hash...".into()),
|
||||||
})
|
})
|
||||||
.into_bytes(),
|
.into_bytes(),
|
||||||
stream,
|
|
||||||
|result| {
|
|
||||||
if let Err(e) = result {
|
|
||||||
error!(
|
|
||||||
"Internal server error on handle peer `{peer}` request `{}`: `{e}`",
|
|
||||||
request.url.as_str()
|
|
||||||
)
|
|
||||||
}
|
|
||||||
},
|
|
||||||
),
|
|
||||||
Route::Info(id) => match public.torrent(id) {
|
Route::Info(id) => match public.torrent(id) {
|
||||||
Some(torrent) => send(
|
Some(torrent) => match info(config, torrent) {
|
||||||
&match info(config, torrent) {
|
|
||||||
Ok(data) => response::success::Default {
|
Ok(data) => response::success::Default {
|
||||||
data: data.as_bytes(),
|
data: data.as_bytes(),
|
||||||
meta: response::success::default::Meta {
|
meta: response::success::default::Meta {
|
||||||
|
|
@ -217,37 +193,35 @@ fn response(
|
||||||
.into_bytes()
|
.into_bytes()
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
stream,
|
None => {
|
||||||
|result| {
|
warn!(
|
||||||
if let Err(e) = result {
|
"Requested resource `{}` not found by peer `{peer}`",
|
||||||
error!("Internal server error on handle peer `{peer}` request: `{e}`")
|
request.url.as_str()
|
||||||
|
);
|
||||||
|
response::Failure::Permanent(response::failure::Permanent::NotFound(
|
||||||
|
response::failure::permanent::NotFound { message: None },
|
||||||
|
))
|
||||||
|
.into_bytes()
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
),
|
|
||||||
None => todo!(),
|
|
||||||
},
|
|
||||||
Route::NotFound => {
|
Route::NotFound => {
|
||||||
warn!(
|
warn!(
|
||||||
"Requested resource `{}` not found by peer `{peer}`",
|
"Requested resource `{}` not found by peer `{peer}`",
|
||||||
request.url.as_str()
|
request.url.as_str()
|
||||||
);
|
);
|
||||||
send(
|
response::Failure::Permanent(response::failure::Permanent::NotFound(
|
||||||
&response::Failure::Permanent(response::failure::Permanent::NotFound(
|
|
||||||
response::failure::permanent::NotFound { message: None },
|
response::failure::permanent::NotFound { message: None },
|
||||||
))
|
))
|
||||||
.into_bytes(),
|
.into_bytes()
|
||||||
|
}
|
||||||
|
},
|
||||||
stream,
|
stream,
|
||||||
|result| {
|
|result| {
|
||||||
if let Err(e) = result {
|
if let Err(e) = result {
|
||||||
error!(
|
error!("Internal server error on handle peer `{peer}` request: `{e}`")
|
||||||
"Internal server error on handle peer `{peer}` request `{}`: `{e}`",
|
|
||||||
request.url.as_str()
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn send(data: &[u8], stream: &mut TlsStream<TcpStream>, callback: impl FnOnce(Result<()>)) {
|
fn send(data: &[u8], stream: &mut TlsStream<TcpStream>, callback: impl FnOnce(Result<()>)) {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue