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

91 lines
2.6 KiB
JavaScript
Raw Normal View History

2018-09-29 12:54:03 +05:30
const backends = {};
2018-01-16 11:39:17 +05:30
backends.sqlite = require('frappejs/backends/sqlite');
//backends.mysql = require('frappejs/backends/mysql');
const path = require('path');
2018-01-12 17:55:07 +05:30
const express = require('express');
2018-04-30 19:57:41 +05:30
const cors = require('cors');
2018-01-12 17:55:07 +05:30
const app = express();
2018-02-19 22:11:10 +05:30
const server = require('http').Server(app);
const io = new require('socket.io').Server(server);
2018-01-16 11:39:17 +05:30
const frappe = require('frappejs');
2018-03-05 22:15:21 +05:30
const restAPI = require('./restAPI');
const frappeModels = require('frappejs/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-02-20 19:41:44 +05:30
const fs = require('fs');
const { setupExpressRoute: setRouteForPDF } = require('frappejs/server/pdf');
const morgan = require('morgan');
const { addWebpackMiddleware } = require('../webpack/serve');
const { getAppConfig, resolveAppDir } = require('../webpack/utils');
frappe.conf = getAppConfig();
2018-02-20 19:41:44 +05:30
require.extensions['.html'] = function (module, filename) {
module.exports = fs.readFileSync(filename, 'utf8');
};
2018-01-12 17:55:07 +05:30
module.exports = {
2019-12-10 01:30:01 +05:30
async start({backend, connectionParams, models}) {
2018-01-12 17:55:07 +05:30
await this.init();
2018-01-23 13:30:29 +05:30
if (models) {
2018-03-05 22:15:21 +05:30
frappe.registerModels(models, 'server');
}
2018-01-24 17:22:05 +05:30
2018-01-12 17:55:07 +05:30
// database
await this.initDb({backend:backend, connectionParams:connectionParams});
2018-01-12 17:55:07 +05:30
// app
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.use(express.static(frappe.conf.distPath));
2019-12-07 00:02:55 +05:30
app.use('/static', express.static(resolveAppDir(frappe.conf.staticPath)))
app.use(morgan('tiny'));
2018-04-30 19:57:41 +05:30
if (connectionParams.enableCORS) {
app.use(cors());
}
2018-02-19 22:11:10 +05:30
// socketio
io.on('connection', function (socket) {
frappe.db.bindSocketServer(socket);
});
2018-01-12 17:55:07 +05:30
// routes
2018-03-05 22:15:21 +05:30
restAPI.setup(app);
2018-01-12 17:55:07 +05:30
if (process.env.NODE_ENV === 'development') {
// webpack dev server
addWebpackMiddleware(app);
}
frappe.config.port = frappe.conf.dev.devServerPort;
2018-04-30 19:57:41 +05:30
2018-01-12 17:55:07 +05:30
// listen
2018-04-30 19:57:41 +05:30
server.listen(frappe.config.port, () => {
console.log(`FrappeJS server running on http://localhost:${frappe.config.port}`)
});
2018-01-12 17:55:07 +05:30
frappe.app = app;
2018-02-19 22:11:10 +05:30
frappe.server = server;
setRouteForPDF();
2018-01-24 17:22:05 +05:30
},
2018-01-23 13:30:29 +05:30
async init() {
2018-03-05 22:15:21 +05:30
frappe.isServer = true;
2019-12-10 01:30:01 +05:30
frappe.init();
2018-03-05 22:15:21 +05:30
frappe.registerModels(frappeModels, 'server');
frappe.registerLibs(common);
2018-04-30 19:57:41 +05:30
await frappe.login('Administrator');
2018-01-23 13:30:29 +05:30
},
async initDb({backend, connectionParams}) {
frappe.db = await new backends[backend](connectionParams);
2018-01-23 13:30:29 +05:30
await frappe.db.connect();
await frappe.db.migrate();
},
2018-01-12 17:55:07 +05:30
}