Reorder code
To improve readibility and to prepeare split of `webhookey` code from `Rocket` some functions are reordered.
This commit is contained in:
parent
4594db6c44
commit
beb039aa3c
1 changed files with 67 additions and 67 deletions
134
src/main.rs
134
src/main.rs
|
@ -305,39 +305,6 @@ impl Hooks {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[rocket::async_trait]
|
|
||||||
impl<'r> FromData<'r> for Hooks {
|
|
||||||
type Error = WebhookeyError;
|
|
||||||
|
|
||||||
async fn from_data(
|
|
||||||
request: &'r Request<'_>,
|
|
||||||
data: Data<'r>,
|
|
||||||
) -> Outcome<Self, (Status, Self::Error), Data<'r>> {
|
|
||||||
match Hooks::get_commands(request, data).await {
|
|
||||||
Ok(hooks) => {
|
|
||||||
if hooks.inner.is_empty() {
|
|
||||||
let client_ip = &request
|
|
||||||
.client_ip()
|
|
||||||
.unwrap_or(IpAddr::V4(Ipv4Addr::UNSPECIFIED));
|
|
||||||
|
|
||||||
warn!("Unmatched hook from {}", &client_ip);
|
|
||||||
return Failure((Status::NotFound, WebhookeyError::UnmatchedHook(*client_ip)));
|
|
||||||
}
|
|
||||||
|
|
||||||
Success(hooks)
|
|
||||||
}
|
|
||||||
Err(WebhookeyError::Unauthorized(e)) => {
|
|
||||||
error!("{}", WebhookeyError::Unauthorized(e));
|
|
||||||
Failure((Status::Unauthorized, WebhookeyError::Unauthorized(e)))
|
|
||||||
}
|
|
||||||
Err(e) => {
|
|
||||||
error!("{}", e);
|
|
||||||
Failure((Status::BadRequest, e))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn accept_ip(hook_name: &str, client_ip: &IpAddr, ip: &IpFilter) -> bool {
|
fn accept_ip(hook_name: &str, client_ip: &IpAddr, ip: &IpFilter) -> bool {
|
||||||
if ip.validate(client_ip) {
|
if ip.validate(client_ip) {
|
||||||
info!("Allow hook `{}` from {}", &hook_name, &client_ip);
|
info!("Allow hook `{}` from {}", &hook_name, &client_ip);
|
||||||
|
@ -376,6 +343,18 @@ fn get_value_from_pointer<'a>(data: &'a serde_json::Value, pointer: &'a str) ->
|
||||||
.ok_or_else(|| anyhow!("Could not convert value `{}` to string", value))
|
.ok_or_else(|| anyhow!("Could not convert value `{}` to string", value))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn get_string(value: &serde_json::Value) -> Result<String, WebhookeyError> {
|
||||||
|
match &value {
|
||||||
|
serde_json::Value::Bool(bool) => Ok(bool.to_string()),
|
||||||
|
serde_json::Value::Number(number) => Ok(number.to_string()),
|
||||||
|
serde_json::Value::String(string) => Ok(string.as_str().to_string()),
|
||||||
|
x => {
|
||||||
|
error!("Could not get string from: {:?}", x);
|
||||||
|
unimplemented!()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
fn replace_parameters(
|
fn replace_parameters(
|
||||||
input: &str,
|
input: &str,
|
||||||
headers: &HeaderMap,
|
headers: &HeaderMap,
|
||||||
|
@ -405,40 +384,6 @@ fn replace_parameters(
|
||||||
Ok(result.join(""))
|
Ok(result.join(""))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn get_string(value: &serde_json::Value) -> Result<String, WebhookeyError> {
|
|
||||||
match &value {
|
|
||||||
serde_json::Value::Bool(bool) => Ok(bool.to_string()),
|
|
||||||
serde_json::Value::Number(number) => Ok(number.to_string()),
|
|
||||||
serde_json::Value::String(string) => Ok(string.as_str().to_string()),
|
|
||||||
x => {
|
|
||||||
error!("Could not get string from: {:?}", x);
|
|
||||||
unimplemented!()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[post("/", format = "json", data = "<hooks>")]
|
|
||||||
async fn receive_hook<'a>(address: SocketAddr, hooks: Hooks) -> Status {
|
|
||||||
info!("Post request received from: {}", address);
|
|
||||||
|
|
||||||
hooks.inner.iter().for_each(|(name, command)| {
|
|
||||||
info!("Execute `{}` from hook `{}`", &command, &name);
|
|
||||||
|
|
||||||
match run_script::run(command, &vec![], &ScriptOptions::new()) {
|
|
||||||
Ok((status, stdout, stderr)) => {
|
|
||||||
info!("Command `{}` exited with return code: {}", &command, status);
|
|
||||||
trace!("Output of command `{}` on stdout: {:?}", &command, &stdout);
|
|
||||||
debug!("Output of command `{}` on stderr: {:?}", &command, &stderr);
|
|
||||||
}
|
|
||||||
Err(e) => {
|
|
||||||
error!("Execution of `{}` failed: {}", &command, e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
Status::Ok
|
|
||||||
}
|
|
||||||
|
|
||||||
fn get_config() -> Result<File> {
|
fn get_config() -> Result<File> {
|
||||||
// Look for config in CWD..
|
// Look for config in CWD..
|
||||||
if let Ok(config) = File::open("config.yml") {
|
if let Ok(config) = File::open("config.yml") {
|
||||||
|
@ -472,6 +417,61 @@ fn get_config() -> Result<File> {
|
||||||
bail!("No configuration file found.");
|
bail!("No configuration file found.");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[rocket::async_trait]
|
||||||
|
impl<'r> FromData<'r> for Hooks {
|
||||||
|
type Error = WebhookeyError;
|
||||||
|
|
||||||
|
async fn from_data(
|
||||||
|
request: &'r Request<'_>,
|
||||||
|
data: Data<'r>,
|
||||||
|
) -> Outcome<Self, (Status, Self::Error), Data<'r>> {
|
||||||
|
match Hooks::get_commands(request, data).await {
|
||||||
|
Ok(hooks) => {
|
||||||
|
if hooks.inner.is_empty() {
|
||||||
|
let client_ip = &request
|
||||||
|
.client_ip()
|
||||||
|
.unwrap_or(IpAddr::V4(Ipv4Addr::UNSPECIFIED));
|
||||||
|
|
||||||
|
warn!("Unmatched hook from {}", &client_ip);
|
||||||
|
return Failure((Status::NotFound, WebhookeyError::UnmatchedHook(*client_ip)));
|
||||||
|
}
|
||||||
|
|
||||||
|
Success(hooks)
|
||||||
|
}
|
||||||
|
Err(WebhookeyError::Unauthorized(e)) => {
|
||||||
|
error!("{}", WebhookeyError::Unauthorized(e));
|
||||||
|
Failure((Status::Unauthorized, WebhookeyError::Unauthorized(e)))
|
||||||
|
}
|
||||||
|
Err(e) => {
|
||||||
|
error!("{}", e);
|
||||||
|
Failure((Status::BadRequest, e))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[post("/", format = "json", data = "<hooks>")]
|
||||||
|
async fn receive_hook<'a>(address: SocketAddr, hooks: Hooks) -> Status {
|
||||||
|
info!("Post request received from: {}", address);
|
||||||
|
|
||||||
|
hooks.inner.iter().for_each(|(name, command)| {
|
||||||
|
info!("Execute `{}` from hook `{}`", &command, &name);
|
||||||
|
|
||||||
|
match run_script::run(command, &vec![], &ScriptOptions::new()) {
|
||||||
|
Ok((status, stdout, stderr)) => {
|
||||||
|
info!("Command `{}` exited with return code: {}", &command, status);
|
||||||
|
trace!("Output of command `{}` on stdout: {:?}", &command, &stdout);
|
||||||
|
debug!("Output of command `{}` on stderr: {:?}", &command, &stderr);
|
||||||
|
}
|
||||||
|
Err(e) => {
|
||||||
|
error!("Execution of `{}` failed: {}", &command, e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
Status::Ok
|
||||||
|
}
|
||||||
|
|
||||||
#[rocket::main]
|
#[rocket::main]
|
||||||
async fn main() -> Result<()> {
|
async fn main() -> Result<()> {
|
||||||
env_logger::init();
|
env_logger::init();
|
||||||
|
|
Loading…
Reference in a new issue