Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 9 additions & 9 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,15 @@ use hyper_util::rt::TokioIo;
use tokio::net::TcpListener;
use tracing::{debug, error, info};

use crate::state::AppState;

mod config;
mod dsn;
mod logging;
mod metrics;
mod request;
mod service;
mod state;

#[derive(Parser, Debug)]
struct Args {
Expand All @@ -36,7 +39,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
println!("configuration file: {0}", args.config);

// Parse the configuration file
let configdata = Arc::new(config::from_args(&args)?);
let configdata = config::from_args(&args)?;

// Initialize metrics and logging
metrics::init(metrics::MetricsConfig::from_config(&configdata));
Expand All @@ -46,27 +49,24 @@ async fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
info!("Listening on {addr}");
let listener = TcpListener::bind(addr).await?;

// Create a map of inbound -> outbound keys for simpler lookups.
let keymap = Arc::new(dsn::make_key_map(configdata.keys.clone()));
let state = Arc::new(AppState::from_config(configdata));

if configdata.verbose {
if state.config.verbose {
debug!("DSN configuration");
debug!("{}", dsn::format_key_map(&keymap));
debug!("{}", dsn::format_key_map(&state.keymap));
}

loop {
let (stream, _) = listener.accept().await?;
let io = TokioIo::new(stream);
let keymap_loop = keymap.clone();
let configdata_loop = configdata.clone();
let state_loop = state.clone();

tokio::task::spawn(async move {
if let Err(err) = http1::Builder::new()
.serve_connection(
io,
service_fn(move |req: Request<Incoming>| {
let config_loop = configdata_loop.clone();
service::handle_request(req, config_loop.clone(), keymap_loop.clone())
service::handle_request(req, state_loop.clone())
}),
)
.await
Expand Down
26 changes: 12 additions & 14 deletions src/request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ use hyper::{HeaderMap, Request, Uri};
use regex::Regex;
use serde_json::Value;
use std::io::prelude::*;
use std::sync::Arc;
use std::time::Instant;
use tracing::{debug, warn};

Expand All @@ -23,7 +22,7 @@ const INGEST_PATH_SEGMENTS: [&str; 3] = ["envelope", "store", "integration"];
/// to the outbound DSN. This function returns `RequestBuilder` because the body types
/// are tedious to deal with.
pub fn make_outbound_request(
config: &Arc<ConfigData>,
config: &ConfigData,
uri: &Uri,
headers: &HeaderMap,
outbound: &dsn::Dsn,
Expand Down Expand Up @@ -135,7 +134,7 @@ fn replace_public_key(target: &str, outbound: &dsn::Dsn) -> String {
}

pub async fn read_and_decode_body<B: Body>(
config: &Arc<ConfigData>,
config: &ConfigData,
request: Request<B>,
headers: &HeaderMap,
public_key: &str,
Expand Down Expand Up @@ -260,7 +259,7 @@ mod tests {

#[test]
fn make_outbound_request_remove_proxy_headers() {
let config = Arc::new(ConfigData::default());
let config = ConfigData::default();
let outbound: dsn::Dsn = "https://[email protected]/6789"
.parse()
.unwrap();
Expand Down Expand Up @@ -290,7 +289,7 @@ mod tests {

#[test]
fn make_outbound_request_replace_sentry_auth_header() {
let config = Arc::new(ConfigData::default());
let config = ConfigData::default();
let outbound: dsn::Dsn = "https://[email protected]/6789"
.parse()
.unwrap();
Expand All @@ -315,7 +314,7 @@ mod tests {

#[test]
fn make_outbound_request_replace_authorization_header() {
let config = Arc::new(ConfigData::default());
let config = ConfigData::default();
let outbound: dsn::Dsn = "https://[email protected]/6789"
.parse()
.unwrap();
Expand Down Expand Up @@ -346,7 +345,7 @@ mod tests {

#[test]
fn make_outbound_request_replace_query_key() {
let config = Arc::new(ConfigData::default());
let config = ConfigData::default();
let outbound: dsn::Dsn = "https://[email protected]/6789"
.parse()
.unwrap();
Expand All @@ -370,7 +369,7 @@ mod tests {

#[test]
fn make_outbound_request_replace_path_host_and_scheme() {
let config = Arc::new(ConfigData::default());
let config = ConfigData::default();
let outbound: dsn::Dsn = "https://[email protected]/6789"
.parse()
.unwrap();
Expand All @@ -397,7 +396,7 @@ mod tests {

#[test]
fn make_outbound_request_replace_project_id_oltp_url() {
let config = Arc::new(ConfigData::default());
let config = ConfigData::default();
let outbound: dsn::Dsn = "https://[email protected]/6789"
.parse()
.unwrap();
Expand All @@ -420,7 +419,7 @@ mod tests {

#[test]
fn make_outbound_request_content_encoding_header() {
let config = Arc::new(ConfigData::default());
let config = ConfigData::default();
let outbound: dsn::Dsn = "https://[email protected]/6789"
.parse()
.unwrap();
Expand All @@ -443,10 +442,10 @@ mod tests {
"should be absent when envelope_header modification is on"
);

let config = Arc::new(ConfigData {
let config = ConfigData {
modify_envelope_header: false,
..ConfigData::default()
});
};
let builder = make_outbound_request(&config, &uri, &headers, &outbound);
let res = builder.body("");

Expand Down Expand Up @@ -639,7 +638,7 @@ mod tests {

#[tokio::test]
async fn test_read_and_decode_body() {
let config = Arc::new(make_test_config());
let config = make_test_config();
assert!(config.modify_envelope_header, "Should default to true");

let contents = b"some content to be compressed";
Expand All @@ -666,7 +665,6 @@ mod tests {
async fn test_read_and_decode_body_decode_disabled() {
let mut config = make_test_config();
config.modify_envelope_header = false;
let config = Arc::new(config);

let contents = b"some content to be compressed";
let mut encoder = DeflateEncoder::new(&contents[..], Compression::fast());
Expand Down
Loading
Loading