Skip to content

Commit

Permalink
httpd: implementing a json response
Browse files Browse the repository at this point in the history
Signed-off-by: Vincenzo Palazzo <[email protected]>
  • Loading branch information
vincenzopalazzo committed Feb 11, 2025
1 parent 1ba7036 commit c7e3868
Showing 1 changed file with 56 additions and 3 deletions.
59 changes: 56 additions & 3 deletions lampo-httpd/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,69 @@ use std::net::ToSocketAddrs;
use std::{fmt::Display, sync::Arc};

use actix::{web, HttpResponseWrapper, OpenApiExt};
use actix_web::{App, HttpResponse, HttpServer};
use actix_web::{App, HttpResponse, HttpServer, ResponseError};
use paperclip::actix::{self, CreatedJson};

use lampo_common::error;
use lampo_common::json;
use lampod::LampoDaemon;

use commands::inventory::{rest_funds, rest_getinfo, rest_networkchannels};
use commands::offchain::{rest_decode, rest_invoice, rest_pay};
use commands::peer::{rest_channels, rest_close, rest_connect, rest_fundchannel};

/// Result type for json responses
pub type ResultJson<T> = std::result::Result<CreatedJson<T>, actix_web::Error>;

#[derive(Debug)]
struct JsonRPCError {
code: i32,
message: String,
data: Option<json::Value>,
}

impl Display for JsonRPCError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "code: {}, message: {}", self.code, self.message)
}
}

impl ResponseError for JsonRPCError {
fn status_code(&self) -> actix_web::http::StatusCode {
actix_web::http::StatusCode::INTERNAL_SERVER_ERROR
}

fn error_response(&self) -> actix_web::HttpResponse {
let status_code = self.status_code().clone();
let body = json::json!({
"code": self.code,
"message": self.message,
"data": self.data,
});
let body = json::to_string(&body).unwrap();
actix_web::HttpResponse::new(status_code).set_body(actix_web::body::BoxBody::new(
actix_web::web::Bytes::from(body),
))
}
}

impl From<lampo_common::jsonrpc::Error> for JsonRPCError {
fn from(err: lampo_common::jsonrpc::Error) -> Self {
match err {
lampo_common::jsonrpc::Error::Rpc(err) => Self {
code: err.code,
message: err.message,
data: err.data,
},
_ => Self {
code: -1,
message: format!("{err}"),
data: None,
},
}
}
}

/// This struct represents app state and it is pass on every
/// endpoint.
pub(crate) struct AppState {
Expand Down Expand Up @@ -131,7 +182,8 @@ macro_rules! post {
) -> ResultJson<$res_ty> {
let response = [<json_$name>](&state.lampod, &json::json!({})).await;
if let Err(err) = response {
return Err(actix_web::error::ErrorInternalServerError(err));
let err: crate::JsonRPCError = err.into();
return Err(err.into());
}
let response = json::from_value::<$res_ty>(response.unwrap());
let response = response.unwrap();
Expand All @@ -155,7 +207,8 @@ macro_rules! post {
let request = json::to_value(&request).unwrap();
let response = [<json_$name>](&state.lampod, &request).await;
if let Err(err) = response {
return Err(actix_web::error::ErrorInternalServerError(err));
let err: crate::JsonRPCError = err.into();
return Err(err.into());
}
let response = json::from_value::<$res_ty>(response.unwrap());
let response = response.unwrap();
Expand Down

0 comments on commit c7e3868

Please sign in to comment.