summaryrefslogtreecommitdiff
path: root/webhook/src/handler/handler.rs
diff options
context:
space:
mode:
authorMatthieu <matthieu@developershouse.xyz>2021-10-08 14:48:39 +0400
committerMatthieu <matthieu@developershouse.xyz>2021-10-08 14:48:39 +0400
commita02b25f235ba6eff33c6fd965c97071d5f112b6d (patch)
tree2fc18dbde0f1b2403cb83cf1adb6aa5e66cef7b2 /webhook/src/handler/handler.rs
parent308df902d6ff8656cea13e61e5277890d5ad4f08 (diff)
changes in the proto names, and new spec for nats
Diffstat (limited to 'webhook/src/handler/handler.rs')
-rw-r--r--webhook/src/handler/handler.rs70
1 files changed, 41 insertions, 29 deletions
diff --git a/webhook/src/handler/handler.rs b/webhook/src/handler/handler.rs
index b993aaa..9545bec 100644
--- a/webhook/src/handler/handler.rs
+++ b/webhook/src/handler/handler.rs
@@ -1,15 +1,26 @@
use super::{signature::validate_signature, types::Interaction};
use crate::config::Config;
-use hyper::{Body, Method, Request, Response, StatusCode, body::{to_bytes, Bytes}, service::Service};
+use hyper::{
+ body::{to_bytes, Bytes},
+ service::Service,
+ Body, Method, Request, Response, StatusCode,
+};
use log::{error, info, trace};
use nats::Connection;
use serde::{Deserialize, Serialize};
-use std::{future::Future, io::{Error, ErrorKind}, pin::Pin, str::from_utf8, sync::Arc, task::{Context, Poll}};
+use std::{
+ future::Future,
+ io::{Error, ErrorKind},
+ pin::Pin,
+ str::from_utf8,
+ sync::Arc,
+ task::{Context, Poll},
+};
/// Hyper service used to handle the discord webhooks
#[derive(Clone)]
pub struct HandlerService {
- pub config: Config,
+ pub config: Arc<Config>,
pub nats: Arc<Connection>,
}
@@ -56,7 +67,7 @@ impl HandlerService {
#[derive(Debug, Serialize, Deserialize)]
pub struct Ping {
#[serde(rename = "type")]
- t: i32
+ t: i32,
}
/// Implementation of the service
@@ -75,17 +86,19 @@ impl Service<Request<Body>> for HandlerService {
Box::pin(async move {
match self_clone.check_request(req).await {
Ok(data) => {
- let value: Interaction = serde_json::from_str(from_utf8(&data).unwrap()).unwrap();
+ let value: Interaction =
+ serde_json::from_str(from_utf8(&data).unwrap()).unwrap();
trace!("received value: {:?}", value);
match value.t {
1 => {
info!("sending pong");
// a ping must be responded with another ping
- return Ok(Response::builder().header("Content-Type", "application/json").body(serde_json::to_string(&Ping {
- t: 1
- }).unwrap().into()).unwrap());
- },
+ return Ok(Response::builder()
+ .header("Content-Type", "application/json")
+ .body(serde_json::to_string(&Ping { t: 1 }).unwrap().into())
+ .unwrap());
+ }
_ => {
let payload = serde_json::to_string(&common::payloads::CachePayload {
tracing: common::payloads::Tracing {
@@ -93,33 +106,32 @@ impl Service<Request<Body>> for HandlerService {
span: None,
},
data: value,
- }).unwrap();
+ })
+ .unwrap();
- match self_clone.nats.request("nova.cache.dispatch.interaction", payload) {
- Ok(response) => {
- Ok(
- Response::builder()
- .header("Content-Type", "application/json")
- .body(from_utf8(&response.data).unwrap().to_string().into())
- .unwrap()
- )
- },
+ match self_clone
+ .nats
+ .request("nova.cache.dispatch.interaction", payload)
+ {
+ Ok(response) => Ok(Response::builder()
+ .header("Content-Type", "application/json")
+ .body(from_utf8(&response.data).unwrap().to_string().into())
+ .unwrap()),
Err(error) => {
error!("failed to request nats: {}", error);
- Ok(
- Response::builder()
- .status(500)
- .body("an internal server error occured".to_string().into())
- .unwrap()
- )
+ Ok(Response::builder()
+ .status(500)
+ .body("an internal server error occured".to_string().into())
+ .unwrap())
}
}
- },
+ }
}
- },
- Err(error) => {
- Ok(Response::builder().status(StatusCode::UNAUTHORIZED).body(error.to_string().into()).unwrap())
}
+ Err(error) => Ok(Response::builder()
+ .status(StatusCode::UNAUTHORIZED)
+ .body(error.to_string().into())
+ .unwrap()),
}
})
}