Skip to content

Commit 24a7cf5

Browse files
committed
Refactor code
1 parent c911b54 commit 24a7cf5

File tree

2 files changed

+42
-65
lines changed

2 files changed

+42
-65
lines changed

src/config.ts

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,38 +3,38 @@ export const config = {
33
secure: false,
44
template: true,
55
allow: {
6-
origin: "http://localhost:3000",
7-
credentials: "true",
8-
methods: "GET,PUT,POST,DELETE,OPTIONS,PATCH",
6+
origin: 'http://localhost:3000',
7+
credentials: 'true',
8+
methods: 'GET,PUT,POST,DELETE,OPTIONS,PATCH',
99
headers:
10-
"Access-Control-Allow-Headers, Authorization, Origin,Accept, X-Requested-With, Content-Type, Access-Control-Request-Method, Access-Control-Request-Headers",
10+
'Access-Control-Allow-Headers, Authorization, Origin,Accept, X-Requested-With, Content-Type, Access-Control-Request-Method, Access-Control-Request-Headers',
1111
},
1212
log: {
13-
level: "debug",
13+
level: 'debug',
1414
map: {
15-
time: "@timestamp",
16-
msg: "message",
15+
time: '@timestamp',
16+
msg: 'message',
1717
},
1818
db: true,
1919
},
2020
middleware: {
2121
log: true,
22-
skips: "health,log",
23-
request: "request",
24-
status: "status",
25-
size: "size",
22+
skips: 'health,log',
23+
request: 'request',
24+
status: 'status',
25+
size: 'size',
2626
},
2727
db: {
28-
uri: "mongodb://localhost:27017",
29-
db: "masterdata2",
28+
uri: 'mongodb://localhost:27017',
29+
db: 'masterdata2',
3030
},
3131
amq: {
32-
host: "localhost",
32+
host: 'localhost',
3333
port: 61613,
34-
username: "admin",
35-
password: "admin",
36-
destinationName: "topic",
37-
subscriptionName: "sub-user",
34+
username: 'admin',
35+
password: 'admin',
36+
destinationName: 'topic',
37+
subscriptionName: 'sub-user',
3838
}
3939
};
4040

src/index.ts

Lines changed: 24 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,71 +1,48 @@
1-
import { merge } from 'config-plus';
21
import { json } from 'body-parser';
2+
import { merge } from 'config-plus';
33
import dotenv from 'dotenv';
44
import express from 'express';
55
import http from 'http';
66
import { getBody } from 'logger-core';
77
import { connectToDb } from 'mongodb-extension';
8-
import { createContext } from './context';
9-
import { ActiveMQConnection, Config } from './services/activemq';
108
import { config, env } from './config';
9+
import { createContext } from './context';
10+
import { ActiveMQConnection } from './services/activemq';
1111

1212
dotenv.config();
1313

1414
const app = express();
1515
const conf = merge(config, process.env, env, process.env.ENV);
1616

17-
const port = conf.port;
18-
19-
2017
app.use(json());
2118

2219
connectToDb(
2320
`${conf.db.uri}`,
2421
`${conf.db.db}`
2522
).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);
4524
const client = await amqConnection.connect();
46-
const ctx = createContext(db, client, config);
25+
const ctx = createContext(db, client, conf.amq);
4726
ctx.read(ctx.handle);
4827

49-
5028
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+
});
7148
});

0 commit comments

Comments
 (0)