@ -1,3 +1,4 @@
/*jshint esversion: 6*/
var express = require ( 'express' ) ;
var app = express ( ) ;
var crontab = require ( "./crontab" ) ;
@ -9,7 +10,6 @@ var mime = require('mime');
var fs = require ( 'fs' ) ;
var busboy = require ( 'connect-busboy' ) ; // for file upload
// include the routes
var routes = require ( "./routes" ) . routes ;
@ -17,7 +17,7 @@ var routes = require("./routes").routes;
app . set ( 'view engine' , 'ejs' ) ;
var bodyParser = require ( 'body-parser' ) ;
app . use ( bodyParser . json ( ) ) ; // to support JSON-encoded bodies
app . use ( bodyParser . json ( ) ) ; // to support JSON-encoded bodies
app . use ( bodyParser . urlencoded ( { // to support URL-encoded bodies
extended : true
} ) ) ;
@ -29,12 +29,14 @@ app.use(express.static(__dirname + '/public/css'));
app . use ( express . static ( _ _dirname + '/public/js' ) ) ;
app . set ( 'views' , _ _dirname + '/views' ) ;
//set port
// set port to 8000 or the value set by environment var PORT
app . set ( 'port' , ( process . env . PORT || 8000 ) ) ;
// root page handler
app . get ( routes . root , function ( req , res ) {
// get all the crontabs
// reload the database before rendering
crontab . reload _db ( ) ;
// send all the required parameters
crontab . crontabs ( function ( docs ) {
res . render ( 'index' , {
routes : JSON . stringify ( routes ) ,
@ -46,6 +48,11 @@ app.get(routes.root, function(req, res) {
} ) ;
} ) ;
/ *
Handle to save crontab to database
If it is a new job @ param _id is set to - 1
@ param name , command , schedule , logging has to be sent with _id ( if exists )
* /
app . post ( routes . save , function ( req , res ) {
// new job
if ( req . body . _id == - 1 ) {
@ -58,20 +65,25 @@ app.post(routes.save, function(req, res) {
res . end ( ) ;
} ) ;
// set stop to job
app . post ( routes . stop , function ( req , res ) {
crontab . status ( req . body . _id , true ) ;
res . end ( ) ;
} ) ;
// set start to job
app . post ( routes . start , function ( req , res ) {
crontab . status ( req . body . _id , false ) ;
res . end ( ) ;
} ) ;
// remove a job
app . post ( routes . remove , function ( req , res ) {
crontab . remove ( req . body . _id ) ;
res . end ( ) ;
} ) ;
// set crontab. Needs env_vars to be passed
app . get ( routes . crontab , function ( req , res , next ) {
crontab . set _crontab ( req . query . env _vars , function ( err ) {
if ( err ) next ( err ) ;
@ -79,11 +91,13 @@ app.get(routes.crontab, function(req, res, next) {
} ) ;
} ) ;
// backup crontab db
app . get ( routes . backup , function ( req , res ) {
crontab . backup ( ) ;
res . end ( ) ;
} ) ;
// This renders the restore page similar to backup page
app . get ( routes . restore , function ( req , res ) {
// get all the crontabs
restore . crontabs ( req . query . db , function ( docs ) {
@ -96,16 +110,19 @@ app.get(routes.restore, function(req, res) {
} ) ;
} ) ;
// delete backup db
app . get ( routes . delete _backup , function ( req , res ) {
restore . delete ( req . query . db ) ;
res . end ( ) ;
} ) ;
// restore from backup db
app . get ( routes . restore _backup , function ( req , res ) {
crontab . restore ( req . query . db ) ;
res . end ( ) ;
} ) ;
// export current crontab db so that user can download it
app . get ( routes . export , function ( req , res ) {
var file = _ _dirname + '/crontabs/crontab.db' ;
@ -119,7 +136,7 @@ app.get(routes.export, function(req, res) {
filestream . pipe ( res ) ;
} ) ;
// import from exported crontab db
app . post ( routes . import , function ( req , res ) {
var fstream ;
req . pipe ( req . busboy ) ;
@ -133,13 +150,14 @@ app.post(routes.import, function(req, res) {
} ) ;
} ) ;
// import from current ACTUALL crontab
app . get ( routes . import _crontab , function ( req , res ) {
crontab . import _crontab ( ) ;
res . end ( ) ;
} ) ;
// get the log file a given job. id passed as query param
app . get ( routes . logger , function ( req , res ) {
var fs = require ( "fs" ) ;
_file = crontab . log _folder + "/" + req . query . id + ".log" ;
if ( fs . existsSync ( _file ) )
res . sendFile ( _file ) ;
@ -155,7 +173,7 @@ app.use(function(err, req, res, next) {
data . message = err . message || 'Internal Server Error' ;
if ( process . env . NODE _ENV === 'development' && err . stack ) {
data . stack = err . stack
data . stack = err . stack ;
}
if ( parseInt ( data . statusCode ) >= 500 ) {
@ -166,5 +184,15 @@ app.use(function(err, req, res, next) {
} ) ;
app . listen ( app . get ( 'port' ) , function ( ) {
// If --autosave is used then we will also save whatever is in the db automatically without having to mention it explictly
// we do this by watching log file and setting a on change hook to it
if ( process . argv . includes ( "--autosave" ) ) {
crontab . autosave _crontab ( ( ) => { } ) ;
fs . watchFile ( _ _dirname + '/crontabs/crontab.db' , ( ) => {
crontab . autosave _crontab ( ( ) => {
console . log ( "Attempted to autosave crontab" ) ;
} ) ;
} ) ;
}
console . log ( "Crontab UI is running at http://localhost:" + app . get ( 'port' ) ) ;
} ) ;