configuration.ts 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. import { App, Configuration } from '@midwayjs/decorator';
  2. import * as DefaultConfig from './config/config.default';
  3. import * as koa from '@midwayjs/koa';
  4. // 数据库
  5. import * as typegoose from '@midwayjs/typegoose';
  6. import * as Typegoose from '@typegoose/typegoose';
  7. import { ResponseMiddleware } from './middleware/response.middleware';
  8. import { DefaultErrorFilter } from './filter/default.filter';
  9. @Configuration({
  10. namespace: 'free',
  11. imports: [koa, typegoose],
  12. importConfigs: [
  13. {
  14. default: DefaultConfig,
  15. },
  16. ],
  17. })
  18. export class FreeConfiguration {
  19. @App()
  20. app: koa.Application;
  21. getApiUrl() {
  22. const path = this.app.getConfig()?.swagger?.swaggerPath;
  23. const port = this.app.getConfig()?.koa?.port;
  24. if (path)
  25. console.log(`api文档: http://127.0.0.1:${port}${path}/index.html`);
  26. }
  27. async onReady() {
  28. // TODO something
  29. this.app.getMiddleware().insertLast(ResponseMiddleware);
  30. this.app.useFilter([DefaultErrorFilter]);
  31. Typegoose.setGlobalOptions({
  32. schemaOptions: {
  33. id: true,
  34. toJSON: { getters: true, virtuals: true },
  35. toObject: { getters: true, virtuals: true },
  36. },
  37. options: { allowMixed: Typegoose.Severity.ALLOW },
  38. });
  39. this.getApiUrl();
  40. console.log('free frame is ready');
  41. }
  42. }