2018-09-29 07:24:03 +00:00
|
|
|
const backends = {};
|
2018-01-16 06:09:17 +00:00
|
|
|
backends.sqlite = require('frappejs/backends/sqlite');
|
2018-02-16 13:13:46 +00:00
|
|
|
//backends.mysql = require('frappejs/backends/mysql');
|
2018-09-29 07:17:43 +00:00
|
|
|
const path = require('path');
|
2018-01-12 12:25:07 +00:00
|
|
|
const express = require('express');
|
2018-04-30 14:27:41 +00:00
|
|
|
const cors = require('cors');
|
2018-01-12 12:25:07 +00:00
|
|
|
const app = express();
|
2018-02-19 16:41:10 +00:00
|
|
|
const server = require('http').Server(app);
|
|
|
|
const io = require('socket.io')(server);
|
2018-01-16 06:09:17 +00:00
|
|
|
const frappe = require('frappejs');
|
2018-03-05 16:45:21 +00:00
|
|
|
const restAPI = require('./restAPI');
|
2018-02-16 13:13:46 +00:00
|
|
|
const frappeModels = require('frappejs/models');
|
2018-01-16 06:09:17 +00:00
|
|
|
const common = require('frappejs/common');
|
2018-01-12 12:25:07 +00:00
|
|
|
const bodyParser = require('body-parser');
|
2018-02-20 14:11:44 +00:00
|
|
|
const fs = require('fs');
|
2018-04-14 19:14:02 +00:00
|
|
|
const { setupExpressRoute: setRouteForPDF } = require('frappejs/server/pdf');
|
2018-04-20 04:34:12 +00:00
|
|
|
const auth = require('./../auth/auth')();
|
2018-07-29 11:21:03 +00:00
|
|
|
const morgan = require('morgan');
|
|
|
|
const { addWebpackMiddleware } = require('../webpack/serve');
|
2018-10-02 08:03:48 +00:00
|
|
|
const { getAppConfig, resolveAppDir } = require('../webpack/utils');
|
2018-08-18 15:54:17 +00:00
|
|
|
|
|
|
|
frappe.conf = getAppConfig();
|
2018-02-20 14:11:44 +00:00
|
|
|
|
|
|
|
require.extensions['.html'] = function (module, filename) {
|
|
|
|
module.exports = fs.readFileSync(filename, 'utf8');
|
|
|
|
};
|
2018-01-12 12:25:07 +00:00
|
|
|
|
|
|
|
module.exports = {
|
2018-08-10 12:11:48 +00:00
|
|
|
async start({backend, connectionParams, models, authConfig=null}) {
|
2018-01-12 12:25:07 +00:00
|
|
|
await this.init();
|
2018-01-23 08:00:29 +00:00
|
|
|
|
2018-02-16 13:13:46 +00:00
|
|
|
if (models) {
|
2018-03-05 16:45:21 +00:00
|
|
|
frappe.registerModels(models, 'server');
|
2018-02-16 13:13:46 +00:00
|
|
|
}
|
2018-01-24 11:52:05 +00:00
|
|
|
|
2018-01-12 12:25:07 +00:00
|
|
|
// database
|
2018-02-16 13:13:46 +00:00
|
|
|
await this.initDb({backend:backend, connectionParams:connectionParams});
|
2018-01-12 12:25:07 +00:00
|
|
|
|
|
|
|
// app
|
|
|
|
app.use(bodyParser.json());
|
|
|
|
app.use(bodyParser.urlencoded({ extended: true }));
|
2018-08-10 12:11:48 +00:00
|
|
|
|
2018-08-18 15:54:17 +00:00
|
|
|
app.use(express.static(frappe.conf.distPath));
|
2019-12-06 18:32:55 +00:00
|
|
|
app.use('/static', express.static(resolveAppDir(frappe.conf.staticPath)))
|
2018-08-10 12:11:48 +00:00
|
|
|
|
2018-04-20 04:34:12 +00:00
|
|
|
app.use(morgan('tiny'));
|
|
|
|
|
2018-04-30 14:27:41 +00:00
|
|
|
if (connectionParams.enableCORS) {
|
|
|
|
app.use(cors());
|
|
|
|
}
|
|
|
|
|
2018-04-20 04:34:12 +00:00
|
|
|
if(authConfig) {
|
2018-04-30 14:27:41 +00:00
|
|
|
this.setupAuthentication(app, authConfig);
|
2018-04-20 04:34:12 +00:00
|
|
|
}
|
2018-01-12 12:25:07 +00:00
|
|
|
|
2018-02-19 16:41:10 +00:00
|
|
|
// socketio
|
|
|
|
io.on('connection', function (socket) {
|
|
|
|
frappe.db.bindSocketServer(socket);
|
|
|
|
});
|
2018-01-12 12:25:07 +00:00
|
|
|
// routes
|
2018-03-05 16:45:21 +00:00
|
|
|
restAPI.setup(app);
|
2018-01-12 12:25:07 +00:00
|
|
|
|
2018-07-29 11:21:03 +00:00
|
|
|
if (process.env.NODE_ENV === 'development') {
|
|
|
|
// webpack dev server
|
|
|
|
addWebpackMiddleware(app);
|
|
|
|
}
|
|
|
|
|
2018-08-18 15:54:17 +00:00
|
|
|
frappe.config.port = frappe.conf.dev.devServerPort;
|
2018-04-30 14:27:41 +00:00
|
|
|
|
2018-01-12 12:25:07 +00:00
|
|
|
// listen
|
2018-04-30 14:27:41 +00:00
|
|
|
server.listen(frappe.config.port, () => {
|
|
|
|
console.log(`FrappeJS server running on http://localhost:${frappe.config.port}`)
|
|
|
|
});
|
2018-04-14 19:14:02 +00:00
|
|
|
|
2018-01-12 12:25:07 +00:00
|
|
|
frappe.app = app;
|
2018-02-19 16:41:10 +00:00
|
|
|
frappe.server = server;
|
|
|
|
|
2018-04-14 19:14:02 +00:00
|
|
|
setRouteForPDF();
|
2018-01-24 11:52:05 +00:00
|
|
|
},
|
|
|
|
|
2018-01-23 08:00:29 +00:00
|
|
|
async init() {
|
2018-03-05 16:45:21 +00:00
|
|
|
frappe.isServer = true;
|
2018-01-23 08:00:29 +00:00
|
|
|
await frappe.init();
|
2018-03-05 16:45:21 +00:00
|
|
|
frappe.registerModels(frappeModels, 'server');
|
2018-02-16 13:13:46 +00:00
|
|
|
frappe.registerLibs(common);
|
2018-04-14 19:14:02 +00:00
|
|
|
|
2018-04-30 14:27:41 +00:00
|
|
|
await frappe.login('Administrator');
|
2018-01-23 08:00:29 +00:00
|
|
|
},
|
|
|
|
|
2018-02-16 13:13:46 +00:00
|
|
|
async initDb({backend, connectionParams}) {
|
|
|
|
frappe.db = await new backends[backend](connectionParams);
|
2018-01-23 08:00:29 +00:00
|
|
|
await frappe.db.connect();
|
|
|
|
await frappe.db.migrate();
|
|
|
|
},
|
2018-04-30 14:27:41 +00:00
|
|
|
|
|
|
|
setupAuthentication(app, authConfig) {
|
|
|
|
app.post("/api/signup", auth.signup);
|
|
|
|
app.post("/api/login", auth.login);
|
|
|
|
app.use(auth.initialize(authConfig));
|
|
|
|
app.all("/api/resource/*", auth.authenticate());
|
|
|
|
}
|
2018-01-12 12:25:07 +00:00
|
|
|
}
|