main.ts 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. import Koa from 'koa';
  2. import koaBody from 'koa-body';
  3. import cors from '@koa/cors';
  4. import http2 from 'http2';
  5. // import http from 'http';
  6. import fs from 'fs';
  7. import path from 'path';
  8. import { router } from './router';
  9. import { getAppConfig } from './utils';
  10. import { GlobalStore, updateGlobalStore } from './store';
  11. async function start () {
  12. try {
  13. const config = await getAppConfig();
  14. updateGlobalStore(new GlobalStore(config));
  15. const app = new Koa();
  16. app.use(cors({
  17. credentials: true,
  18. allowMethods: ['GET', 'HEAD', 'PUT', 'POST', 'DELETE', 'PATCH', 'OPTIONS']
  19. }))
  20. app.use(koaBody());
  21. app.use(router.routes());
  22. const serverKey = fs.readFileSync(path.resolve(__dirname, '../security/cert.key'), 'utf-8')
  23. const serverCert = fs.readFileSync(path.resolve(__dirname, '../security/cert.pem'), 'utf-8')
  24. http2.createSecureServer({
  25. key: serverKey,
  26. cert: serverCert
  27. }, app.callback()).listen(config.port, () => {
  28. console.log(`[Connectors Server Started] running on port: ${config.port}.`)
  29. });
  30. } catch (error) {
  31. console.error('应用启动失败', error)
  32. }
  33. }
  34. start();