2
0
mirror of https://github.com/frappe/books.git synced 2024-12-23 11:29:03 +00:00
books/server/index.js

56 lines
1.5 KiB
JavaScript
Raw Normal View History

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-02-14 12:50:56 +00:00
async start({backend, connection_params, 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}) {
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
}