|
1 |
| -import { merge } from 'config-plus'; |
2 | 1 | import { json } from 'body-parser';
|
| 2 | +import { merge } from 'config-plus'; |
3 | 3 | import dotenv from 'dotenv';
|
4 | 4 | import express from 'express';
|
5 | 5 | import http from 'http';
|
6 | 6 | import { getBody } from 'logger-core';
|
7 | 7 | import { connectToDb } from 'mongodb-extension';
|
8 |
| -import { createContext } from './context'; |
9 |
| -import { ActiveMQConnection, Config } from './services/activemq'; |
10 | 8 | import { config, env } from './config';
|
| 9 | +import { createContext } from './context'; |
| 10 | +import { ActiveMQConnection } from './services/activemq'; |
11 | 11 |
|
12 | 12 | dotenv.config();
|
13 | 13 |
|
14 | 14 | const app = express();
|
15 | 15 | const conf = merge(config, process.env, env, process.env.ENV);
|
16 | 16 |
|
17 |
| -const port = conf.port; |
18 |
| - |
19 |
| - |
20 | 17 | app.use(json());
|
21 | 18 |
|
22 | 19 | connectToDb(
|
23 | 20 | `${conf.db.uri}`,
|
24 | 21 | `${conf.db.db}`
|
25 | 22 | ).then(async (db) => {
|
26 |
| - if ( |
27 |
| - !conf.amq.host || |
28 |
| - !conf.amq.port || |
29 |
| - !conf.amq.username || |
30 |
| - !conf.amq.password || |
31 |
| - !conf.amq.destinationName || |
32 |
| - !conf.amq.subscriptionName |
33 |
| - ) { |
34 |
| - throw new Error('config wrong!'); |
35 |
| - } |
36 |
| - const config: Config = { |
37 |
| - host: conf.amq.host, |
38 |
| - port: conf.amq.port, |
39 |
| - username: conf.amq.username, |
40 |
| - password: conf.amq.password, |
41 |
| - destinationName: conf.amq.destinationName, |
42 |
| - subscriptionName: conf.amq.subscriptionName, |
43 |
| - }; |
44 |
| - const amqConnection = new ActiveMQConnection(config); |
| 23 | + const amqConnection = new ActiveMQConnection(conf.amq); |
45 | 24 | const client = await amqConnection.connect();
|
46 |
| - const ctx = createContext(db, client, config); |
| 25 | + const ctx = createContext(db, client, conf.amq); |
47 | 26 | ctx.read(ctx.handle);
|
48 | 27 |
|
49 |
| - |
50 | 28 | http.createServer((req, res) => {
|
51 |
| - if (req.url === '/health') { |
52 |
| - ctx.health.check(req, res); |
53 |
| - } else if (req.url === '/send') { |
54 |
| - getBody(req).then((body: any) => { |
55 |
| - ctx |
56 |
| - .write(JSON.parse(body)) |
57 |
| - .then(() => { |
58 |
| - res.writeHead(200, { 'Content-Type': 'application/json' }); |
59 |
| - res.end(JSON.stringify({ message: 'message was produced' })); |
60 |
| - }) |
61 |
| - .catch((err) => { |
62 |
| - res.writeHead(500, { 'Content-Type': 'application/json' }); |
63 |
| - res.end(JSON.stringify({ error: err })); |
64 |
| - }); |
65 |
| - }).catch((err: any) => console.log(err)); |
66 |
| - } |
67 |
| - }) |
68 |
| - .listen(port, () => { |
69 |
| - console.log('Start server at port ' + port); |
70 |
| - }); |
| 29 | + if (req.url === '/health') { |
| 30 | + ctx.health.check(req, res); |
| 31 | + } else if (req.url === '/send') { |
| 32 | + getBody(req).then((body: any) => { |
| 33 | + ctx |
| 34 | + .write(JSON.parse(body)) |
| 35 | + .then(() => { |
| 36 | + res.writeHead(200, { 'Content-Type': 'application/json' }); |
| 37 | + res.end(JSON.stringify({ message: 'message was produced' })); |
| 38 | + }) |
| 39 | + .catch((err) => { |
| 40 | + res.writeHead(500, { 'Content-Type': 'application/json' }); |
| 41 | + res.end(JSON.stringify({ error: err })); |
| 42 | + }); |
| 43 | + }).catch((err: any) => console.log(err)); |
| 44 | + } |
| 45 | + }).listen(conf.port, () => { |
| 46 | + console.log('Start server at port ' + conf.port); |
| 47 | + }); |
71 | 48 | });
|
0 commit comments