-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.js
81 lines (60 loc) · 1.83 KB
/
server.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
const serve = require('./backend-common/util')
const Send = require('koa-send')
const path = require('path')
const version = require('./frontend/package').version
const parameters = require('./customisation/authentication.config')
const getDN = require('./backend-auth/authentication').getDN
const checkLogin = require('./backend-auth/authentication').checkLogin
const haveFrontend = process.argv && process.argv[0] && process.argv[0].indexOf('esb-dashboard.exe') > -1
const router = serve.createRouter(parameters)
router.get('/dn/:user', async ctx => {
const { dn, isAuthorized, result, canResend } = await getDN(ctx.params.user)
ctx.body = {
dn,
isAuthorized,
result,
canResend
}
})
router.put('/authenticate', async (ctx) => {
const userId = ctx.request.body.user
const password = ctx.request.body.password
const user = await checkLogin(userId, password)
if (user && user.dn) console.log(user.dn)
ctx.body = { result: user }
})
router.get('/version', async ctx => {
ctx.body = {
version
}
})
if (haveFrontend) {
router.get('/', async ctx => {
await Send(ctx, path.join('frontend/build', 'index.html'), {root: __dirname})
})
router.get('*', async ctx => {
await Send(ctx, path.join('frontend/build', ctx.path), {root: __dirname})
})
}
const frontendRouteText = haveFrontend ? `
GET / ==> ESB-Dashboard
` : ''
const getHelpText = (parameters) => PORT => {
return `
ESB-Dashboard SPA und Authentication Backend
--------------------------------------------
http listening on port ${PORT}
https listening on port ${PORT+1}
Base dir:
${__dirname}
Configuration:
${JSON.stringify(parameters, 2)}
Routes:
${frontendRouteText}
GET /dn/:user
PUT /authenticate { user, password }
GET /version
GET /checkalive
`
}
serve.startServer(parameters, router, getHelpText(parameters))