diff --git a/src/main.rs b/src/main.rs index 5d29529..ad950a2 100644 --- a/src/main.rs +++ b/src/main.rs @@ -16,73 +16,25 @@ #![feature(let_chains)] mod error; +mod web; -use crate::error::Error; +use crate::web::webhooks; use std::convert::Infallible; use std::net::SocketAddr; -use std::str::from_utf8; -use gitlab::webhooks::WebHook; use hyper::{ - body, Body, header, Method, Request, Response, Server, service::{make_service_fn, service_fn}, + Server, }; -use log::{debug, error}; - -fn error_res(e: E) -> Result, Infallible> { - error!("error response: {:?}", e); - - let text = format!("{:?}", e); - let res = Response::builder() - .status(400) - .body(Body::from(Vec::from(text.as_bytes()))) - .unwrap(); - Ok(res) -} - -async fn webhooks(req: Request) -> Result, Error> { - match req.method() { - &Method::POST => (), - _ => return Err(Error::MethodMismatch), - } - - debug!("Headers: {:?}", req.headers()); - - let headers = req.headers(); - if let Some(content_type) = headers.get(header::CONTENT_TYPE) && - let Some(token) = headers.get("X-Gitlab-Token") { - if content_type != "application/json" { - return Err(Error::InvalidContentType); - } - - if token != "secret" { - return Err(Error::InvalidToken); - } - } - - let tmp = body::to_bytes(req.into_body()).await?; - let text: &str = from_utf8(&tmp)?; - let json: WebHook = serde_json::from_str(text)?; - debug!("Passed: {:?}", json); - - Ok(Response::new("Hello world".into())) -} - -async fn wrapper(req: Request) -> Result, Infallible> { - webhooks(req).await.or_else(error_res) -} #[tokio::main] async fn main() { pretty_env_logger::init(); let addr = SocketAddr::from(([127, 0, 0, 1], 3000)); - let make_svc = make_service_fn(|_conn| async { - Ok::<_, Infallible>(service_fn(wrapper)) - }); + let make_svc = make_service_fn(|_conn| async { Ok::<_, Infallible>(service_fn(webhooks)) }); let server = Server::bind(&addr).serve(make_svc); - println!("Listening on http://{}", addr); if let Err(e) = server.await { diff --git a/src/web.rs b/src/web.rs new file mode 100644 index 0000000..f508fe9 --- /dev/null +++ b/src/web.rs @@ -0,0 +1,66 @@ +// Copyright (C) 2023-2099 The crate authors. +// +// This program is free software: you can redistribute it and/or modify it +// under the terms of the GNU Affero General Public License as published by the +// Free Software Foundation, either version 3 of the License, or (at your +// option) any later version. +// +// This program is distributed in the hope that it will be useful, but WITHOUT +// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or +// FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License +// for more details. +// +// You should have received a copy of the GNU Affero General Public License +// along with this program. If not, see . + +use crate::error::Error; + +use std::convert::Infallible; +use std::str::from_utf8; + +use gitlab::webhooks::WebHook; +use hyper::{body, header, Body, Method, Request, Response}; +use log::{debug, error}; + +fn error_res(e: E) -> Result, Infallible> { + error!("error response: {:?}", e); + + let text = format!("{:?}", e); + let res = Response::builder() + .status(400) + .body(Body::from(Vec::from(text.as_bytes()))) + .unwrap(); + Ok(res) +} + +async fn webhooks_inner(req: Request) -> Result, Error> { + match req.method() { + &Method::POST => (), + _ => return Err(Error::MethodMismatch), + } + + debug!("Headers: {:?}", req.headers()); + + let headers = req.headers(); + if let Some(content_type) = headers.get(header::CONTENT_TYPE) && + let Some(token) = headers.get("X-Gitlab-Token") { + if content_type != "application/json" { + return Err(Error::InvalidContentType); + } + + if token != "secret" { + return Err(Error::InvalidToken); + } + } + + let tmp = body::to_bytes(req.into_body()).await?; + let text: &str = from_utf8(&tmp)?; + let json: WebHook = serde_json::from_str(text)?; + debug!("Passed: {:?}", json); + + Ok(Response::new("Hello world".into())) +} + +pub async fn webhooks(req: Request) -> Result, Infallible> { + webhooks_inner(req).await.or_else(error_res) +}