-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.js
104 lines (85 loc) · 2.77 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
const express = require('express');
const path = require('path');
const session = require('express-session');
const bodyParser = require('body-parser');
const mysql = require('mysql');
const createError = require('http-errors');
const auth = require('./lib/auth');
require('dotenv').config();
const db = mysql.createConnection({
host: process.env.DB_HOST,
user: process.env.DB_USER,
password: process.env.DB_PASS,
database: process.env.DB_NAME,
});
db.connect(err => {
if (err) throw err;
console.log('Connected!');
});
const VehicleService = require('./services/VehicleService');
const EmployeeService = require('./services/EmployeeService');
const BinService = require('./services/BinService');
const TrackService = require('./services/TrackService');
const AllocationService = require('./services/AllocationService')
const AvatarService = require('./services/AvatarService');
const ConsultationService = require('./services/ConsultationService');
const IndexService = require('./services/IndexService');
const ComplainService = require('./services/complainService');
const employeeService = new EmployeeService(db);
const avatars = new AvatarService(path.join(__dirname, './data/avatars'));
const vehicleService = new VehicleService(db);
const binService = new BinService(db);
const trackService = new TrackService(db);
const indexService = new IndexService(db);
const allocationService = new AllocationService(db);
const consultationService = new ConsultationService(db);
const complainService = new ComplainService(db);
const routes = require('./routes');
const app = express();
const port = 3000;
// sessions setup
app.set('trust proxy', 1);
app.use(session({
secret: 'very secret',
resave: true,
saveUninitialized: false
}));
app.use(bodyParser.urlencoded({ extended: true }));
// init passport
const passportConf = auth.passportConfig({employeeService});
app.use(passportConf.initialize);
app.use(passportConf.session);
app.use(passportConf.setUser);
// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(express.static(path.join(__dirname, 'public')));
app.use(
'/',
routes({
vehicleService,
employeeService,
binService,
indexService,
avatars,
trackService,
allocationService,
consultationService,
complainService,
})
);
app.use((req, res, next) => {
return next(createError(404, "Sorry but we couldn't find this page"));
});
app.use((err, req, res, next) => {
res.locals.message = err.message;
const status = err.status || 500;
res.locals.status = status;
res.status(status);
res.render('error');
});
app.listen(port, '0.0.0.0', () => {
console.log(`Express server listening port on ${port}!`);
});