summaryrefslogtreecommitdiff
path: root/webhook/src
diff options
context:
space:
mode:
Diffstat (limited to 'webhook/src')
-rw-r--r--webhook/src/config.rs19
-rw-r--r--webhook/src/handle.rs7
-rw-r--r--webhook/src/main.rs19
-rw-r--r--webhook/src/utils.rs48
4 files changed, 30 insertions, 63 deletions
diff --git a/webhook/src/config.rs b/webhook/src/config.rs
new file mode 100644
index 0000000..eead97b
--- /dev/null
+++ b/webhook/src/config.rs
@@ -0,0 +1,19 @@
+use serde::Deserialize;
+
+#[derive(Debug, Deserialize, Clone, Default)]
+pub struct ServerSettings {
+ pub port: u16,
+ pub address: String,
+}
+
+#[derive(Debug, Deserialize, Clone, Default)]
+pub struct Discord {
+ pub public_key: String,
+ pub client_id: u32,
+}
+
+#[derive(Debug, Deserialize, Clone, Default)]
+pub struct Config {
+ pub server: ServerSettings,
+ pub discord: Discord,
+} \ No newline at end of file
diff --git a/webhook/src/handle.rs b/webhook/src/handle.rs
index d08ee1f..b60889e 100644
--- a/webhook/src/handle.rs
+++ b/webhook/src/handle.rs
@@ -10,8 +10,7 @@ use std::pin::Pin;
use std::str::from_utf8;
use std::task::{Context, Poll};
use serde::{Deserialize, Serialize};
-
-use super::utils::Settings;
+use crate::config::Config;
pub fn validate_signature(b64_public_key: &str, data: &Vec<u8>, b64_signature: &str) -> bool {
// First, we need to check if the signature & private key is valid base64.
@@ -56,7 +55,7 @@ fn get_signature(headers: &HeaderMap) -> Option<(&str, &str)> {
}
pub struct HandlerService {
- pub config: Settings,
+ pub config: Config,
}
#[derive(Debug, Serialize, Deserialize)]
@@ -116,7 +115,7 @@ impl Service<Request<Body>> for HandlerService {
}
pub struct MakeSvc {
- pub settings: Settings,
+ pub settings: Config,
}
impl<T> Service<T> for MakeSvc {
diff --git a/webhook/src/main.rs b/webhook/src/main.rs
index 2daaa4d..3125373 100644
--- a/webhook/src/main.rs
+++ b/webhook/src/main.rs
@@ -1,21 +1,18 @@
use std::net::ToSocketAddrs;
+mod handle;
+mod config;
use hyper::Server;
use log::info;
-
-extern crate log;
-pub mod handle;
-pub mod utils;
-
use handle::MakeSvc;
-use utils::{setup_program, Settings};
+use common::config::Settings;
+use crate::config::Config;
#[tokio::main]
async fn main() {
- setup_program("webhook");
- let config = Settings::new().unwrap();
+ let settings: Settings<Config> = Settings::new("webhook").unwrap();
- let addr = format!("{}:{}", config.server.address, config.server.port)
+ let addr = format!("{}:{}", settings.config.server.address, settings.config.server.port)
.to_socket_addrs()
.unwrap()
.next()
@@ -23,10 +20,10 @@ async fn main() {
info!(
"Starting server on {}:{}",
- config.server.address, config.server.port
+ settings.config.server.address, settings.config.server.port
);
let server = Server::bind(&addr).serve(MakeSvc {
- settings: config.clone(),
+ settings: settings.config.clone(),
});
if let Err(e) = server.await {
diff --git a/webhook/src/utils.rs b/webhook/src/utils.rs
deleted file mode 100644
index 442b5ba..0000000
--- a/webhook/src/utils.rs
+++ /dev/null
@@ -1,48 +0,0 @@
-use std::env;
-
-use config::{Config, ConfigError, Environment, File};
-use log::info;
-use serde::Deserialize;
-
-/// Executes the required configuration steps for the program,
-/// excluding build information, Sentry and logging.
-pub fn setup_program(_name: &str) {
- pretty_env_logger::init();
-}
-
-#[derive(Debug, Deserialize, Clone)]
-pub struct Server {
- pub port: u16,
- pub address: String,
-}
-
-#[derive(Debug, Deserialize, Clone)]
-pub struct Discord {
- pub public_key: String,
- pub client_id: u32,
-}
-
-#[derive(Debug, Deserialize, Clone)]
-pub struct Settings {
- pub server: Server,
- pub discord: Discord,
-}
-
-impl Settings {
- pub fn new() -> Result<Self, ConfigError> {
- let mut default = Config::default();
- default.merge(File::with_name("config/default"))?;
- let mode = env::var("ENV").unwrap_or_else(|_| "development".into());
- info!("Configuration Environment: {}", mode);
-
- default.merge(File::with_name(&format!("config/{}", mode)).required(false))?;
- default.merge(File::with_name("config/local").required(false))?;
- default.merge(Environment::with_prefix("NOVA_GATEWAY"))?;
-
- println!("Debug mode: {:?}", default.get_bool("debug"));
-
- let config: Self = default.try_into().unwrap();
-
- Ok(config)
- }
-}