多维表格
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

95 lines
2.4 KiB

7 years ago
#! /usr/bin/env node
7 years ago
const morgan = require('morgan');
const bodyParser = require('body-parser');
const express = require('express');
const sqlConfig = require('commander');
const mysql = require('mysql');
const cors = require('cors');
const dataHelp = require('../lib/util/data.helper.js');
const Xapi = require('../lib/xapi.js');
const cmdargs = require('../lib/util/cmd.helper.js');
const cluster = require('cluster');
const numCPUs = require('os').cpus().length;
7 years ago
function startXmysql(sqlConfig) {
7 years ago
/**************** START : setup express ****************/
let app = express();
app.use(morgan('tiny'));
app.use(cors());
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({
extended: true
}));
/**************** END : setup express ****************/
7 years ago
/**************** START : setup mysql ****************/
let mysqlPool = mysql.createPool(sqlConfig);
/**************** END : setup mysql ****************/
7 years ago
/**************** START : setup Xapi ****************/
console.log('');
console.log('');
console.log('');
console.log(' Generating REST APIs at the speed of your thought.. ');
console.log('');
7 years ago
let t = process.hrtime();
let moreApis = new Xapi(sqlConfig, mysqlPool, app);
moreApis.init((err, results) => {
7 years ago
app.listen(sqlConfig.portNumber, sqlConfig.ipAddress);
var t1 = process.hrtime(t);
var t2 = t1[0] + t1[1] / 1000000000;
7 years ago
console.log(" Xmysql took : %d seconds", dataHelp.round(t2, 1));
console.log(" API's base URL : " + "localhost:" + sqlConfig.portNumber);
console.log(' ');
console.log(' - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - ');
});
/**************** END : setup Xapi ****************/
}
function start(sqlConfig) {
//handle cmd line arguments
cmdargs.handle(sqlConfig);
if (cluster.isMaster && sqlConfig.useCpuCores > 1) {
console.log(`Master ${process.pid} is running`);
for (let i = 0; i < numCPUs && i < sqlConfig.useCpuCores; i++) {
console.log(`Forking process number ${i}...`);
cluster.fork();
}
cluster.on('exit', function(worker, code, signal) {
console.log('Worker ' + worker.process.pid + ' died with code: '
+ code + ', and signal: ' + signal);
console.log('Starting a new worker');
cluster.fork();
});
} else {
startXmysql(sqlConfig);
}
}
start(sqlConfig);
7 years ago