Golang implementa webhook y ejecuta el script especificado

Golang implementa webhooks para activar de forma remota la ejecución de scripts especificados.

descargar

Dirección de GITHUB: https://github.com/adnanh/webhook

https://github.com/adnanh/webhook/releases descargue directamente la última versión correspondiente.

configuración de webhook

vim webhook.json
Elimine los comentarios al usar

[
  {
    
    
    "id": "4444", // 这里填写 http://server/hooks/{ID} 对应的是{ID} 值。也是唯一值。
    "execute-command": "./test4.sh", // 须要执行的shell文件。
    "command-working-directory": "./", // 执行的工作目录。
    "pass-arguments-to-command":  // 指定获取 post请求参数
    [
      {
    
    
        "source": "payload",  // 这里是固定,获取请求信息
        "name": "text.content"  // 这里text.content 是获取 json里面 text:content 里面对应的值。 在测试里面能更直观查看。
      }
    ],
    "trigger-rule":  // 指定数据格式才执行。
    {
    
    
	  "match":
      {
    
    
        "type": "value",
        "value": "lswzw",  // 这里做的是匹配固定值。
        "parameter":
        {
    
    
          "source": "payload",  // 获取请求信息
          "name": "msgtype"  // 获取请求里 json   msgtype:对应的参数
        }
      }
    }
  },
    // 下面是创建一个2222 这个接口, 请求后执行不同的指令。
  {
    
    
    "id": "2222",
    "execute-command": "./test2.sh",
    "command-working-directory": "./",
    "pass-arguments-to-command":
    [
      {
    
    
        "source": "payload",
        "name": "text.content"
      }
    ],
    "trigger-rule":
    {
    
    
	  "match":
      {
    
    
        "type": "value",
        "value": "lswzw",
        "parameter":
        {
    
    
          "source": "payload",
          "name": "msgtype"
        }
      }
    }
  }
]

Configuración de script a ejecutar

Aquí está el shell que se ejecutará. Puede escribir cualquier contenido del shell usted mismo. $ 1 es el primer parámetro que se pasa.
test2.sh

#!/bin/bash
echo $1 >> test

test4.sh

#!/bin/bash
echo $1 >> test

puesta en marcha

nohup ./webhook -port 9000 -hotreload -hooks webhook.json -verbose > webhook.log 2>&1 &

Nota: -hotreload recargará automáticamente el archivo de configuración después de modificar directamente el archivo json y guardarlo. No es necesario reiniciar el servicio.

Prueba de llamada

curl -H "Content-Type:application/json" -X POST --data '{
  "msgtype": "lswzw",
  "text":{
  "content":"this is lswzw 2222!"
  }
}' http://127.0.0.1:9000/hooks/2222


curl -H "Content-Type:application/json" -X POST --data '{
  "msgtype": "lswzw",
  "text":{
  "content":"this is lswzw 4444!"
  }
}' http://127.0.0.1:9000/hooks/4444

Después de la ejecución. Verificar el archivo de prueba generado traerá el valor pasado por el contenido.

Supongo que te gusta

Origin blog.csdn.net/lswzw/article/details/106917430
Recomendado
Clasificación