2018-01-12 12:25:07 +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-01-12 12:25:07 +00:00
|
|
|
|
|
|
|
const express = require('express');
|
|
|
|
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-02-16 13:13:46 +00:00
|
|
|
const rest_api = require('./rest_api');
|
|
|
|
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');
|
|
|
|
|
|
|
|
module.exports = {
|
2018-02-16 13:13:46 +00:00
|
|
|
async start({backend, connectionParams, models}) {
|
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) {
|
|
|
|
frappe.registerModels(models);
|
|
|
|
}
|
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 }));
|
|
|
|
app.use(express.static('./'));
|
|
|
|
|
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
|
|
|
|
rest_api.setup(app);
|
|
|
|
|
|
|
|
// listen
|
|
|
|
frappe.app = app;
|
2018-02-19 16:41:10 +00:00
|
|
|
frappe.server = server;
|
|
|
|
|
|
|
|
server.listen(frappe.config.port);
|
2018-01-24 11:52:05 +00:00
|
|
|
},
|
|
|
|
|
2018-01-23 08:00:29 +00:00
|
|
|
async init() {
|
|
|
|
await frappe.init();
|
2018-02-16 13:13:46 +00:00
|
|
|
frappe.registerModels(frappeModels);
|
|
|
|
frappe.registerLibs(common);
|
2018-01-23 08:00:29 +00:00
|
|
|
await frappe.login();
|
|
|
|
},
|
|
|
|
|
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-01-12 12:25:07 +00:00
|
|
|
}
|