| 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 = {
- // The 'name' property must be set. Otherwise, the 'name' will be '0' and won't work well. -- 2021.04.05 Yuki Takei
- // see: https://github.com/TypedProject/tsed/blob/7630cda20a1f6fa3a692ecc3e6cd51d37bc3c45f/packages/typeorm/src/utils/createConnection.ts#L10
- name: 'default',
- type: process.env.TYPEORM_CONNECTION,
- host: process.env.TYPEORM_HOST,
- port: process.env.TYPEORM_PORT,
- database: process.env.TYPEORM_DATABASE,
- username: process.env.TYPEORM_USERNAME,
- password: process.env.TYPEORM_PASSWORD,
- synchronize: 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> {
- // for synchromizing when boot
- this.injector.get<TypeORMService>(TypeORMService);
- }
- }
|