-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
55 lines (46 loc) · 1.46 KB
/
server.js
File metadata and controls
55 lines (46 loc) · 1.46 KB
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
const { createServer } = require('http')
const { c, to } = require('4k')
const { required } = require('4k/route-helper')
const server4k = require('4k/server')
const ws = require('./ws')
const db = require('./redis')
const routes = require('./routes')
const missingEnvKeys = [
'DOMAIN',
'MAILGUN_LIST',
'MAILGUN_APIKEY',
'API_PORT',
'GITHUB_CLIENT_ID',
'GITHUB_CLIENT_SECRET',
'CLOUDFLARE_APIKEY',
'CLOUDFLARE_ZONEID',
'CLOUDFLARE_EMAIL' ].filter(key => !(key in process.env))
if (missingEnvKeys.length) {
throw Error(`Missing environement variable ${missingEnvKeys.join(', ')}`)
}
const log = _ => (console.log(_), _)
const server = createServer(server4k({
routes,
domain: `https://api.${process.env.DOMAIN}`,
allowOrigin: `https://${process.env.DOMAIN}`,
session: {
options: { domain: `api.${process.env.DOMAIN}`, path: '/' },
get: required(c([
key => `sessions:${key}`,
db.get,
user => db.hget('users', user),
JSON.parse,
])),
},
})).listen(process.env.API_PORT, () => {
console.info(`server started: http://localhost:${process.env.API_PORT}`)
})
ws.init(server)
module.exports = server
// It return JSON statusCode 200 by default
// If you want to handle the answer yourself, you must resolve to a function
// This function will be called with the response
// and a function to parse the result as your are now responsible to close the request
// Restrictions:
// you may not use 'req' as param name
// bodyOpts