160 lines
6.4 KiB
Rust
160 lines
6.4 KiB
Rust
use super::build_response;
|
|
use crate::api::rpc::RpcHandler;
|
|
use crate::bitcoin_status;
|
|
use crate::electrs_status;
|
|
use anyhow::Result;
|
|
use hyper::{Response, StatusCode};
|
|
use std::sync::Arc;
|
|
|
|
use super::{is_valid_app_id, ApiHandler};
|
|
|
|
impl ApiHandler {
|
|
pub(super) async fn handle_container_logs_http(
|
|
rpc: Arc<RpcHandler>,
|
|
path: &str,
|
|
cors_origin: &str,
|
|
) -> Result<Response<hyper::Body>> {
|
|
let query = path
|
|
.strip_prefix("/api/container/logs")
|
|
.and_then(|s| s.strip_prefix('?'))
|
|
.unwrap_or("");
|
|
let params: std::collections::HashMap<String, String> = query
|
|
.split('&')
|
|
.filter_map(|p| {
|
|
let mut it = p.splitn(2, '=');
|
|
let k = it.next()?.to_string();
|
|
let v = it.next()?.to_string();
|
|
Some((k, v))
|
|
})
|
|
.collect();
|
|
|
|
let app_id = params.get("app_id").map(|s| s.as_str()).unwrap_or("lnd");
|
|
|
|
// Validate app_id format
|
|
if !is_valid_app_id(app_id) {
|
|
let body = serde_json::json!({ "error": "Invalid app_id" });
|
|
let body_bytes = serde_json::to_vec(&body).unwrap_or_default();
|
|
return Ok(build_response(
|
|
StatusCode::BAD_REQUEST,
|
|
"application/json",
|
|
hyper::Body::from(body_bytes),
|
|
));
|
|
}
|
|
|
|
let lines = params
|
|
.get("lines")
|
|
.and_then(|s| s.parse::<u32>().ok())
|
|
.unwrap_or(200);
|
|
|
|
match rpc.get_container_logs_value(app_id, lines).await {
|
|
Ok(value) => {
|
|
let body = serde_json::json!({ "result": value });
|
|
let body_bytes = serde_json::to_vec(&body).unwrap_or_default();
|
|
Ok(Response::builder()
|
|
.status(StatusCode::OK)
|
|
.header("Content-Type", "application/json")
|
|
.header("Access-Control-Allow-Origin", cors_origin)
|
|
.header("Access-Control-Allow-Credentials", "true")
|
|
.header("Vary", "Origin")
|
|
.body(hyper::Body::from(body_bytes))
|
|
.unwrap())
|
|
}
|
|
Err(e) => {
|
|
let body = serde_json::json!({ "error": e.to_string() });
|
|
let body_bytes = serde_json::to_vec(&body).unwrap_or_default();
|
|
Ok(Response::builder()
|
|
.status(StatusCode::INTERNAL_SERVER_ERROR)
|
|
.header("Content-Type", "application/json")
|
|
.header("Access-Control-Allow-Origin", cors_origin)
|
|
.header("Access-Control-Allow-Credentials", "true")
|
|
.header("Vary", "Origin")
|
|
.body(hyper::Body::from(body_bytes))
|
|
.unwrap())
|
|
}
|
|
}
|
|
}
|
|
|
|
pub(super) async fn handle_electrs_status() -> Result<Response<hyper::Body>> {
|
|
let status = electrs_status::get_electrs_sync_status().await;
|
|
let body = serde_json::to_vec(&status).unwrap_or_default();
|
|
Ok(Response::builder()
|
|
.status(StatusCode::OK)
|
|
.header("Content-Type", "application/json")
|
|
.header("Cache-Control", "no-store")
|
|
.body(hyper::Body::from(body))
|
|
.unwrap_or_else(|_| Response::new(hyper::Body::from("{}"))))
|
|
}
|
|
|
|
pub(super) async fn handle_bitcoin_status() -> Result<Response<hyper::Body>> {
|
|
let status = bitcoin_status::get_bitcoin_status().await;
|
|
let body = serde_json::to_vec(&status).unwrap_or_default();
|
|
Ok(Response::builder()
|
|
.status(StatusCode::OK)
|
|
.header("Content-Type", "application/json")
|
|
.header("Cache-Control", "no-store")
|
|
.body(hyper::Body::from(body))
|
|
.unwrap_or_else(|_| Response::new(hyper::Body::from("{}"))))
|
|
}
|
|
|
|
pub(super) async fn handle_lnd_connect_info(
|
|
rpc: std::sync::Arc<super::super::rpc::RpcHandler>,
|
|
) -> Result<Response<hyper::Body>> {
|
|
match rpc.handle_lnd_connect_info().await {
|
|
Ok(val) => {
|
|
let body = serde_json::to_vec(&val).unwrap_or_default();
|
|
Ok(build_response(
|
|
StatusCode::OK,
|
|
"application/json",
|
|
hyper::Body::from(body),
|
|
))
|
|
}
|
|
Err(e) => Ok(Response::builder()
|
|
.status(StatusCode::INTERNAL_SERVER_ERROR)
|
|
.header("Content-Type", "application/json")
|
|
.body(hyper::Body::from(
|
|
serde_json::json!({"error": e.to_string()}).to_string(),
|
|
))
|
|
.unwrap()),
|
|
}
|
|
}
|
|
|
|
pub(super) async fn handle_lnd_proxy(
|
|
path: &str,
|
|
cors_origin: &str,
|
|
) -> Result<Response<hyper::Body>> {
|
|
let suffix = path.strip_prefix("/proxy/lnd").unwrap_or("/");
|
|
let url = format!("http://127.0.0.1:8080{}", suffix);
|
|
match reqwest::get(&url).await {
|
|
Ok(resp) => {
|
|
let status = resp.status().as_u16();
|
|
let headers = resp.headers().clone();
|
|
let body = resp.bytes().await.unwrap_or_default();
|
|
let mut builder = Response::builder().status(status);
|
|
if let Some(ct) = headers.get("content-type") {
|
|
if let Ok(s) = ct.to_str() {
|
|
builder = builder.header("Content-Type", s);
|
|
}
|
|
}
|
|
builder
|
|
.header("Access-Control-Allow-Origin", cors_origin)
|
|
.header("Access-Control-Allow-Credentials", "true")
|
|
.header("Vary", "Origin")
|
|
.body(hyper::Body::from(body))
|
|
.map_err(|e| anyhow::anyhow!("response build: {}", e))
|
|
}
|
|
Err(e) => {
|
|
let body = serde_json::json!({ "error": e.to_string() });
|
|
let body_bytes = serde_json::to_vec(&body).unwrap_or_default();
|
|
Ok(Response::builder()
|
|
.status(StatusCode::BAD_GATEWAY)
|
|
.header("Content-Type", "application/json")
|
|
.header("Access-Control-Allow-Origin", cors_origin)
|
|
.header("Access-Control-Allow-Credentials", "true")
|
|
.header("Vary", "Origin")
|
|
.body(hyper::Body::from(body_bytes))
|
|
.unwrap())
|
|
}
|
|
}
|
|
}
|
|
}
|