forked from altipla-consulting/golang-server-reload
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
222 lines (172 loc) · 4.76 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
'use strict';
var childProcess = require('child_process'),
exec = childProcess.exec,
spawn = childProcess.spawn,
httpProxy = require('http-proxy'),
http = require('http'),
chalk = require('chalk'),
watch = require('gulp-watch'),
dateformat = require('dateformat');
// Log function from gulp-util
var log = function() {
var time = '[' + chalk.grey(dateformat(new Date(), 'HH:MM:ss')) + ']';
var args = Array.prototype.slice.call(arguments);
args.unshift(time);
console.log.apply(console, args);
};
var Server = function(packageName, sourcesPath, serverName) {
this.packageName_ = packageName;
this.sourcesPath_ = sourcesPath;
this.serverName_ = serverName;
this.envFn_ = function() { };
this.touchFile_ = null;
this.filesChanged_ = false;
this.building_ = true;
this.buildError_ = null;
this.runOnExit_ = null;
this.runningProccess_;
};
Server.prototype.setTouchFile = function(touchFile) {
this.touchFile_ = touchFile;
};
Server.prototype.setEnvFunction = function(envFn) {
this.envFn_ = envFn;
};
Server.prototype.serve = function(realPort, listenPort) {
var that = this;
// Watch for changes in the source files
var sources = [
this.sourcesPath_ + '/**/*.go',
];
watch(sources, function() {
if (!that.building_) {
that.building_ = true;
that.build_();
return;
}
that.filesChanged_ = true;
});
// Close child process if it's running while we exit
process.on('exit', function() {
if (that.runningProccess_) {
that.runningProccess_.kill('SIGKILL');
}
});
var proxy = httpProxy.createProxyServer();
proxy.on('error', function(err) {
log(chalk.red(err));
});
var server = http.createServer(function(req, res) {
if (that.building_) {
res.writeHead(500, {'Content-Type': 'text/plain'});
res.end('still building...\n');
return;
}
if (that.buildError_) {
res.writeHead(500, {'Content-Type': 'text/plain'});
res.end(that.buildError_ + '\n');
return;
}
// All correct, proxy the request
proxy.web(req, res, {target: 'http://localhost:' + realPort});
});
server.listen(listenPort);
this.building_ = true;
setTimeout(function() {
that.build_();
}, 3000);
};
Server.prototype.runProcess_ = function() {
var that = this;
if (this.runningProccess_) {
// Try a soft kill of the process first
this.runningProccess_.kill('SIGINT');
this.runOnExit_ = function() {
this.runOnExit_ = null;
this.runProcess_();
};
// If the process does not exits in 3 seconds, kill it
setTimeout(function() {
if (that.runOnExit_) {
that.runningProccess_.kill('SIGKILL');
that.runOnExit_();
}
}, 3000);
return;
}
if (this.filesChanged_) {
setImmediate(function() {
that.build_();
});
return;
}
log(chalk.green('Build successful. Spawn server...'));
this.runningProccess_ = spawn(this.serverName_, [], {
env: this.envFn_(),
});
// Pipe the streams of the process to the console
this.runningProccess_.stdout.pipe(process.stdout);
this.runningProccess_.stderr.pipe(process.stderr);
// When exited clean the process and call hooks if needed
this.runningProccess_.once('exit', function() {
that.runningProccess_ = null;
if (that.runOnExit_) {
that.runOnExit_();
}
});
// Catch any spawning error
this.runningProccess_.once('error', function(err) {
throw err;
});
// Let the process some time to initialize itself
setTimeout(function() {
if (that.filesChanged_) {
setImmediate(function() {
that.build_();
});
return;
}
that.building_ = false;
if (that.touchFile_) {
exec('touch ' + that.touchFile_);
}
}, 1000);
};
Server.prototype.build_ = function() {
var that = this;
log(chalk.yellow('Compile go application...'));
exec('go install ' + this.packageName_, function(err, stdout, stderr) {
that.buildError_ = null;
// If there's a compilation error debug it and save it for later requests
if (err && (err.code === 1 || err.code === 2)) {
that.building_ = false;
log(chalk.red('Build failed'));
console.log(stderr + '\n');
that.buildError_ = stderr;
if (that.filesChanged_) {
that.filesChanged_ = false;
setImmediate(function() {
that.build_();
});
return;
}
if (that.touchFile_) {
exec('touch ' + that.touchFile_);
}
return;
}
if (err) {
log(chalk.red('Exit code: ' + err.code));
throw err;
}
if (that.filesChanged_) {
that.filesChanged_ = false;
setImmediate(function() {
that.build_();
});
return;
}
that.runProcess_();
});
};
module.exports = Server;