-
Notifications
You must be signed in to change notification settings - Fork 135
/
Copy pathindex.js
65 lines (55 loc) · 1.77 KB
/
index.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
const Koa = require('koa')
const KoaRouter = require('koa-router')
// Importing Awilix relatively here, use `awilix` in your
// own setup.
const awilix = require('../..')
// Destructuring to make it nicer.
const { createContainer, asValue, asFunction, asClass } = awilix
// Create a Koa app.
const app = new Koa()
const router = new KoaRouter()
// Create a container.
const container = createContainer({ strict: true })
// Register useful stuff
const MessageService = require('./services/MessageService')
const makeMessageRepository = require('./repositories/messageRepository')
container.register({
// used by the repository; registered.
DB_CONNECTION_STRING: asValue('localhost:1234', {
lifetime: awilix.Lifetime.SINGLETON,
}),
// resolved for each request.
messageService: asClass(MessageService).scoped(),
// only resolved once
messageRepository: asFunction(makeMessageRepository).singleton(),
})
// For each request we want a custom scope.
app.use((ctx, next) => {
console.log('Registering scoped stuff')
ctx.scope = container.createScope()
// based on the query string, let's make a user..
ctx.scope.register({
// This is where you'd use something like Passport,
// and retrieve the req.user or something.
currentUser: asValue({
id: ctx.request.query.userId,
}),
})
return next()
})
// Register a route..
router.get('/messages', (ctx) => {
// Use the scope to resolve the message service.
const messageService = ctx.scope.resolve('messageService')
return messageService.findMessages().then((messages) => {
ctx.body = messages
ctx.status = 200
})
})
// use the routes.
app.use(router.routes())
app.use(router.allowedMethods())
const PORT = 4321
app.listen(PORT, () => {
console.log('Awilix Example running on port', PORT)
})