generated from MyLife-Services/mylife-maht
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
116 lines (114 loc) · 4.43 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
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
// *imports
import path from 'path'
import { fileURLToPath } from 'url'
// server
import Koa from 'koa'
import { koaBody } from 'koa-body'
import render from 'koa-ejs'
import session from 'koa-generic-session'
import serve from 'koa-static'
// import Router from 'koa-router'
// misc
import chalk from 'chalk'
// local services
import MyLife from './inc/js/mylife-agent-factory.mjs'
// constants/variables
// @todo - parse environment variables in Globals and then have them available via as values
const app = new Koa()
const port = JSON.parse(process.env.PORT ?? '3000')
const __filename = fileURLToPath(import.meta.url)
const __dirname = path.dirname(__filename)
const MemoryStore = new session.MemoryStore()
const _Maht = await MyLife // Mylife is the pre-instantiated exported version of organization with very unique properties. MyLife class can protect fields that others cannot, #factory as first refactor will request
const serverRouter = await _Maht.router
console.log(chalk.bgBlue('created-core-entity:', chalk.bgRedBright('MAHT')))
// test harness region
// koa-ejs
render(app, {
root: path.join(__dirname, 'views'),
layout: 'layout',
viewExt: 'html',
cache: false,
debug: false,
})
// Set an interval to check for alerts every minute (60000 milliseconds)
setInterval(checkForLiveAlerts, JSON.parse(process.env.MYLIFE_SYSTEM_ALERT_CHECK_INTERVAL ?? '60000'))
// app bootup
// app context (ctx) modification
app.context.MyLife = _Maht
app.context.Globals = _Maht.globals
app.context.menu = _Maht.menu
app.context.hostedMembers = JSON.parse(process.env.MYLIFE_HOSTED_MBR_ID) // array of mbr_id
// does _Maht, as uber-sessioned, need to have ctx injected?
app.keys = [process.env.MYLIFE_SESSION_KEY ?? `mylife-session-failsafe|${_Maht.newGuid()}`]
// Enable Koa body w/ configuration
app.use(koaBody({
multipart: true,
formidable: {
maxFileSize: parseInt(process.env.MYLIFE_EMBEDDING_SERVER_FILESIZE_LIMIT_ADMIN) || 10485760, // 10MB in bytes
},
}))
.use(serve(path.join(__dirname, 'views', 'assets')))
.use(
session( // session initialization
{
key: 'mylife.sid', // cookie session id
maxAge: parseInt(process.env.MYLIFE_SESSION_TIMEOUT_MS) || 900000, // session lifetime in milliseconds
autoCommit: true,
overwrite: true,
httpOnly: false,
signed: true,
rolling: false,
renew: false,
store: MemoryStore,
},
app
))
.use(async (ctx,next) => { // GLOBAL ERROR `.catch()` to present in ctx format.
try {
await next()
} catch (err) {
ctx.status = err.statusCode || err.status || 500
ctx.body = {
message: err.message
}
console.error(err)
}
})
.use(async (ctx,next) => { // SESSION: member login
// system context, koa: https://koajs.com/#request
if(!ctx.session?.MemberSession){
/* create generic session [references/leverages modular capabilities] */
ctx.session.MemberSession = await ctx.MyLife.getMyLifeSession() // create default locked session upon first request; does not require init(), _cannot_ have in fact, as it is referencing a global modular set of utilities and properties in order to charge-back to system as opposed to member
/* platform-required session-external variables */
ctx.session.signup = false
/* log */
console.log(chalk.bgBlue('created-member-session'))
}
ctx.state.locked = ctx.session.MemberSession.locked
ctx.state.MemberSession = ctx.session.MemberSession // lock-down session to state
ctx.state.member = ctx.state.MemberSession?.member
?? ctx.MyLife // point member to session member (logged in) or MAHT (not logged in)
ctx.state.avatar = ctx.state.member.avatar
ctx.state.contributions = ctx.state.avatar.contributions
ctx.state.interfaceMode = ctx.state.avatar?.mode??'standard'
ctx.state.menu = ctx.MyLife.menu
if(!await ctx.state.MemberSession.requestConsent(ctx))
ctx.throw(404,'asset request rejected by consent')
await next()
})
.use(async(ctx,next) => { // alert check
await next()
})
// .use(MyLifeMemberRouter.routes()) // enable member routes
// .use(MyLifeMemberRouter.allowedMethods()) // enable member routes
.use(serverRouter.routes()) // enable system routes
.use(serverRouter.allowedMethods()) // enable system routes
.listen(port, () => { // start the server
console.log(chalk.bgGreenBright('server available')+chalk.yellow(`\nlistening on port ${port}`))
})
// Example of a periodic alert check function
function checkForLiveAlerts() {
console.log("Checking for live alerts...")
_Maht.getAlerts()
}