Skip to content

Commit

Permalink
initial server
Browse files Browse the repository at this point in the history
  • Loading branch information
SG60 committed Dec 24, 2023
1 parent 36d5f14 commit f4cafbe
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 2 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/rust.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ jobs:
with: {name: "nix-community"}
- uses: DeterminateSystems/magic-nix-cache-action@main

- name: test
run: nix develop -c cargo test
- name: build
run: nix develop -c cross build --target ${{ matrix.target.rust }} --release

Expand Down
2 changes: 2 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
anyhow = "1.0.76"
axum = "0.7.2"
opentelemetry-tracing-utils = "0.1.0"
tokio = { version = "1.35.1", features = ["full"] }
tower = "0.4.13"
tracing = "0.1.40"
97 changes: 95 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,96 @@
fn main() {
println!("Hello, world!");
use anyhow::Result;
use axum::{
http::{HeaderMap, StatusCode},
routing::post,
Router,
};
use tracing::info;

#[tokio::main]
async fn main() -> Result<()> {
// initialise tracing
opentelemetry_tracing_utils::set_up_logging().expect("tracing setup should work");

// build our application with a single route
let app = app();

// run our app with hyper, listening globally on port 3000
let listener = tokio::net::TcpListener::bind("0.0.0.0:3000").await.unwrap();
axum::serve(listener, app).await.unwrap();

Ok(())
}
fn app() -> Router {
Router::new().route("/webhook", post(post_webhook_handler))
}

#[tracing::instrument(ret, err)]
async fn post_webhook_handler(
headers: HeaderMap,
body: String,
) -> Result<&'static str, StatusCode> {
let webhook_is_validated = true;

dbg!(&headers);

dbg!(body);

if !webhook_is_validated {
return Err(StatusCode::BAD_REQUEST);
};

info!("validated webhook");

match headers
.get("X-GitHub-Event")
.ok_or(StatusCode::BAD_REQUEST)?
.to_str()
.map_err(|_| StatusCode::BAD_REQUEST)?
{
"pull_request" | "push" => Ok("asdf"), // false => Err(StatusCode::BAD_REQUEST),
_ => Err(StatusCode::BAD_REQUEST),
}
}

#[cfg(test)]
mod tests {
use axum::{body::Body, http::Request};
use tower::ServiceExt;

use super::*;

#[tokio::test]
async fn pull_request_synchronised() {
let app = app();

// `Router` implements `tower::Service<Request<Body>>` so we can
// call it like any tower service, no need to run an HTTP server.
let response = app
.oneshot(
Request::builder()
.uri("/webhook")
.method("POST")
.header("X-GitHub-Event", "pull_request")
.body(Body::from("{a: 5, e: 20}"))
.unwrap(),
)
.await
.unwrap();

assert_eq!(response.status(), StatusCode::OK);
let (parts, body) = response.into_parts();

let body_string: String = String::from_utf8(
axum::body::to_bytes(body, usize::MAX)
.await
.unwrap()
.to_vec(),
)
.unwrap();

dbg!(parts);
dbg!(&body_string);

assert!(body_string.contains("forwarded"));
}
}

0 comments on commit f4cafbe

Please sign in to comment.