|
| 1 | +use actix_web::{ |
| 2 | + HttpMessage, HttpResponse, error::ParseError, http::header, post, web, |
| 3 | +}; |
| 4 | +use serde::Deserialize; |
| 5 | +use tracing::trace; |
| 6 | + |
| 7 | +use crate::routes::ApiError; |
| 8 | +use crate::util::gotenberg::{ |
| 9 | + GeneratedPdfType, MODRINTH_GENERATED_PDF_TYPE, MODRINTH_PAYMENT_ID, |
| 10 | +}; |
| 11 | +use crate::util::guards::internal_network_guard; |
| 12 | + |
| 13 | +pub fn config(cfg: &mut actix_web::web::ServiceConfig) { |
| 14 | + cfg.service(success).service(error); |
| 15 | +} |
| 16 | + |
| 17 | +#[post("/gotenberg/success", guard = "internal_network_guard")] |
| 18 | +pub async fn success( |
| 19 | + web::Header(header::ContentDisposition { |
| 20 | + disposition, |
| 21 | + parameters: disposition_parameters, |
| 22 | + }): web::Header<header::ContentDisposition>, |
| 23 | + web::Header(GotenbergTrace(trace)): web::Header<GotenbergTrace>, |
| 24 | + web::Header(ModrinthGeneratedPdfType(r#type)): web::Header< |
| 25 | + ModrinthGeneratedPdfType, |
| 26 | + >, |
| 27 | + maybe_payment_id: Option<web::Header<ModrinthPaymentId>>, |
| 28 | + body: web::Bytes, |
| 29 | +) -> Result<HttpResponse, ApiError> { |
| 30 | + trace!( |
| 31 | + %trace, |
| 32 | + %disposition, |
| 33 | + ?disposition_parameters, |
| 34 | + r#type = r#type.as_str(), |
| 35 | + ?maybe_payment_id, |
| 36 | + body.len = body.len(), |
| 37 | + "Received Gotenberg generated PDF" |
| 38 | + ); |
| 39 | + |
| 40 | + Ok(HttpResponse::Ok().finish()) |
| 41 | +} |
| 42 | + |
| 43 | +#[allow(dead_code)] |
| 44 | +#[derive(Debug, Deserialize)] |
| 45 | +pub struct ErrorBody { |
| 46 | + status: Option<String>, |
| 47 | + message: Option<String>, |
| 48 | +} |
| 49 | + |
| 50 | +#[post("/gotenberg/error", guard = "internal_network_guard")] |
| 51 | +pub async fn error( |
| 52 | + web::Header(GotenbergTrace(trace)): web::Header<GotenbergTrace>, |
| 53 | + web::Header(ModrinthGeneratedPdfType(r#type)): web::Header< |
| 54 | + ModrinthGeneratedPdfType, |
| 55 | + >, |
| 56 | + maybe_payment_id: Option<web::Header<ModrinthPaymentId>>, |
| 57 | + web::Json(error_body): web::Json<ErrorBody>, |
| 58 | +) -> Result<HttpResponse, ApiError> { |
| 59 | + trace!( |
| 60 | + %trace, |
| 61 | + r#type = r#type.as_str(), |
| 62 | + ?maybe_payment_id, |
| 63 | + ?error_body, |
| 64 | + "Received Gotenberg error webhook" |
| 65 | + ); |
| 66 | + |
| 67 | + Ok(HttpResponse::Ok().finish()) |
| 68 | +} |
| 69 | + |
| 70 | +#[derive(Debug)] |
| 71 | +struct GotenbergTrace(String); |
| 72 | + |
| 73 | +impl header::TryIntoHeaderValue for GotenbergTrace { |
| 74 | + type Error = header::InvalidHeaderValue; |
| 75 | + |
| 76 | + fn try_into_value(self) -> Result<header::HeaderValue, Self::Error> { |
| 77 | + header::HeaderValue::from_str(&self.0) |
| 78 | + } |
| 79 | +} |
| 80 | + |
| 81 | +impl header::Header for GotenbergTrace { |
| 82 | + fn name() -> header::HeaderName { |
| 83 | + header::HeaderName::from_static("gotenberg-trace") |
| 84 | + } |
| 85 | + |
| 86 | + fn parse<M: HttpMessage>(m: &M) -> Result<Self, ParseError> { |
| 87 | + m.headers() |
| 88 | + .get(Self::name()) |
| 89 | + .ok_or(ParseError::Header)? |
| 90 | + .to_str() |
| 91 | + .map_err(|_| ParseError::Header) |
| 92 | + .map(ToOwned::to_owned) |
| 93 | + .map(GotenbergTrace) |
| 94 | + } |
| 95 | +} |
| 96 | + |
| 97 | +#[derive(Debug)] |
| 98 | +struct ModrinthGeneratedPdfType(GeneratedPdfType); |
| 99 | + |
| 100 | +impl header::TryIntoHeaderValue for ModrinthGeneratedPdfType { |
| 101 | + type Error = header::InvalidHeaderValue; |
| 102 | + |
| 103 | + fn try_into_value(self) -> Result<header::HeaderValue, Self::Error> { |
| 104 | + header::HeaderValue::from_str(self.0.as_str()) |
| 105 | + } |
| 106 | +} |
| 107 | + |
| 108 | +impl header::Header for ModrinthGeneratedPdfType { |
| 109 | + fn name() -> header::HeaderName { |
| 110 | + MODRINTH_GENERATED_PDF_TYPE |
| 111 | + } |
| 112 | + |
| 113 | + fn parse<M: HttpMessage>(m: &M) -> Result<Self, ParseError> { |
| 114 | + m.headers() |
| 115 | + .get(Self::name()) |
| 116 | + .ok_or(ParseError::Header)? |
| 117 | + .to_str() |
| 118 | + .map_err(|_| ParseError::Header)? |
| 119 | + .parse() |
| 120 | + .map_err(|_| ParseError::Header) |
| 121 | + .map(ModrinthGeneratedPdfType) |
| 122 | + } |
| 123 | +} |
| 124 | + |
| 125 | +#[derive(Debug)] |
| 126 | +struct ModrinthPaymentId(String); |
| 127 | + |
| 128 | +impl header::TryIntoHeaderValue for ModrinthPaymentId { |
| 129 | + type Error = header::InvalidHeaderValue; |
| 130 | + |
| 131 | + fn try_into_value(self) -> Result<header::HeaderValue, Self::Error> { |
| 132 | + header::HeaderValue::from_str(&self.0) |
| 133 | + } |
| 134 | +} |
| 135 | + |
| 136 | +impl header::Header for ModrinthPaymentId { |
| 137 | + fn name() -> header::HeaderName { |
| 138 | + MODRINTH_PAYMENT_ID |
| 139 | + } |
| 140 | + |
| 141 | + fn parse<M: HttpMessage>(m: &M) -> Result<Self, ParseError> { |
| 142 | + m.headers() |
| 143 | + .get(Self::name()) |
| 144 | + .ok_or(ParseError::Header)? |
| 145 | + .to_str() |
| 146 | + .map_err(|_| ParseError::Header) |
| 147 | + .map(ToOwned::to_owned) |
| 148 | + .map(ModrinthPaymentId) |
| 149 | + } |
| 150 | +} |
0 commit comments