2
0
mirror of https://github.com/frappe/books.git synced 2025-01-26 16:48:28 +00:00
books/server/index.js

56 lines
1.5 KiB
JavaScript
Raw Normal View History

2018-01-12 17:55:07 +05:30
const backends = {};
2018-01-16 11:39:17 +05:30
backends.sqlite = require('frappejs/backends/sqlite');
2018-01-12 17:55:07 +05:30
const express = require('express');
const app = express();
2018-01-16 11:39:17 +05:30
const frappe = require('frappejs');
2018-01-12 17:55:07 +05:30
const rest_api = require('./rest_api')
2018-01-24 17:22:05 +05:30
const init_models = require('frappejs/server/init_models');
2018-01-16 11:39:17 +05:30
const common = require('frappejs/common');
2018-01-12 17:55:07 +05:30
const bodyParser = require('body-parser');
2018-01-23 13:30:29 +05:30
const path = require('path');
2018-01-12 17:55:07 +05:30
module.exports = {
2018-01-23 13:30:29 +05:30
async start({backend, connection_params, static, models_path}) {
2018-01-12 17:55:07 +05:30
await this.init();
2018-01-23 13:30:29 +05:30
2018-01-24 17:22:05 +05:30
this.init_models(models_path);
2018-01-12 17:55:07 +05:30
// database
2018-01-24 17:22:05 +05:30
await this.init_db({backend:backend, connection_params:connection_params});
2018-01-12 17:55:07 +05:30
// 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 17:22:05 +05:30
},
init_models(models_path) {
// import frappe modules
init_models(path.join(path.dirname(require.resolve('frappejs')), 'models'));
2018-01-12 17:55:07 +05:30
2018-01-24 17:22:05 +05:30
// import modules from the app
init_models(models_path);
2018-01-23 13:30:29 +05:30
},
async init() {
await frappe.init();
common.init_libs(frappe);
await frappe.login();
},
async init_db({backend, connection_params}) {
frappe.db = await new backends[backend].Database(connection_params);
await frappe.db.connect();
await frappe.db.migrate();
},
2018-01-12 17:55:07 +05:30
}