server.ts 1.14 KB
Newer Older
Rayyyyy's avatar
Rayyyyy committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
import express, { Express, Request, Response } from 'express';

import { SimpleBrowser } from './browser';
import config from './config';
import { logger } from './utils';

const session_history: Record<string, SimpleBrowser> = {};

const app: Express = express();

app.use(express.json());

app.post('/', async (req: Request, res: Response) => {
  const {
    session_id,
    action,
  }: {
    session_id: string;
    action: string;
  } = req.body;
  logger.info(`session_id: ${session_id}`);
  logger.info(`action: ${action}`);
Rayyyyy's avatar
Rayyyyy committed
23

Rayyyyy's avatar
Rayyyyy committed
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
  if (!session_history[session_id]) {
    session_history[session_id] = new SimpleBrowser();
  }

  const browser = session_history[session_id];

  try {
    res.json(await browser.action(action));
  } catch (err) {
    logger.error(err);
    res.status(400).json(err);
  }
})

process.on('SIGINT', () => {
  process.exit(0);
});

process.on('uncaughtException', e => {
  logger.error(e);
});

const { HOST, PORT } = config;

(async () => {
  app.listen(PORT, HOST, () => {
    logger.info(`⚡️[server]: Server is running at http://${HOST}:${PORT}`);
    try {
      (<any>process).send('ready');
    } catch (err) {}
  });
})();