diff --git a/.eslintrc b/.eslintrc index 9502eb4..5fcede2 100644 --- a/.eslintrc +++ b/.eslintrc @@ -14,7 +14,9 @@ "no-undef": 2, "no-shadow": [1, {"hoist": "functions", "allow": ["e"] }], "no-use-before-define": [2, "nofunc"], - "no-unused-vars": [1, {"vars": "all", "args": "none"}] + "no-unused-vars": [1, {"vars": "all", "args": "none"}], + "no-dupe-keys": 2, + "no-redeclare": 2 }, "env": { "node": true diff --git a/gulpfile.js b/gulpfile.js index 75f11e8..8a321f8 100644 --- a/gulpfile.js +++ b/gulpfile.js @@ -306,7 +306,7 @@ gulp.task('fetchPublicFromClient', () => { gulp.task('apidoc', () => { - const cmd = 'node_modules/apidoc/bin/apidoc -i src -o public/apidoc'; + const cmd = 'node_modules/apidoc-support-socketio/bin/apidoc-support-socketio -i src -o public/apidoc'; console.log( cmd ); syncExec( cmd ); diff --git a/package.json b/package.json index e9214cc..6856dff 100644 --- a/package.json +++ b/package.json @@ -1,9 +1,9 @@ { - "name": "corco", - "version": "0.1.0", + "name": "parallels-comments", + "version": "0.2.0", "private": true, "scripts": { - "apidoc": "node_modules/apidoc/bin/apidoc -i src -o public/apidoc", + "apidoc": "node_modules/apidoc-support-socketio/bin/apidoc-support-socketio -i src -o public/apidoc", "start": "node starter", "restart": "node starter --restart" }, @@ -14,6 +14,8 @@ "debug": "~2.2.0", "express": "~4.12.4", "jade": "~1.9.2", + "log4js": "^0.6.30", + "log4js-extend": "^0.1.2", "morgan": "~1.5.3", "node-sass": "^3.4.2", "node-svn-ultimate": "^1.0.2", @@ -26,7 +28,7 @@ "yargs": "^3.31.0" }, "devDependencies": { - "apidoc": "^0.14.0", + "apidoc-support-socketio": "^0.2.3", "babel-preset-es2015": "^6.3.13", "babel-register": "^6.3.13", "del": "^2.2.0", diff --git a/pm2script.json b/pm2script.json index 6a8bfea..54c296a 100644 --- a/pm2script.json +++ b/pm2script.json @@ -1,5 +1,5 @@ { - "name" : "corco-server", + "name" : "paralalles-comments", //"args" : ["--toto=heya coco", "-d", "1"], "cwd" : "src/", diff --git a/public/main.html b/public/main.html new file mode 100644 index 0000000..c8e6981 --- /dev/null +++ b/public/main.html @@ -0,0 +1,67 @@ + + + + + + + + + {{title}} + + + + + + + aaa + bbb + ccc +
remove
+ +
+ +
+ + + + + + + + + diff --git a/public/sub.html b/public/sub.html new file mode 100644 index 0000000..5e9d785 --- /dev/null +++ b/public/sub.html @@ -0,0 +1,27 @@ + + + + + + + + + {{title}} + + + + + + + +
+ +
+ + + + + diff --git a/public/sub2.html b/public/sub2.html new file mode 100644 index 0000000..8aea6f2 --- /dev/null +++ b/public/sub2.html @@ -0,0 +1,24 @@ + + + + + + + + + {{title}} + + + + + + + + sub2 + sub2 + sub2 + sub2 + + + + diff --git a/src/_apidoc.js b/src/_apidoc.js deleted file mode 100644 index 98d5718..0000000 --- a/src/_apidoc.js +++ /dev/null @@ -1,20 +0,0 @@ - -/** - * @api {get} /corcoFile GET /corcoFile - * @apiGroup CorcoFile - * @apiName GET /corcoFile - * @apiDescription READ the all corcoFile list - * @apiVersion 0.1.0 - * @apiUse CommonResult - * - * @apiUse CommonErrorCode - * @apiError (errorCode) { string } NO_ROOT_DIRECTORY the root directory in the server side was not found - * - * @apiSuccess { array } fileList The list of corcoFiles - * @apiSuccessExample Example data on success: - * { - * fileList: ["test1.corco","test2.corco"] - * } - * - * @apiSampleRequest /corcoFile - */ diff --git a/src/app.js b/src/app.js index da17e39..3e59aa4 100644 --- a/src/app.js +++ b/src/app.js @@ -24,11 +24,12 @@ app.use(bodyParser.json()); app.use(bodyParser.urlencoded({ extended: false })); app.use(cookieParser()); -app.use('/', router); +app.use('/', router ); app.use('/', docRouter ); //app.use(express.static( path.join(__dirname, 'public'))); app.use(express.static( process.env.PUBLIC_PATH )); +console.log( process.env.PUBLIC_PATH ); // catch 404 and forward to error handler app.use(function(req, res, next) { diff --git a/src/bin/www b/src/bin/www index 6e5f336..3a83bb8 100755 --- a/src/bin/www +++ b/src/bin/www @@ -4,8 +4,14 @@ * Module dependencies. */ -process.env.DEVELOPMENT = JSON.parse( process.env.DEVELOPMENT ); -process.env.PRODUCTION = JSON.parse( process.env.PRODUCTION ); +try { + process.env.DEVELOPMENT = JSON.parse( process.env.DEVELOPMENT ); + process.env.PRODUCTION = JSON.parse( process.env.PRODUCTION ); +} catch( err ) { + + process.env.DEVELOPMENT = true; + process.env.PRODUCTION = false; +} if( process.env.DEVELOPMENT ) { require("babel-register"); diff --git a/src/controller/doc.js b/src/controller/doc.js index e231622..f49fd56 100644 --- a/src/controller/doc.js +++ b/src/controller/doc.js @@ -12,17 +12,35 @@ * @apiError (errorCode) { string } NO_ERROR success */ +var _ = require('underscore'); var express = require('express'); var router = express.Router(); -var logger = require('tracer').colorConsole(); +var log4js = require('log4js'); +var log4js_extend = require("log4js-extend"); +log4js_extend(log4js, { + path: __dirname, + format: "at @name (@file:@line:@column)" +}); +var logger = log4js.getLogger(); + var DOC_DIR_PATH = process.env.DOC_DIR_PATH; var fileManager = require('./fileManager/index'); fileManager.initRootPath( DOC_DIR_PATH ); +function getErrorWillSendClient( err ) { + + err = _.pick( err, 'errorCode' ); + if( _.isEmpty( err ) ) { + err = { errorCode: 'UNKNOWN_ERROR' }; + } + + return err; +} + /** - * @api {post} /getDocList getDocList + * @api {post} /readDocList readDocList * @apiGroup Doc - * @apiName getDocList + * @apiName readDocList * @apiDescription READ the all doc list * @apiVersion 0.1.0 * @apiUse CommonResult @@ -36,9 +54,9 @@ fileManager.initRootPath( DOC_DIR_PATH ); * fileList: ["test1.corco","test2.corco"] * } * - * @apiSampleRequest /getDocList + * @apiSampleRequest /readDocList */ -router.post('/getDocList', (req, res, next) => { +router.post('/readDocList', (req, res, next) => { fileManager.loadFileList().then( ( fileList ) => { res.send( fileList ); @@ -46,73 +64,91 @@ router.post('/getDocList', (req, res, next) => { }); /** - * @api {get} /corcoFile/:fileName GET /corcoFile/:fileName - * @apiGroup CorcoFile - * @apiName GET /corcoFile/:fileName + * @api {post} /readDoc readDoc + * @apiGroup Doc + * @apiName readDoc * @apiDescription READ the contents of specific filename * @apiVersion 0.1.0 * - * @apiSuccess { string } raw The contents of corcoFile + * @apiParam { string } fileName fileName + * * @apiSuccessExample Example data on success: * { * raw: "corco_question\n질문합니다!\ncorco_answer\n답변합니다" * } * - * @apiSampleRequest /corcoFile/test.corco + * @apiSampleRequest /readDoc + * @apiSampleRequestParam fileName="test.corco" */ -router.get('/:fileName', (req, res, next) => { +router.post('/readDoc', (req, res, next) => { + + const fileName = req.body.fileName; - logger.debug('get /corcoFile/'+req.params.fileName); + logger.debug( fileName ); + logger.debug('post /readDoc/'+fileName); //var filePath = path.join( CORCO_SVN_PATH, req.params.fileName ); - fileManager.loadFile( req.params.fileName ).then( ( fileContent ) => { - + fileManager.loadFile( fileName ).then( ( fileContent ) => { res.send({ raw: fileContent }); }); }); /** - * @api {post} /corcoFile/:fileName POST /corcoFile/:fileName - * @apiGroup CorcoFile - * @apiName POST /corcoFile + * @api {post} /createDoc createDoc + * @apiGroup Doc + * @apiName createDoc * @apiDescription CREATE a new corco file * @apiVersion 0.1.0 * + * @apiParam { string } fileName The contents of corcoFile want to be save * @apiParam { string } raw The contents of corcoFile want to be save * * @apiSuccess { string } errorCode A result of the request * @apiSuccessExample Example data on success: * { errorCode: "NO_ERROR" } * - * @apiSampleRequest /corcoFile/test.corco + * @apiSampleRequest /createDoc + * @apiSampleRequestParam fileName="test4.corco" + * @apiSampleRequestParam raw="test test test" */ -router.post('/:fileName', (req, res, next) => { +router.post('/createDoc', (req, res, next) => { console.log('wef'); - fileManager.saveFile( req.params.fileName, req.body.raw ).then( () => { + logger.info( req.body ); + fileManager.createFile( req.body.fileName, req.body.raw ).then( () => { res.send({ errorCode: 'NO_ERROR' }); + }).catch( ( err ) => { + + logger.error( err ); + res.send( getErrorWillSendClient( err ) ); }); }); /** - * @api {put} /corcoFile/:fileName PUT /corcoFile/:fileName - * @apiGroup CorcoFile - * @apiName PUT /corcoFile + * @api {post} /updateDoc updateDoc + * @apiGroup Doc + * @apiName updateDoc * @apiDescription UPDATE a exist corco file * @apiVersion 0.1.0 * + * @apiParam { string } fileName fileName * @apiParam { string } raw The contents of corcoFile want to be save * * @apiSuccess { string } errorCode A result of the request * @apiSuccessExample Example data on success: * { errorCode: "NO_ERROR" } * - * @apiSampleRequest /corcoFile/test.corco + * @apiSampleRequest /updateDoc + * @apiSampleRequestParam fileName="test4.corco" + * @apiSampleRequestParam raw="test test test" */ -router.put('/:fileName', (req, res, next) => { +router.post('/updateDoc', (req, res, next) => { - fileManager.saveFile( req.params.fileName, req.body.raw ).then( () => { + fileManager.updateFile( req.body.fileName, req.body.raw ).then( () => { res.send({ errorCode: 'NO_ERROR' }); + }).catch( ( err ) => { + logger.error( err ); + res.send( getErrorWillSendClient( err ) ); }); }); diff --git a/src/controller/fileManager/index.js b/src/controller/fileManager/index.js index 209fac3..4adbbcf 100644 --- a/src/controller/fileManager/index.js +++ b/src/controller/fileManager/index.js @@ -6,14 +6,39 @@ const _ = require('underscore'); const path = require('path'); const pathExists = require('path-exists'); const svnUltimate = require('node-svn-ultimate'); -const logger = require('tracer').colorConsole(); +var log4js = require('log4js'); +var log4js_extend = require("log4js-extend"); +log4js_extend(log4js, { + path: __dirname, + format: "at @name (@file:@line:@column)" +}); +var logger = log4js.getLogger(); + const svnOptions = { username: 'corco', password: 'corco', cwd: process.env.CORCO_SVN_PATH }; +let rootPath = ''; +let doUseSvn = false; + const svn = { + info: function( targetPath ) { + + var deferred = when.defer(); + svnUltimate.commands.info( targetPath, ( err, info ) => { + + if( err ) { + deferred.reject( err ); + } + + deferred.resolve( info ); + }); + + return deferred.promise; + }, + add: function( filePath, options ) { var deferred = when.defer(); options = _.extend({}, svnOptions, options ); @@ -52,36 +77,81 @@ const svn = { } }; -let rootPath = ''; - function realPath( relativePath ) { return path.join( rootPath, relativePath ); } const fileManager = { - saveFile: ( fileName, raw ) => { + createFile: ( fileName, raw ) => { - logger.debug('saveFile'); + logger.debug('createFile'); var filePath = realPath( fileName ); - var alreadyExist = pathExists.sync( filePath ); - logger.debug('saveFile1'); + if( doUseSvn ) { + return svn.update().then( () => { - return nodefn.call( fs.writeFile, filePath, raw ).then( () => { - - return svn.update(); + var alreadyExist = pathExists.sync( filePath ); + if( alreadyExist ) { + logger.error( { errorCode: 'ALREADY_EXIST' } ); + throw { errorCode: 'ALREADY_EXIST' }; + } - }).then( function() { + return nodefn.call( fs.writeFile, filePath, raw ); + + }).then( function() { - if( ! alreadyExist ) { return svn.add( filePath ); + + }).then( function() { + return svn.commit( filePath ); + }); + + } else { + + var alreadyExist = pathExists.sync( filePath ); + if( alreadyExist ) { + var deferred = when.defer(); + logger.error( { errorCode: 'ALREADY_EXIST' } ); + deferred.reject({ errorCode: 'ALREADY_EXIST' }); + return deferred.promise; } + return nodefn.call( fs.writeFile, filePath, raw ); + } + }, - }).then( function() { - return svn.commit( filePath ); - }); + updateFile: ( fileName, raw ) => { + + var filePath = realPath( fileName ); + + if( doUseSvn ) { + + return svn.update().then( () => { + + var alreadyExist = pathExists.sync( filePath ); + if( ! alreadyExist ) { + logger.error( { errorCode: 'NO_EXIST_FILE' } ); + throw { errorCode: 'NO_EXIST_FILE' }; + } + + return nodefn.call( fs.writeFile, filePath, raw ); + + }).then( function() { + return svn.commit( filePath ); + }); + + } else { + + var alreadyExist = pathExists.sync( filePath ); + if( ! alreadyExist ) { + var deferred = when.defer(); + logger.error( { errorCode: 'NO_EXIST_FILE' } ); + deferred.reject({ errorCode: 'NO_EXIST_FILE' }); + return deferred.promise; + } + return nodefn.call( fs.writeFile, filePath, raw ); + } }, loadFile: ( fileName ) => { @@ -94,7 +164,8 @@ const fileManager = { return nodefn.call( fs.readdir, rootPath ).then( ( fileList ) => { return _.filter( fileList, ( fileName ) => { - if( /\.corco$/.test( fileName )) return true; + if( /^\./.test( fileName )) return false; + return true; }); }); }, @@ -102,7 +173,19 @@ const fileManager = { initRootPath: ( root ) => { console.log('initRootPath'); rootPath = root; + doUseSvn; + svn.info( rootPath ).then( function( info ) { + + logger.info( info ); + doUseSvn = true; + + }).catch( err => { + + logger.error( err ); + doUseSvn = false; + }); } }; + module.exports = fileManager; diff --git a/starter.js b/starter.js index af53cad..f80fe9f 100644 --- a/starter.js +++ b/starter.js @@ -12,7 +12,7 @@ const processName = "parallels-comment"; var pm2script = { "name": processName, - "cwd": DEVELOPMENT ? path.join( __dirname,'src') : '', + "cwd": path.join( __dirname, DEVELOPMENT ? 'src' : 'build' ), "args": ["--color"], "script": path.join('bin','www'), @@ -27,11 +27,10 @@ var pm2script = { // Default environment variables that will be injected in any environment and at any start "env": { "PORT": 3015, - "DEVELOPMENT": true, + "DEVELOPMENT": false, "PRODUCTION": false, "DOC_DIR_PATH": path.join( CDN_HOME_DIR, "files" ), - "VIEWS_PATH": path.join( CDN_HOME_DIR, "views" ), "PUBLIC_PATH": path.join( CDN_HOME_DIR, 'public' ) },