- Building RESTful Web services with Go
- Naren Yellavula
- 243字
- 2021-07-02 20:14:06
Using Gulp for creating auto code compiling and server reloading
With the little introduction we gave about Gulp in the preceding section, we are going to write a gulpfile for telling the computer to execute a few tasks.
I install Gulp and Gulp-shell using npm:
npm install gulp gulp-shell
After this, create a gulpfile.js in the root directory of the project. Here, it is github.com/src/narenaryan/romanserver. Now add this content to gulpfile.js. First, whenever a file changes, install binary task gets executed. Then, the supervisor will be restarted. The watch task looks for any file change and executes the preceding tasks. We are also ordering the tasks so that they occur one after the other synchronously. All of these tasks are Gulp tasks and can be defined by the gulp.task function. It takes two arguments with task name, task. sell.task allows Gulp to execute system commands:
var gulp = require("gulp");
var shell = require('gulp-shell');
// This compiles new binary with source change
gulp.task("install-binary", shell.task([
'go install github.com/narenaryan/romanserver'
]));
// Second argument tells install-binary is a deapendency for restart-supervisor
gulp.task("restart-supervisor", ["install-binary"], shell.task([
'supervisorctl restart myserver'
]))
gulp.task('watch', function() {
// Watch the source code for all changes
gulp.watch("*", ['install-binary', 'restart-supervisor']);
});
gulp.task('default', ['watch']);
Now, if you run the gulp command in the source directory, it starts watching your source code changes:
gulp
Now, if we modify the code, then the code is compiled, installed, and the server restarted in a flash:
- 網絡教育學習指導
- Web安全防護指南:基礎篇
- 物聯網網絡安全及應用
- 互聯網安全的40個智慧洞見:2014年中國互聯網安全大會文集
- 物聯網長距離無線通信技術應用與開發
- Microservice Patterns and Best Practices
- Working with Legacy Systems
- 4G小基站系統原理、組網及應用
- 紅藍攻防:構建實戰化網絡安全防御體系
- 深入理解Nginx:模塊開發與架構解析
- LwIP應用開發實戰指南:基于STM32
- 華為HCIA-Datacom認證指南
- React Design Patterns and Best Practices(Second Edition)
- Cisco無線局域網配置基礎
- 趣話通信:6G的前世、今生和未來