👕 refactor: Extract functions for scraper API endpoints

main
_ 2020-12-13 01:12:56 +00:00
parent 622895f77d
commit 1e81421444
1 changed files with 82 additions and 45 deletions

View File

@ -409,64 +409,17 @@ async fn handle_server_list (
}
#[instrument (level = "trace", skip (req, state))]
async fn handle_all (req: Request <Body>, state: Arc <RelayState>)
async fn handle_scraper_api_v1 (
req: Request <Body>,
state: Arc <RelayState>,
path_rest: &str
)
-> Result <Response <Body>, RequestError>
{
let path = req.uri ().path ();
//println! ("{}", path);
debug! ("Request path: {}", path);
use key_validity::KeyValidity;
let api_key = req.headers ().get ("X-ApiKey");
if req.method () == Method::POST {
// This is stuff the server can use. Clients can't
// POST right now
return if let Some (request_code) = prefix_match ("/7ZSFUKGV/http_response/", path) {
let request_code = request_code.into ();
Ok (server_endpoint::handle_response (req, state, request_code).await?)
}
else {
Ok (error_reply (StatusCode::BAD_REQUEST, "Can't POST this")?)
};
}
if let Some (listen_code) = prefix_match ("/7ZSFUKGV/http_listen/", path) {
let api_key = match api_key {
None => return Ok (error_reply (StatusCode::FORBIDDEN, "Can't run server without an API key")?),
Some (x) => x,
};
server_endpoint::handle_listen (state, listen_code.into (), api_key.as_bytes ()).await
}
else if let Some (rest) = prefix_match ("/frontend/servers/", path) {
if rest == "" {
Ok (handle_server_list (state).await?)
}
else if let Some (idx) = rest.find ('/') {
let listen_code = String::from (&rest [0..idx]);
let path = String::from (&rest [idx..]);
let (parts, _) = req.into_parts ();
Ok (handle_http_request (parts, path, state, listen_code).await?)
}
else {
Ok (error_reply (StatusCode::BAD_REQUEST, "Bad URI format")?)
}
}
else if path == "/" {
let s = state.handlebars.render ("relay_root", &())?;
Ok (ok_reply (s)?)
}
else if path == "/frontend/relay_up_check" {
Ok (error_reply (StatusCode::OK, "Relay is up")?)
}
else if path == "/frontend/test_mysterious_error" {
Err (RequestError::Mysterious)
}
else if path == "/scraper/v1/test" || path == "/scraper/api/test" {
use key_validity::KeyValidity;
let api_key = match api_key {
None => return Ok (error_reply (StatusCode::FORBIDDEN, "Can't run scraper without an API key")?),
Some (x) => x,
@ -502,8 +455,92 @@ async fn handle_all (req: Request <Body>, state: Arc <RelayState>)
}
}
if path_rest == "test" {
Ok (error_reply (StatusCode::OK, "You're valid!")?)
}
else {
Ok (error_reply (StatusCode::NOT_FOUND, "Unknown API endpoint")?)
}
}
#[instrument (level = "trace", skip (req, state))]
async fn handle_scraper_api (
req: Request <Body>,
state: Arc <RelayState>,
path_rest: &str
)
-> Result <Response <Body>, RequestError>
{
if let Some (rest) = prefix_match ("v1/", path_rest) {
handle_scraper_api_v1 (req, state, rest).await
}
else if let Some (rest) = prefix_match ("api/", path_rest) {
handle_scraper_api_v1 (req, state, rest).await
}
else {
Ok (error_reply (StatusCode::NOT_FOUND, "Unknown scraper API version")?)
}
}
#[instrument (level = "trace", skip (req, state))]
async fn handle_all (req: Request <Body>, state: Arc <RelayState>)
-> Result <Response <Body>, RequestError>
{
let path = req.uri ().path ().to_string ();
//println! ("{}", path);
debug! ("Request path: {}", path);
if req.method () == Method::POST {
// This is stuff the server can use. Clients can't
// POST right now
return if let Some (request_code) = prefix_match ("/7ZSFUKGV/http_response/", &path) {
let request_code = request_code.into ();
Ok (server_endpoint::handle_response (req, state, request_code).await?)
}
else {
Ok (error_reply (StatusCode::BAD_REQUEST, "Can't POST this")?)
};
}
if let Some (listen_code) = prefix_match ("/7ZSFUKGV/http_listen/", &path) {
let api_key = req.headers ().get ("X-ApiKey");
let api_key = match api_key {
None => return Ok (error_reply (StatusCode::FORBIDDEN, "Can't run server without an API key")?),
Some (x) => x,
};
server_endpoint::handle_listen (state, listen_code.into (), api_key.as_bytes ()).await
}
else if let Some (rest) = prefix_match ("/frontend/servers/", &path) {
if rest == "" {
Ok (handle_server_list (state).await?)
}
else if let Some (idx) = rest.find ('/') {
let listen_code = String::from (&rest [0..idx]);
let path = String::from (&rest [idx..]);
let (parts, _) = req.into_parts ();
Ok (handle_http_request (parts, path, state, listen_code).await?)
}
else {
Ok (error_reply (StatusCode::BAD_REQUEST, "Bad URI format")?)
}
}
else if path == "/" {
let s = state.handlebars.render ("relay_root", &())?;
Ok (ok_reply (s)?)
}
else if path == "/frontend/relay_up_check" {
Ok (error_reply (StatusCode::OK, "Relay is up")?)
}
else if path == "/frontend/test_mysterious_error" {
Err (RequestError::Mysterious)
}
else if let Some (rest) = prefix_match ("/scraper/", &path) {
handle_scraper_api (req, state, rest).await
}
else {
Ok (error_reply (StatusCode::OK, "Hi")?)
}