Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
import CrossOriginMW from './middleware/CrossOriginMW';
// Import controllers
import UserController from './api/user/UserController';
// Import Factories
import ExceptionFactory from './error/ExceptionFactory';
// Import Services
import UserService from './services/UserService';
// Import Repositories
import BaseRepository from './repository/BaseRepository';
import UserRepository from './repository/UserRepository';
let container = awilix.createContainer({
resolutionMode: awilix.ResolutionMode.CLASSIC
});
mongoose.Promise = global.Promise;
mongoose.connect(config.DB.mongodb.nodebaseapp.connection, { useMongoClient: true });
console.log("Initializing Swagger API documentation...");
let swaggerDefinition = {
info: {
title: 'RESTful web services for node/express/es6 starter',
version: '1.0.0',
description: 'Documentation for node/express/es6 starter',
},
host: 'localhost:3001',
basePath: '/v1/',
schemes: ['http'],
export function configureContainer() {
const opts = {
injectionMode: InjectionMode.CLASSIC
}
return createContainer(opts)
// load modules and registers 加载,注册模块 e.g. import userService from `services/user-service.js`
.loadModules(modulesToLoad, {
cwd: `${__dirname}/..`,
formatName: 'camelCase'
})
.register({
// construct logger
logger: asValue(logger)
})
}
export function configureContainer() {
const opts = {
// Classic means Awilix will look at function parameter
// names rather than passing a Proxy.
injectionMode: InjectionMode.CLASSIC
}
return createContainer(opts)
.loadModules(modulesToLoad, {
// `modulesToLoad` paths should be relative
// to this file's parent directory.
cwd: `${__dirname}/..`,
// Example: registers `services/todo-service.js` as `todoService`
formatName: 'camelCase'
})
.register({
// Our logger is already constructed,
// so provide it as-is to anyone who wants it.
logger: asValue(logger)
})
}
const { createContainer, asValue, asFunction } = require('awilix')
// you can do this
const app = require('./app')
const server = require('./interfaces/http/server')
const router = require('./interfaces/http/router')
const auth = require('./interfaces/http/auth')
const config = require('../config')
const logger = require('./infra/logging/logger')
const database = require('./infra/database')
const jwt = require('./infra/jwt')
const response = require('./infra/support/response')
const date = require('./infra/support/date')
const repository = require('./infra/repositories')
const container = createContainer()
// SYSTEM
container
.register({
app: asFunction(app).singleton(),
server: asFunction(server).singleton(),
router: asFunction(router).singleton(),
logger: asFunction(logger).singleton(),
database: asFunction(database).singleton(),
auth: asFunction(auth).singleton(),
jwt: asFunction(jwt).singleton(),
response: asFunction(response).singleton(),
date: asFunction(date).singleton(),
config: asValue(config),
repository: asFunction(repository).singleton()
})
mediator.on('db.ready', (db) => {
const container = createContainer()
container.register({
database: asValue(db),
validate: asValue(models.validate),
booking: asValue(models.booking),
user: asValue(models.user),
ticket: asValue(models.ticket),
ObjectID: asValue(database.ObjectID),
serverSettings: asValue(serverSettings),
paymentService: asValue(services.paymentService),
notificationService: asValue(services.notificationService)
})
mediator.emit('di.ready', container)
})
mediator.once('init', () => {
const container = createContainer()
container.register({
dockerSettings: asValue(dockerSettings),
serverSettings: asValue(serverSettings)
})
mediator.emit('di.ready', container)
})
}
_initContainer() {
this._container = awilix.createContainer();
this._container.register({
config: asValue(this._config),
logger: asValue(this._logger),
amqp: asClass(AMQPService).singleton(),
k8s: asClass(K8sService).singleton(),
amqpConnection: asFunction(() => this._container.resolve('amqp').getConnection())
});
}
private middleware(): void {
this.express.use(logger('dev'));
this.express.use(bodyParser.json());
this.express.use(bodyParser.urlencoded({ extended: false }));
this.router = express.Router();
this.container = createContainer();
this.container.register({ router: asValue(this.router) });
this.injectModules();
this.injectStorage();
this.injectFiles();
this.injectExternalLibraries();
this.injectSchemas();
this.injectPlugins();
this.express.use((req: Request, res, next) => {
req.container = this.container.createScope();
req.container.register({
user: asValue(req.user)
});
next();
constructor() {
this.container = createContainer();
}
describe('Payment API', () => {
let app = null
let paid = null
const serverSettings = {
port: 3000
}
const container = createContainer()
container.register({
validate: asValue(models.validate),
serverSettings: asValue(serverSettings),
stripe: asValue(stripe(stripeSettings.secret))
})
let _testRepo = {
registerPurchase ({container}, payment) {
return new Promise((resolve, reject) => {
container.cradle.stripe.charges.create({
amount: Math.ceil(payment.amount * 100),
currency: payment.currency,
source: {
number: payment.number,
cvc: payment.cvc,