summaryrefslogtreecommitdiff
path: root/exes/webhook/src/handler/handler.rs
diff options
context:
space:
mode:
Diffstat (limited to 'exes/webhook/src/handler/handler.rs')
-rw-r--r--exes/webhook/src/handler/handler.rs52
1 files changed, 24 insertions, 28 deletions
diff --git a/exes/webhook/src/handler/handler.rs b/exes/webhook/src/handler/handler.rs
index b2ef44c..af79185 100644
--- a/exes/webhook/src/handler/handler.rs
+++ b/exes/webhook/src/handler/handler.rs
@@ -1,13 +1,12 @@
use super::error::WebhookError;
use super::signature::validate_signature;
-use crate::config::Config;
+use crate::config::WebhookConfig;
use ed25519_dalek::PublicKey;
use hyper::{
body::{to_bytes, Bytes},
service::Service,
Body, Method, Request, Response, StatusCode,
};
-use serde::{Deserialize, Serialize};
use shared::nats_crate::Client;
use shared::{
log::{debug, error},
@@ -17,10 +16,9 @@ use std::{
future::Future,
pin::Pin,
str::from_utf8,
- sync::Arc,
task::{Context, Poll},
};
-use twilight_model::gateway::event::{DispatchEvent};
+use twilight_model::gateway::event::DispatchEvent;
use twilight_model::{
application::interaction::{Interaction, InteractionType},
gateway::payload::incoming::InteractionCreate,
@@ -28,14 +26,13 @@ use twilight_model::{
/// Hyper service used to handle the discord webhooks
#[derive(Clone)]
-pub struct HandlerService {
- pub config: Arc<Config>,
- pub nats: Arc<Client>,
- pub public_key: Arc<PublicKey>,
+pub struct WebhookService {
+ pub config: WebhookConfig,
+ pub nats: Client,
}
-impl HandlerService {
- async fn check_request(&self, req: Request<Body>) -> Result<Bytes, WebhookError> {
+impl WebhookService {
+ async fn check_request(req: Request<Body>, pk: PublicKey) -> Result<Bytes, WebhookError> {
if req.method() == Method::POST {
let signature = if let Some(sig) = req.headers().get("X-Signature-Ed25519") {
sig.to_owned()
@@ -57,7 +54,7 @@ impl HandlerService {
let data = to_bytes(req.into_body()).await?;
if validate_signature(
- &self.public_key,
+ &pk,
&[timestamp.as_bytes().to_vec(), data.to_vec()].concat(),
signature.to_str()?,
) {
@@ -74,10 +71,11 @@ impl HandlerService {
}
async fn process_request(
- &mut self,
req: Request<Body>,
+ nats: Client,
+ pk: PublicKey,
) -> Result<Response<Body>, WebhookError> {
- match self.check_request(req).await {
+ match Self::check_request(req, pk).await {
Ok(data) => {
let utf8 = from_utf8(&data);
match utf8 {
@@ -86,7 +84,7 @@ impl HandlerService {
match value.kind {
InteractionType::Ping => Ok(Response::builder()
.header("Content-Type", "application/json")
- .body(serde_json::to_string(&Ping { t: 1 }).unwrap().into())
+ .body(r#"{"t":1}"#.into())
.unwrap()),
_ => {
debug!("calling nats");
@@ -106,10 +104,13 @@ impl HandlerService {
let payload = serde_json::to_string(&data).unwrap();
- match self.nats.request(
- "nova.cache.dispatch.INTERACTION_CREATE".to_string(),
- Bytes::from(payload),
- ).await {
+ match nats
+ .request(
+ "nova.cache.dispatch.INTERACTION_CREATE".to_string(),
+ Bytes::from(payload),
+ )
+ .await
+ {
Ok(response) => Ok(Response::builder()
.header("Content-Type", "application/json")
.body(Body::from(response.reply.unwrap()))
@@ -144,15 +145,9 @@ impl HandlerService {
}
}
-#[derive(Debug, Serialize, Deserialize)]
-pub struct Ping {
- #[serde(rename = "type")]
- t: i32,
-}
-
/// Implementation of the service
-impl Service<Request<Body>> for HandlerService {
- type Response = Response<Body>;
+impl Service<hyper::Request<Body>> for WebhookService {
+ type Response = hyper::Response<Body>;
type Error = hyper::Error;
type Future = Pin<Box<dyn Future<Output = Result<Self::Response, Self::Error>> + Send>>;
@@ -161,9 +156,10 @@ impl Service<Request<Body>> for HandlerService {
}
fn call(&mut self, req: Request<Body>) -> Self::Future {
- let mut clone = self.clone();
+ let future =
+ Self::process_request(req, self.nats.clone(), self.config.discord.public_key);
Box::pin(async move {
- let response = clone.process_request(req).await;
+ let response = future.await;
match response {
Ok(r) => Ok(r),