Skip to content

Instantly share code, notes, and snippets.

@boxdot
Created March 12, 2020 20:29
Show Gist options
  • Save boxdot/7b641f908c4974d4402a8e97cab111a8 to your computer and use it in GitHub Desktop.
Save boxdot/7b641f908c4974d4402a8e97cab111a8 to your computer and use it in GitHub Desktop.
use gitlab::webhooks::WebHook;
use hyper::service::{make_service_fn, service_fn};
use hyper::{Body, Request, Response, Server};
use log::{log_enabled, Level};
async fn gitlab_webhook(req: Request<Body>) -> Result<Response<Body>, hyper::Error> {
log::debug!("< {:?}", req);
let body = hyper::body::to_bytes(req.into_body()).await?;
let event: Result<WebHook, _> = serde_json::from_slice(&body);
match event {
Ok(event) => println!("{:?}", event),
Err(e) => {
log::error!("failed to deserialize event: {}", e);
if log_enabled!(Level::Debug) {
if let Ok(body) = std::str::from_utf8(&body) {
log::debug!("body: {}", body);
}
}
}
}
Ok(Response::new(Body::empty()))
}
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
env_logger::from_env(env_logger::Env::default().default_filter_or("debug")).init();
let addr = ([0, 0, 0, 0], 8081).into();
let service = make_service_fn(|_| async { Ok::<_, hyper::Error>(service_fn(gitlab_webhook)) });
let server = Server::bind(&addr).serve(service);
println!("Listening on http://{}", addr);
server.await?;
Ok(())
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment