Skip to content

Commit b897179

Browse files
committed
refactor: centralize configuration using envConfig for port and server URL
1 parent 359fd8b commit b897179

File tree

4 files changed

+53
-30
lines changed

4 files changed

+53
-30
lines changed

src/index.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
import app from "./app";
2-
import axios from "axios"
32
import "./services/zeromqService";
3+
import { ENV_CONFIG } from "./utils/envConfig";
44

5-
const PORT = process.env.PORT || 4000;
6-
7-
app.listen(PORT, () => {
8-
console.log(`Microservice running on port ${PORT}`);
5+
app.listen(ENV_CONFIG.PORT, () => {
6+
console.log(`Microservice running on port ${ENV_CONFIG.PORT}`);
97
});

src/routes/integrations.ts

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
import { Router, Request, Response } from "express";
1+
import { Router, Request, Response } from "express";
2+
import { ENV_CONFIG } from "../utils/envConfig";
23

34
const router = Router();
45

5-
router.get("/integration.json", (req: Request, res: Response) => {
6+
router.get("/integration-json", (req: Request, res: Response) => {
67
const integrationData = {
78
data: {
89
date: {
@@ -12,9 +13,9 @@ router.get("/integration.json", (req: Request, res: Response) => {
1213
descriptions: {
1314
app_description:
1415
"Analyzes your codebase for static errors and reports them to Telex channels with prioritized error classification.",
15-
app_logo: "https://example.com/path-to-code-error-agent-logo.png",
16+
app_logo: "https://example.com/path-to-code-error-agent-logo.png",
1617
app_name: "Code Error Agent",
17-
app_url: "https://code-error-agent-production.up.railway.app/",
18+
app_url: ENV_CONFIG.SERVER_URL,
1819
background_color: "#FF4444",
1920
},
2021
integration_category: "AI & Machine Learning",
@@ -53,14 +54,12 @@ router.get("/integration.json", (req: Request, res: Response) => {
5354
default: "*/15 * * * *",
5455
},
5556
],
56-
tick_url:
57-
"https://code-error-agent-production.up.railway.app/tick",
58-
target_url:
59-
"https://code-error-agent-production.up.railway.app/webhook",
57+
tick_url: `${ENV_CONFIG.SERVER_URL}/tick`,
58+
target_url: `${ENV_CONFIG.SERVER_URL}/webhook`,
6059
},
6160
};
6261

6362
res.json(integrationData);
6463
});
6564

66-
export default router;
65+
export default router;

src/services/zeromqService.ts

Lines changed: 34 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
import * as zmq from "zeromq";
2-
import {
3-
ProcessedError,
4-
} from "../controllers/errorController";
2+
import { ProcessedError } from "../controllers/errorController";
53
import axios from "axios";
4+
import { ENV_CONFIG } from "../utils/envConfig";
65

76
const webhookUrl = "https://ping.telex.im/v1/webhooks";
87

@@ -11,9 +10,16 @@ async function initializeServer() {
1110
const publishSocket = new zmq.Publisher();
1211

1312
try {
14-
await replySocket.bind("tcp://code-error-microservice.onrender.com:3030");
15-
await publishSocket.bind("tcp://code-error-microservice.onrender.com:3031");
16-
console.log("ZeroMQ server bound to ports 3030 (Reply) and 3031 (Publish)");
13+
const zeroMqBindHost = "0.0.0.0";
14+
const zeroMqBindPortPublish = ENV_CONFIG.PORT + 1;
15+
const zeroMqBindPortReply = zeroMqBindPortPublish + 1;
16+
await replySocket.bind(`tcp://${zeroMqBindHost}:${zeroMqBindPortReply}`);
17+
await publishSocket.bind(
18+
`tcp://${zeroMqBindHost}:${zeroMqBindPortPublish}`
19+
);
20+
console.log(
21+
`ZeroMQ server bound to ports ${zeroMqBindPortReply} (Reply) and ${zeroMqBindPortPublish} (Publish)`
22+
);
1723

1824
const serverPublish = async (message: string) => {
1925
await publishSocket.send(["update", message]);
@@ -29,27 +35,35 @@ async function initializeServer() {
2935
} catch (error) {
3036
parsedMessage = msg.toString() as unknown as ProcessedError;
3137
console.error("Failed to parse message:", error);
32-
await replySocket.send(JSON.stringify({ status: "error", message: "Invalid message format" }));
38+
await replySocket.send(
39+
JSON.stringify({ status: "error", message: "Invalid message format" })
40+
);
3341
continue;
3442
}
3543
if (!parsedMessage || !parsedMessage.channelId || !parsedMessage.errors) {
3644
console.warn("Invalid message format");
37-
await replySocket.send(JSON.stringify({ status: "error", message: "Invalid message format" }));
45+
await replySocket.send(
46+
JSON.stringify({ status: "error", message: "Invalid message format" })
47+
);
3848
continue;
3949
}
4050

41-
const errorSummary = parsedMessage.errors.map(err => ({
51+
const errorSummary = parsedMessage.errors.map((err) => ({
4252
message: err.message,
43-
stack: err.stack
53+
stack: err.stack,
4454
}));
4555

4656
const message = `
4757
Errors:
48-
${errorSummary.map((err, index) => `
58+
${errorSummary
59+
.map(
60+
(err, index) => `
4961
Error ${index + 1}:
5062
Message: ${err.message}
5163
Stack: ${err.stack}
52-
`).join('\n')}
64+
`
65+
)
66+
.join("\n")}
5367
`.trim();
5468

5569
const telexPayload = {
@@ -74,16 +88,20 @@ async function initializeServer() {
7488
await replySocket.send(JSON.stringify({ status: "success" }));
7589
} catch (error) {
7690
console.error("Failed to send to webhook:", error);
77-
await replySocket.send(JSON.stringify({ status: "error", message: "Failed to send to webhook" }));
91+
await replySocket.send(
92+
JSON.stringify({
93+
status: "error",
94+
message: "Failed to send to webhook",
95+
})
96+
);
7897
}
7998
}
8099

81100
return { serverPublish };
82101
} catch (error) {
83102
console.error("ZeroMQ server error:", error);
84103
throw error;
85-
}
86-
104+
}
87105
}
88106

89-
export const zeromqClient = initializeServer();
107+
export const zeromqClient = initializeServer();

src/utils/envConfig.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import { config } from "dotenv";
2+
3+
config();
4+
5+
export const ENV_CONFIG = {
6+
PORT: Number(process.env.PORT || 4000),
7+
SERVER_URL: process.env.SERVER_URL || "http://localhost:4000",
8+
};

0 commit comments

Comments
 (0)