Skip to content
This repository was archived by the owner on Jan 6, 2021. It is now read-only.

Commit c58ce46

Browse files
committed
Moved log function to the separate module
Refactor Updated README
1 parent 58c68c7 commit c58ce46

File tree

5 files changed

+88
-63
lines changed

5 files changed

+88
-63
lines changed

README.md

+17-13
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
About
22
===
3-
Git web-hook receiver written on node.js
3+
Git web-hook receiver written in node.js
44

55
Requirements
66
===
@@ -9,25 +9,29 @@ Requirements
99

1010
Instalation
1111
===
12+
```
1213
git clone git://github.com/sashasimkin/hook-receiver.git
14+
```
1315

1416
Usage
1517
===
16-
1. Create file %name%.json
17-
2. Write config(below about this)(json object):
18-
3. Change server PORT and IP(line 1-2)
19-
4. node app.js
20-
5. Use http://IP:PORT/%name% as hook url in github, gitlab, etc.
18+
1. Create file {{name}}.json with config object in dir config/ below about this).
19+
2. node app.js IP:PORT (IP non-required, but if you filled port only the command must looks like `node app.js :PORT`). Defaults `IP='0.0.0.0';PORT=6666`.
20+
3. Use `http://IP:PORT/{{name}}` as hook url in github, gitlab, etc.
2121

22-
Config parameters:
22+
Configuration:
2323
===
24-
* user - System user, from which will ne performed commands
25-
* path - root path for project
26-
* commands - Shell commands, which will be performed after receive hook
27-
* refs - Non-required, if ref not match, any operation will not be performed. String or Array of Strings which be substituted to ref.match()
24+
It is a json file with json object inside in directory config/. This configuration used every time when hook has been recieved.
25+
26+
Configuration parameters:
27+
===
28+
* `user` - System user, from which will be performed commands
29+
* `path` - root path for project, shell commans has been executed there
30+
* `commands` - Shell commands, which will be performed after recieve hook
31+
* `refs` - Non-required, if ref not match, any operation will not be performed. string or array of strings which be substituted to ref.match()
2832

2933

3034
TODO
3135
===
32-
* Think about string-command executing
33-
* require in config with shell commands results
36+
* Commands string instade of array.
37+
* cfg.require parameter with applying the cfg.commands results

app.js

+35-50
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,9 @@
1-
process.env.IP = process.env.IP || '0.0.0.0';
2-
process.env.PORT = process.env.PORT || 6666;
3-
//Rewrite for logging to files
4-
var dbg = true;
5-
function log(msg, file){
6-
if(file){
7-
//Write to file
8-
} else {
9-
console.log(msg);
10-
if(dbg && typeof msg == 'object'){
11-
console.dir(msg);
12-
}
13-
}
14-
15-
return false;
16-
}
171
//Dependencies
182
var http = require("http");
193
var fs = require("fs");
204
var path = require("path");
21-
// var os = require("os");
225
var child_process = require('child_process');
6+
var log = require("./logging");
237
//Runtime variables
248
var cfg_dir = __dirname + '/config/';
259
var cfg_map = {};
@@ -66,13 +50,21 @@ fs.readdir(cfg_dir, function(wtf, files){
6650
});
6751
});
6852

53+
//Server options
54+
if(process.argv[3]){
55+
var run_argv = process.argv[3].split(':');
56+
process.env.IP = run_argv[0] || '0.0.0.0';
57+
process.env.PORT = run_argv[1];
58+
}
59+
process.env.IP = process.env.IP || '0.0.0.0';
60+
process.env.PORT = process.env.PORT || 6666;
61+
6962
// create a server
7063
http.createServer(function(request, response) {
7164
request.url = request.url.slice(1);
72-
//Fucking favicon.ico!
73-
if(request.url == 'favicon.ico'){
74-
return request.connection.destroy();
75-
}
65+
66+
//Prevent favicon.ico requests
67+
if(request.url == 'favicon.ico') return request.connection.destroy();
7668

7769
if(request.method == 'POST' && cfg_map[request.url]){
7870
var body = '';
@@ -85,64 +77,57 @@ http.createServer(function(request, response) {
8577
});
8678

8779
request.on('end', function () {
88-
var bodyObj = JSON.parse(body.trim())
89-
log(body);
90-
80+
var bodyObj = JSON.parse(body.trim());
9181
var cfg = cfg_map[request.url];
9282
var spawn_options = {
9383
encoding: "utf-8",
9484
env: process.env
9585
};
96-
if(cfg.user) spawn_options.uid = cfg.user;
9786

98-
if(!fs.readdirSync(cfg.path)){
87+
if(cfg.user) {
88+
spawn_options.uid = cfg.user;
89+
}
90+
91+
if(!fs.readdirSync(cfg.path)) {
9992
return log('Invalid path "' + cfg.path + '" in config "' + request.url + '"');
10093
}
10194
spawn_options.cwd = cfg.path;
10295

10396
var refsType = typeof cfg.refs;
104-
if(['string', 'object'].indexOf(refsType = typeof cfg.refs)){
97+
if(['string', 'object'].indexOf(refsType)){
10598
if(refsType == 'string') cfg.refs = [cfg.refs];
106-
var cont = false;
99+
var refNotMatch = true;
107100
for(var key in cfg.refs){
108101
if(bodyObj.ref.match(cfg.refs[key])) {
109-
cont = true;
102+
refNotMatch = false;
110103
break;
111104
}
112105
}
113-
if(!cont) return log('No refs match. Aborting.');
106+
if(refNotMatch) return log('No refs match. Aborting.');
114107
}
115108

116109
if(cfg.commands.length){
117-
var handleExec = function(err, stdout, stderr) {
118-
if(err){log(err);}
119-
120-
110+
var onData = function(data) {
111+
log('Command "' + commandString + '" with data: ' + data);
112+
};
113+
var onError = function(data) {
114+
log('Error in command "' + commandString + '" with data: ' + data);
121115
};
122116

123117
for(var i in cfg.commands){
124118
var commandArray = cfg.commands[i];
125119
var commandString = commandArray.join(' ');
126120
var result = child_process.spawn(commandArray.shift(), commandArray, spawn_options);
127121

128-
result.stdout.on('data', function (data) {
129-
log('Command "' + commandString + '" with data: ' + data);
130-
});
131-
132-
result.stderr.on('data', function (data) {
133-
log('Error in command "' + commandString + '" with data: ' + data);
134-
});
122+
result.stdout.on('data', onData);
123+
result.stderr.on('data', onError);
135124
}
136125
}
137-
//Do work according to hook data
138126
});
127+
128+
response.end("Deploy in queue!");
129+
} else {
130+
response.writeHead(404, 'There is nothing.');
131+
response.end("404;");
139132
}
140-
// on every request, we'll output 'Hello world'
141-
response.end("I'm alive!");
142133
}).listen(process.env.PORT, process.env.IP);
143-
144-
// Note: when spawning a server on Cloud9 IDE,
145-
// listen on the process.env.PORT and process.env.IP environment variables
146-
147-
// Click the 'Run' button at the top to start your server,
148-
// then click the URL that is emitted to the Output tab of the console

logging/lib/log.js

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
var dbg = true;
2+
var dir = __dirname + '/../logs';
3+
4+
exports = module.exports = function(msg, file){
5+
if(file){
6+
//TODO: Write to file
7+
} else {
8+
console.log(msg);
9+
if(dbg && typeof msg == 'object'){
10+
console.dir(msg);
11+
}
12+
}
13+
14+
return false;
15+
}

logging/logs/.gitkeep

Whitespace-only changes.

logging/package.json

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
{
2+
"name": "Lite Logging",
3+
"description": "Lite logging system",
4+
"version": "0.1",
5+
"author": {
6+
"name": "Sash Simkin",
7+
"email": "sashasimkin@gmail.com"
8+
},
9+
"main": "lib/log",
10+
"engines": {
11+
"node": ">=0.6.21"
12+
},
13+
"licenses": [
14+
{
15+
"type": "GPL",
16+
"url": "http://github.com/martynsmith/node-irc/raw/master/COPYING"
17+
}
18+
],
19+
"readme": "Read me please",
20+
"_id": "logging@0.1"
21+
}

0 commit comments

Comments
 (0)