Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
private injectStorage(): void {
// transform something.repo.repository into repoRepository
const formatter = (name, descriptor) => {
const splat = name.split('.');
splat.shift();
return camelcase(splat.join('.'));
};
this.registerModule([
[`${__dirname}/../../external/repositories/${config.storage}/**/*.js`, { lifetime: Lifetime.SINGLETON }]
], formatter);
}
import { createContainer, Lifetime, InjectionMode, asValue } from 'awilix'
import { logger } from './logger'
/**
* Configures a new container 配置容器
*
* @return {Object} The container.
*/
const modulesToLoad = [
['services/*.js', Lifetime.SCOPED],
['stores/*.js', Lifetime.SINGLETON]
]
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)
})
const MongoUsersRepository = require('./infra/repository/user/MongoUsersRepository');
const { User: UserModel } = require('./infra/database/models');
const logger = require('./infra/logging/logger');
const database = require('./infra/mongoose');
const container = createContainer();
container.register({
// System
app: asClass(Application, { lifetime: Lifetime.SINGLETON }),
server: asClass(Server, { lifetime: Lifetime.SINGLETON }),
router: asFunction(router, { lifetime: Lifetime.SINGLETON }),
logger: asFunction(logger, { lifetime: Lifetime.SINGLETON }),
config: asValue(config),
//Middlewares
loggerMiddleware: asFunction(loggerMiddleware, { lifetime: Lifetime.SINGLETON }),
containerMiddleware: asValue(scopePerRequest(container)),
errorHandler: asValue(config.production ? errorHandler : devErrorHandler),
swaggerMiddleware: asValue(swaggerMiddleware),
//Repositories
usersRepository: asClass(MongoUsersRepository, { lifetime: Lifetime.SINGLETON }),
//Database
database: asFunction(database),
import ffmpegHlsStreamer from './streamer/ffmpeg-hls-streamer';
import ffmpeDashStreamer from './streamer/ffmpeg-dash-streamer';
import hlsPresetDecision from './streamer/hls/hls-preset-decision';
import M3u8Generator from './streamer/m3u8-generator';
import fsExtra from 'fs-extra';
import path from 'path';
import throttledQueue from 'throttled-queue';
/**
* Using Awilix, the following files and folders (glob patterns)
* will be loaded.
*/
const modulesToLoad = [
['services/*.js', Lifetime.SCOPED],
['lib/extended-info-providers/*-provider.js', Lifetime.SINGLETON],
['stores/*.js', Lifetime.SINGLETON]
];
/**
* Configures a new container.
*
* @return {Object} The container.
*/
export async function configureContainer() {
const opts = {
resolutionMode: ResolutionMode.CLASSIC
};
// @todo create a bootstrap sequence
const dataFolder = process.env.DATA_FOLDER || path.join(process.env.HOME, '.media_speed');
const imagesFolder = path.join(dataFolder, 'cache', 'images');
const transcodingTempFolder = path.join(dataFolder, 'cache', 'transcoding_temp');
private injectModules(): void {
// Core injections
this.registerModule([
[`${__dirname}/../../application/business/**/*.js`, { lifetime: Lifetime.SCOPED }],
[`${__dirname}/../../application/entities/services/**/*.js`, { lifetime: Lifetime.SINGLETON }]
]);
}
import { createContainer, Lifetime, InjectionMode, asValue } from 'awilix'
import { logger } from './logger'
/**
* Using Awilix, the following files and folders (glob patterns)
* will be loaded.
*/
const modulesToLoad = [
// Services should be scoped to the request.
// This means that each request gets a separate instance
// of a service.
['services/*.js', Lifetime.SCOPED],
// Stores will be singleton (1 instance per process).
// This is just for demo purposes, you can do whatever you want.
['stores/*.js', Lifetime.SINGLETON]
]
/**
* Configures a new container.
*
* @return {Object} The container.
*/
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
const devErrorHandler = require('./interfaces/http/errors/devErrorHandler');
const swaggerMiddleware = require('./interfaces/http/swagger/swaggerMiddleware');
const MongoUsersRepository = require('./infra/repository/user/MongoUsersRepository');
const { User: UserModel } = require('./infra/database/models');
const logger = require('./infra/logging/logger');
const database = require('./infra/mongoose');
const container = createContainer();
container.register({
// System
app: asClass(Application, { lifetime: Lifetime.SINGLETON }),
server: asClass(Server, { lifetime: Lifetime.SINGLETON }),
router: asFunction(router, { lifetime: Lifetime.SINGLETON }),
logger: asFunction(logger, { lifetime: Lifetime.SINGLETON }),
config: asValue(config),
//Middlewares
loggerMiddleware: asFunction(loggerMiddleware, { lifetime: Lifetime.SINGLETON }),
containerMiddleware: asValue(scopePerRequest(container)),
errorHandler: asValue(config.production ? errorHandler : devErrorHandler),
swaggerMiddleware: asValue(swaggerMiddleware),
//Repositories
usersRepository: asClass(MongoUsersRepository, { lifetime: Lifetime.SINGLETON }),
registerAll(): void {
this.container.loadModules([
[`${__dirname}/business/**/*.js`, Lifetime.TRANSIENT],
[`${__dirname}/services/**/*.js`, Lifetime.SINGLETON],
[`${__dirname}/adapters/**/*.js`, Lifetime.TRANSIENT]
], {
formatName: 'camelCase',
cwd: '.',
registrationOptions: {
resolutionMode: ResolutionMode.CLASSIC
}
});
debug('app:dependency-resolver')(this.container.registrations);
}
}
if (target.init) {
container.register({ [name]: asFunction(target.init.bind(target)).singleton() })
} else {
container.register({ name, target })
}
}
)
container.loadModules([
'../app/services/*.js'
], {
cwd: __dirname,
formatName: 'camelCase',
registrationOptions: {
lifetime: Lifetime.SINGLETON
}
})
return container
}