-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.js
107 lines (91 loc) · 2.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
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
import { createContainer, asClass, asValue, asFunction } from 'awilix'
import { collectDefaultMetrics, Registry, Counter } from 'prom-client'
import Koa from 'koa'
import Router from '@koa/router'
import koaMount from 'koa-mount'
import {
createHealthMonitor as createMlabsHealthMonitor,
healthLogging,
createHealthy
} from '@meltwater/mlabs-health'
import { objFromKeys, sleeP } from '@meltwater/phi'
import { createServer, koaHealthy, createHealthCheck } from '../index.js'
import { noLifecycle } from './filters.js'
const createHealthMonitor = () =>
createMlabsHealthMonitor(objFromKeys(createHealthCheck, ['puppies']))
const metricDefs = [
{
name: 'puppies_total',
help: 'Number of puppies',
type: Counter
}
]
const createStart =
({ reqId, log, registry, healthMonitor, collectAppMetrics }) =>
async () => {
healthLogging({
log: log.child({ isHealthLog: true, isAppLog: false }),
healthMonitor
})
collectDefaultMetrics({ register: registry })
collectAppMetrics({ register: registry })
log.info({ reqId }, 'Start')
}
const createStop = () => async () => {}
const createApp = () => {
const app = new Koa()
const router = new Router()
router.get('/health', koaHealthy())
router.get('/puppies/:id', (ctx) => {
const puppies = ctx.state.container.resolve('puppies')
const metrics = ctx.state.container.resolve('metrics')
metrics.puppies_total.inc()
ctx.body = { data: puppies.get(ctx.params.id) }
ctx.status = 200
})
app.use(koaMount('/api/v1', router.routes()))
app.use(koaMount('/api/v1', router.allowedMethods()))
return app
}
const createPuppies = ({ log, reqId }) => {
const get = (id) => {
log.info('Bark')
return id
}
const health = async () => {
log.child({ service: 'puppies', reqId }).info('Health: Start')
await sleeP(4000)
return true
}
return { health, get }
}
const createDependencies = ({ log, config }) => {
const container = createContainer()
container.register({
log: asValue(log),
metricDefs: asValue(metricDefs),
registry: asClass(Registry).singleton(),
healthMethods: asValue({ health: createHealthy() }),
healthMonitor: asFunction(createHealthMonitor).singleton(),
start: asFunction(createStart).singleton(),
stop: asFunction(createStop).singleton(),
app: asFunction(createApp).singleton()
})
container.register({
puppies: asFunction(createPuppies).scoped()
})
return container
}
// NOTE: This example does not use config files,
// but must still pass a configPath.
export default ({ log }) =>
(port = 9000) => {
const { configFactory, run } = createServer({
logFilters: { noLifecycle },
configPath: 'examples',
createDependencies
})
configFactory.addOverride({ port })
run(configFactory)
return new Promise(() => {})
}