| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394 |
- import { Configuration, Inject, InjectorService } from '@tsed/di';
- import { PlatformApplication } from '@tsed/common';
- import '@tsed/platform-express'; // /!\ keep this import
- import bodyParser from 'body-parser';
- import compress from 'compression';
- import cookieParser from 'cookie-parser';
- import methodOverride from 'method-override';
- import '@tsed/swagger';
- import { TypeORMService } from '@tsed/typeorm';
- import { ConnectionOptions } from 'typeorm';
- export const rootDir = __dirname;
- const connectionOptions: ConnectionOptions = {
- type: process.env.TYPEORM_CONNECTION,
- host: process.env.TYPEORM_HOST,
- database: process.env.TYPEORM_DATABASE,
- username: process.env.TYPEORM_USERNAME,
- password: process.env.TYPEORM_PASSWORD,
- synchronize: true,
- logging: true,
- } as ConnectionOptions;
- @Configuration({
- rootDir,
- acceptMimes: ['application/json'],
- httpPort: process.env.PORT || 8080,
- httpsPort: false, // CHANGE
- mount: {
- '/': [
- `${rootDir}/controllers/*.ts`,
- `${rootDir}/middlewares/*.ts`,
- ],
- },
- componentsScan: [
- `${rootDir}/services/*.ts`,
- ],
- typeorm: [
- {
- ...connectionOptions,
- entities: [
- `${rootDir}/entities/*{.ts,.js}`,
- ],
- migrations: [
- `${rootDir}/migrations/*{.ts,.js}`,
- ],
- subscribers: [
- `${rootDir}/subscribers/*{.ts,.js}`,
- ],
- } as ConnectionOptions,
- ],
- swagger: [
- {
- path: '/docs',
- specVersion: '3.0.1',
- },
- ],
- exclude: [
- '**/*.spec.ts',
- ],
- })
- export class Server {
- @Inject()
- app: PlatformApplication;
- @Configuration()
- settings: Configuration;
- @Inject()
- injector: InjectorService;
- $beforeRoutesInit(): void {
- this.app
- .use(cookieParser())
- .use(compress({}))
- .use(methodOverride())
- .use(bodyParser.json())
- .use(bodyParser.urlencoded({
- extended: true,
- }));
- }
- async $onReady(): Promise<void> {
- const typeormService = this.injector.get<TypeORMService>(TypeORMService);
- console.log(typeormService);
- const connection = typeormService?.connectionManager.get('0');
- console.log(connection);
- }
- }
|