rustfmt: hard_tabs = true
Signed-off-by: Maxime “pep” Buquet <pep@bouah.net>
This commit is contained in:
parent
5b57951ec9
commit
36673bd5bf
4 changed files with 56 additions and 55 deletions
1
rustfmt.toml
Normal file
1
rustfmt.toml
Normal file
|
@ -0,0 +1 @@
|
|||
hard_tabs = true
|
50
src/error.rs
50
src/error.rs
|
@ -18,43 +18,43 @@ use std::str::Utf8Error;
|
|||
|
||||
#[derive(Debug)]
|
||||
pub(crate) enum Error {
|
||||
MethodMismatch,
|
||||
InvalidToken,
|
||||
InvalidContentType,
|
||||
Hyper(hyper::Error),
|
||||
SerdeJson(serde_json::Error),
|
||||
Utf8(Utf8Error),
|
||||
MethodMismatch,
|
||||
InvalidToken,
|
||||
InvalidContentType,
|
||||
Hyper(hyper::Error),
|
||||
SerdeJson(serde_json::Error),
|
||||
Utf8(Utf8Error),
|
||||
}
|
||||
|
||||
impl StdError for Error {}
|
||||
|
||||
impl std::fmt::Display for Error {
|
||||
fn fmt(&self, fmt: &mut std::fmt::Formatter) -> std::fmt::Result {
|
||||
match self {
|
||||
Error::MethodMismatch => write!(fmt, "the method is invalid"),
|
||||
Error::InvalidToken => write!(fmt, "the token is invalid"),
|
||||
Error::InvalidContentType => write!(fmt, "the content-type is invalid"),
|
||||
Error::Hyper(e) => write!(fmt, "hyper error: {}", e),
|
||||
Error::SerdeJson(e) => write!(fmt, "serde_json error: {}", e),
|
||||
Error::Utf8(e) => write!(fmt, "Utf8 error: {}", e),
|
||||
}
|
||||
}
|
||||
fn fmt(&self, fmt: &mut std::fmt::Formatter) -> std::fmt::Result {
|
||||
match self {
|
||||
Error::MethodMismatch => write!(fmt, "the method is invalid"),
|
||||
Error::InvalidToken => write!(fmt, "the token is invalid"),
|
||||
Error::InvalidContentType => write!(fmt, "the content-type is invalid"),
|
||||
Error::Hyper(e) => write!(fmt, "hyper error: {}", e),
|
||||
Error::SerdeJson(e) => write!(fmt, "serde_json error: {}", e),
|
||||
Error::Utf8(e) => write!(fmt, "Utf8 error: {}", e),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<hyper::Error> for Error {
|
||||
fn from(err: hyper::Error) -> Error {
|
||||
Error::Hyper(err)
|
||||
}
|
||||
fn from(err: hyper::Error) -> Error {
|
||||
Error::Hyper(err)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<serde_json::Error> for Error {
|
||||
fn from(err: serde_json::Error) -> Error {
|
||||
Error::SerdeJson(err)
|
||||
}
|
||||
fn from(err: serde_json::Error) -> Error {
|
||||
Error::SerdeJson(err)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<Utf8Error> for Error {
|
||||
fn from(err: Utf8Error) -> Error {
|
||||
Error::Utf8(err)
|
||||
}
|
||||
fn from(err: Utf8Error) -> Error {
|
||||
Error::Utf8(err)
|
||||
}
|
||||
}
|
||||
|
|
20
src/main.rs
20
src/main.rs
|
@ -24,20 +24,20 @@ use std::convert::Infallible;
|
|||
use std::net::SocketAddr;
|
||||
|
||||
use hyper::{
|
||||
service::{make_service_fn, service_fn},
|
||||
Server,
|
||||
service::{make_service_fn, service_fn},
|
||||
Server,
|
||||
};
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() {
|
||||
pretty_env_logger::init();
|
||||
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(webhooks)) });
|
||||
let server = Server::bind(&addr).serve(make_svc);
|
||||
println!("Listening on http://{}", addr);
|
||||
let addr = SocketAddr::from(([127, 0, 0, 1], 3000));
|
||||
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 {
|
||||
eprintln!("Server error: {e}");
|
||||
}
|
||||
if let Err(e) = server.await {
|
||||
eprintln!("Server error: {e}");
|
||||
}
|
||||
}
|
||||
|
|
40
src/web.rs
40
src/web.rs
|
@ -23,26 +23,26 @@ use hyper::{body, header, Body, Method, Request, Response};
|
|||
use log::{debug, error};
|
||||
|
||||
fn error_res<E: std::fmt::Debug>(e: E) -> Result<Response<Body>, Infallible> {
|
||||
error!("error response: {:?}", e);
|
||||
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)
|
||||
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<Body>) -> Result<Response<Body>, Error> {
|
||||
match req.method() {
|
||||
&Method::POST => (),
|
||||
_ => return Err(Error::MethodMismatch),
|
||||
}
|
||||
match req.method() {
|
||||
&Method::POST => (),
|
||||
_ => return Err(Error::MethodMismatch),
|
||||
}
|
||||
|
||||
debug!("Headers: {:?}", req.headers());
|
||||
debug!("Headers: {:?}", req.headers());
|
||||
|
||||
let headers = req.headers();
|
||||
if let Some(content_type) = headers.get(header::CONTENT_TYPE) &&
|
||||
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);
|
||||
|
@ -53,14 +53,14 @@ async fn webhooks_inner(req: Request<Body>) -> Result<Response<Body>, Error> {
|
|||
}
|
||||
}
|
||||
|
||||
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);
|
||||
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()))
|
||||
Ok(Response::new("Hello world".into()))
|
||||
}
|
||||
|
||||
pub async fn webhooks(req: Request<Body>) -> Result<Response<Body>, Infallible> {
|
||||
webhooks_inner(req).await.or_else(error_res)
|
||||
webhooks_inner(req).await.or_else(error_res)
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue