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-07 06:27:37 +00:00
|
|
|
backends.mysql = require('frappejs/backends/mysql');
|
2018-01-12 12:25:07 +00:00
|
|
|
|
|
|
|
const express = require('express');
|
|
|
|
const app = express();
|
2018-01-16 06:09:17 +00:00
|
|
|
const frappe = require('frappejs');
|
2018-01-12 12:25:07 +00:00
|
|
|
const rest_api = require('./rest_api')
|
2018-01-24 11:52:05 +00:00
|
|
|
const init_models = require('frappejs/server/init_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-01-23 08:00:29 +00:00
|
|
|
const path = require('path');
|
2018-01-12 12:25:07 +00:00
|
|
|
|
|
|
|
module.exports = {
|
2018-01-23 08:00:29 +00:00
|
|
|
async start({backend, connection_params, static, models_path}) {
|
2018-01-12 12:25:07 +00:00
|
|
|
await this.init();
|
2018-01-23 08:00:29 +00:00
|
|
|
|
2018-01-24 11:52:05 +00:00
|
|
|
this.init_models(models_path);
|
|
|
|
|
2018-01-12 12:25:07 +00:00
|
|
|
// database
|
2018-01-24 11:52:05 +00:00
|
|
|
await this.init_db({backend:backend, connection_params:connection_params});
|
2018-01-12 12:25:07 +00:00
|
|
|
|
|
|
|
// app
|
|
|
|
app.use(bodyParser.json());
|
|
|
|
app.use(bodyParser.urlencoded({ extended: true }));
|
|
|
|
app.use(express.static('./'));
|
|
|
|
|
|
|
|
// routes
|
|
|
|
rest_api.setup(app);
|
|
|
|
|
|
|
|
// listen
|
|
|
|
frappe.app = app;
|
|
|
|
frappe.server = app.listen(frappe.config.port);
|
2018-01-24 11:52:05 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
init_models(models_path) {
|
|
|
|
// import frappe modules
|
|
|
|
init_models(path.join(path.dirname(require.resolve('frappejs')), 'models'));
|
2018-01-12 12:25:07 +00:00
|
|
|
|
2018-01-24 11:52:05 +00:00
|
|
|
// import modules from the app
|
|
|
|
init_models(models_path);
|
2018-01-23 08:00:29 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
async init() {
|
|
|
|
await frappe.init();
|
|
|
|
common.init_libs(frappe);
|
|
|
|
await frappe.login();
|
|
|
|
},
|
|
|
|
|
|
|
|
async init_db({backend, connection_params}) {
|
2018-02-08 06:46:38 +00:00
|
|
|
frappe.db = await new backends[backend](connection_params);
|
2018-01-23 08:00:29 +00:00
|
|
|
await frappe.db.connect();
|
|
|
|
await frappe.db.migrate();
|
|
|
|
},
|
2018-01-12 12:25:07 +00:00
|
|
|
}
|