首页 > 解决方案 > Gitlab webhook 系统

问题描述

我有一个问题:

我的应用程序使用端口 1337(我在sails.js 框架中使用),我必须从 gitlab 编写一个 webhook 系统。我没有找到很多关于“如何编写webhook系统”的教程,但我有以下代码:

const secret = "your_secret_here";
const repo = "~/your_repo_path_here/";

const http = require('http');
const crypto = require('crypto');
const exec = require('child_process').exec;

http.createServer(function (req, res) {
    req.on('data', function(chunk) {
        let sig = "sha1=" + crypto.createHmac('sha1', secret).update(chunk.toString()).digest('hex');

    if (req.headers['x-hub-signature'] == sig) {
        exec('cd ' + repo + ' && git pull');
    }
});

    res.end();
}).listen(8080);

此代码使用端口 8080,我的问题是:如何使用此代码,但不在端口 8080 上而是在我的应用程序端口 1337 上使用?,如何编辑此代码以成为sails 的正确版本?

感谢所有答案

标签: node.jsgitlabsails.js

解决方案


您正在使用 Node 的http库创建一个新服务。它不适用于 1337 端口,因为该端口已被风帆占用。

您需要在风帆中制作自定义路线,有关如何在此处此处此处执行此操作的更多信息。这需要连接到一个包含处理 webhook 的实际代码的操作。


推荐阅读