Auto reload your Go webserver with Gulp
When you developp a webserver with Go, you must compile each time you do an update in your code. Well.. this is redundant. With Gulp you can automatize this task… Indeed, when a go file is modified, a task compile the application in the “bin” folder (“gopath/bin”) then another launch the executable (the webserver).
So we have 3 distincts tasks :
- Application compile ;
- Lauching application / server ;
- Watching for files modified.
Before starting, check you have NodeJS & NPM installed on your machine.
node -v && npm -v
And the environnement variable for “GOPATH” with
go env
If “GOPATH” is empty, set the variable environnement. More details : https://golang.org/doc/code.html#GOPATH
Gulpfile
In your application folder, create a “package.json” file.
{
"devDependencies": {
"gulp": "^3.8.11",
"gulp-util": "*",
"gulp-sync": "*",
"gulp-livereload": "*",
"node-notifier": "*"
}
}
Then install these modules with the command
npm install
Create a “Gulpfile.js” with these dependences.
const gulp = require('gulp'),
util = require('gulp-util'),
notifier = require('node-notifier'),
sync = require('gulp-sync')(gulp).sync,
reload = require('gulp-livereload'),
child = require('child_process'),
os = require('os');var server = null;// Tasks incoming
Application compile
// Compile application
gulp.task('server:build', function() { var build = child.spawnSync('go', ['install']);
return build;});
Except that it’s not over because compilation errors are not including.
// Compile application
gulp.task('server:build', function() { // Build application in the "gobin" folder
var build = child.spawnSync('go', ['install']); // Something wrong
if (build.stderr.length) {
util.log(util.colors.red('Something wrong with this version :'));
var lines = build.stderr.toString()
.split('\n').filter(function(line) {
return line.length
});
for (var l in lines)
util.log(util.colors.red(
'Error (go install): ' + lines[l]
));
notifier.notify({
title: 'Error (go install)',
message: lines
});
}return build;});
Errors are now displaying in the terminal and in a notify window.
Launch server
// Launch server
gulp.task('server:spawn', function() { // Stop the server
if (server && server !== 'null') {
server.kill();
} // Application name
if (os.platform() == 'win32') {
// Windows
var path_folder = __dirname.split('\\');
} else {
// Linux / MacOS
var path_folder = __dirname.split('/');
}
var length = path_folder.length;
var app = path_folder[length - parseInt(1)]; // Run the server
if (os.platform() == 'win32') {
server = child.spawn(app + '.exe');
} else {
server = child.spawn(app);
} // Display terminal informations
server.stderr.on('data', function(data) {
process.stdout.write(data.toString());
});});
Watching files
// Watch files
gulp.task('server:watch', function() { gulp.watch([
'*.go',
'**/*.go',
], sync([
'server:build',
'server:spawn'
], 'server'));});
This task watching go files presents in the current folder and subfolders of your application.
Let’s play…
At the end of your Gulpfile, at this line for executing our 3 tasks.
gulp.task('default', ['server:build', 'server:spawn', 'server:watch']);
Then run with the default gulp command.
gulp
Complete code available in this Gist.