-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
110 lines (75 loc) · 2.5 KB
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
"use strict";
const log = console.log;
const fs = require("fs");
const express = require("express");
const app = express();
const cors = require("cors");
const bodyParser = require("body-parser");
const middlewares = require('./middlewares/index');
console.clear();
const server = require('http').createServer(app);
var io = require('socket.io')(server);
var methodOverride = require('method-override');
var provider = (process.env.PROVIDER || 'filesys').toLowerCase();
log(`Provider selected: ${provider}`)
const _cache = require('./common/Cache.js');
const AdminCtrol = require('./Controllers/Admin/Crud');
_cache.Init();
const repo = function () {
const reposAvaible = ['nedb', 'filesys', 'mongo', 'redis', 'postgres', 'sql'];
if (reposAvaible.indexOf(provider) === -1) provider = 'filesys';
return require(`./repos/${provider}`);
}();
const ctrl = function () {
const exist = fs.existsSync(`./Controllers/${provider}Ctrl.js`);
if (!exist) return require(`./Controllers/mainCtrl.js`);
return require(`./Controllers/${provider}Ctrl.js`);
}();
if (!repo.Init()) {
log('repo init failed')
process.exit(1)
}
ctrl.Init(repo, _cache);
AdminCtrol.Init(repo);
function errorHandler(err, req, res, next) {
if (res.headersSent) {
console.log(err)
return next(err)
}
res.sendStatus(500)
}
const port = process.env.PORT || 4000;
app.use(cors());
app.use(
bodyParser.json({
strict: false,
})
);
app.use(methodOverride())
app.use(errorHandler)
middlewares.PreLoad(app)
log(AdminCtrol)
//Admin UI
app.get("/admin/", AdminCtrol.Index);
app.get("/realtime/" , ctrl.Realtime)
//Admin Ctrl
app.get("/admin/collections/", AdminCtrol.Get);
app.get("/admin/collections/:id", AdminCtrol.GetById);
app.post("/admin/collections/", AdminCtrol.Post);
app.put("/admin/collections/:id", AdminCtrol.Put);
//Admin Auth
//Main Ctrl
app.get("/rest/", ctrl.Index);
app.get("/rest/:type/", ...middlewares.GET , ctrl.Get, ...middlewares.GETEND);
app.get("/rest/:type/:id", ...middlewares.GETBYID , ctrl.GetById, ...middlewares.GETBYIDEND);
app.post("/rest/:type/",...middlewares.POST , ctrl.Post, ...middlewares.POSTEND);
app.put("/rest/:type/:id",...middlewares.PUT, ctrl.Put , ...middlewares.PUTEND) ;
app.delete("/rest/:type/:id",...middlewares.DELETE ,ctrl.Delete , ...middlewares.DELETEEND);
middlewares.PostLoad(app)
app.set('socketio', io);
io.on('connection', (socket) => {
console.log('connected....', socket.id)
})
server.listen(port, () => {
log(`Example app listening at http://localhost:${port}`);
});