mirror of
https://github.com/PluralKit/PluralKit.git
synced 2026-02-09 15:27:54 +00:00
feat(api): init rust rewrite
This commit is contained in:
parent
5da3c84bce
commit
5440386969
24 changed files with 2443 additions and 586 deletions
42
services/api/src/util.rs
Normal file
42
services/api/src/util.rs
Normal file
|
|
@ -0,0 +1,42 @@
|
|||
use axum::{
|
||||
body::Body,
|
||||
http::{HeaderValue, Request, Response, StatusCode, Uri},
|
||||
response::IntoResponse,
|
||||
};
|
||||
use tracing::error;
|
||||
|
||||
pub fn header_or_unknown(header: Option<&HeaderValue>) -> &str {
|
||||
if let Some(value) = header {
|
||||
match value.to_str() {
|
||||
Ok(v) => v,
|
||||
Err(err) => {
|
||||
error!("failed to parse header value {:#?}: {:#?}", value, err);
|
||||
"failed to parse"
|
||||
}
|
||||
}
|
||||
} else {
|
||||
"unknown"
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn rproxy(req: Request<Body>) -> Response<Body> {
|
||||
let uri = Uri::from_static(&libpk::config.api.remote_url).to_string();
|
||||
|
||||
match hyper_reverse_proxy::call("0.0.0.0".parse().unwrap(), &uri[..uri.len() - 1], req).await {
|
||||
Ok(response) => response,
|
||||
Err(error) => {
|
||||
error!("error proxying request: {:?}", error);
|
||||
Response::builder()
|
||||
.status(StatusCode::INTERNAL_SERVER_ERROR)
|
||||
.body(Body::empty())
|
||||
.unwrap()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn json_err(code: StatusCode, text: String) -> axum::response::Response {
|
||||
let mut response = (code, text).into_response();
|
||||
let headers = response.headers_mut();
|
||||
headers.insert("content-type", HeaderValue::from_static("application/json"));
|
||||
response
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue