-
Notifications
You must be signed in to change notification settings - Fork 0
/
app-admin.js
137 lines (112 loc) · 5.03 KB
/
app-admin.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
const createError = require('http-errors');
const express = require('express');
const path = require('path');
const cookieParser = require('cookie-parser');
const morgan = require('morgan');
const appRootPath = require('app-root-path');
const logger = require('./lib/logger');
// connect db
require('./db');
const meanRestExpress = require('@hicoder/express-core');
// setup emailing
const { GetEmailingManageRouter, MddsEmailer } = require('@hicoder/express-emailing');
const awsConfFile = path.join(appRootPath.toString(), process.env.AWS_CONFIG_FILE_NAME || '.aws.conf.json');
const emailer = new MddsEmailer(awsConfFile);
setTimeout( () => {emailer.startDaemon()}, 20000 );
const emailInfoForAuth = {
serverUrl: process.env.ADMIN_SERVER_URL || 'http://localhost:3001',
serverUrlPasswordReset: process.env.ADMIN_PASSWD_RESET_URL || 'http://localhost:3001/auth/reset/',
serverUrlRegVerification: process.env.REGISTRATION_VERIFICATION_URL || 'http://localhost:3001/auth/regverification/',
}
//for auth client
let option = {
authz: 'role', // admin role based authorization
};
const AuthApp = require('@hicoder/express-auth-app');
const authApp = new AuthApp();
const authFuncs = authApp.getAuthFuncs(option);
//for auth server
const authServer = require('@hicoder/express-auth-server');
const authAccountDef = authServer.authAccountDef;
option = {
authz: 'role', // admin role based authorization
registerEmailVerification: false,
};
const authRouter = authServer.GetDefaultAuthnRouter(authAccountDef, option);
authRouter.setEmailer(emailer, emailInfoForAuth); // set the emailer instance for sending emails
const authzAccessRouter = authServer.GetDefaultAccessManageRouter('Access', authFuncs); // manage public access module
const authzRolesRouter = authServer.GetDefaultRolesManageRouter('Roles', authFuncs); // manage admin roles module
authzRolesRouter.setEmailer(emailer, {});
const defaultUserDef = authServer.authUserDef;
const usersRouter = meanRestExpress.RestRouter(defaultUserDef, 'Users', authFuncs);
usersRouter.setEmailer(emailer, {});
// for Email Template models
const emailingRouter = GetEmailingManageRouter("Emailing", authFuncs);
// for academics models
// const academicsDbDefinition = require('./models/academics/index');
// const academicsRouter = meanRestExpress.RestRouter(academicsDbDefinition, 'Academics', authFuncs);
// academicsRouter.setEmailer(emailer, {}); // set the emailer instance for sending emails
// for public models
// const publicInfoDbDefinition = require('./models/publicInfo/index');
// const publicInfoRouter = meanRestExpress.RestRouter(publicInfoDbDefinition, 'PublicInfo', authFuncs);
// file server
const fileSvr = require('@hicoder/express-file-server');
const defaultAdminSysDef = fileSvr.sampleAdminSysDef;
const fileSOption = {
storage: 'fs',
directory: path.join(__dirname, 'storage', 'uploads'),
linkRoot: '/api/files', // link = linkRoot + '/download' - download needs to be enabled.
}
const dbSOption = {
storage: 'db',
linkRoot: '/api/files', // link = linkRoot + '/download' - download needs to be enabled.
}
const fileSvrRouter = fileSvr.ExpressRouter(defaultAdminSysDef, 'Files', authFuncs, fileSOption);
// Authorization App Client. Call it after all meanRestExpress resources are generated.
const manageModule = ['Users', 'Access', 'Roles', 'Files', 'EmailTemplates']; // the modules that manages
// pass in authzRolesRouter so authApp can upload the managed role moduoes to authzRolesRouter
authApp.run('local', 'app-key', 'app-secrete', authzRolesRouter, { 'roleModules': manageModule });
const app = express();
// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'pug');
app.use(morgan('combined', { stream: logger.stream }));
app.use(express.json({ limit: '10mb' }));
app.use(express.urlencoded({ extended: false }));
app.use(cookieParser());
//app.use('/', indexRouter);
app.use(express.static(path.join(__dirname, 'public-admin')));
// app.use('/api/academics', academicsRouter);
// app.use('/api/publicinfo', publicInfoRouter);
app.use('/api/files', fileSvrRouter);
app.use('/api/users', usersRouter);
app.use('/api/roles', authzRolesRouter);
app.use('/api/access', authzAccessRouter);
app.use('/api/emailing', emailingRouter);
app.use('/api/auth', authRouter);
// fall back to index.html
app.get(/.*/, function(req, res, next) {
if (req.accepts('html')) {
return res.sendFile(path.join(__dirname, './public-admin/index.html'));
} else {
return next();
}
});
// catch 404 and forward to error handler
app.use(function(req, res, next) {
next(createError(404));
});
// error handler
app.use(function(err, req, res, next) {
// set locals, only providing error in development
res.locals.message = err.message;
res.locals.error = req.app.get('env') === 'development' ? err : {};
const allowedErrCode = [400, 401, 403, 404];
if (!allowedErrCode.includes(err.status)) {
logger.error(err)
}
// render the error page
res.status(err.status || 500);
res.render('error');
});
module.exports = app;