-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
69 lines (60 loc) · 1.77 KB
/
index.js
File metadata and controls
69 lines (60 loc) · 1.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
const express = require("express");
const path = require("path");
const { init } = require("@launchdarkly/node-server-sdk");
const {
Observability,
LDObserve,
} = require("@launchdarkly/observability-node");
const dotenv = require("dotenv");
dotenv.config();
const app = express();
const client = init(process.env.LAUNCHDARKLY_SDK_KEY, {
plugins: [
new Observability({
serviceName: "expressjs-launchdarkly-starter",
}),
],
});
const TIMEOUT_IN_SECONDS = 5;
app.get("/", (req, res) => {
const { span } = LDObserve.startWithHeaders("homepage.render", req.headers);
const context = {
kind: "user",
name: "tilde",
key: "context-key-123abc",
};
client.waitForInitialization({ timeout: TIMEOUT_IN_SECONDS }).then(() => {
client
.variation("show-holiday-styling", context, false)
.then((showHolidayStyling) => {
LDObserve.setAttributes({
"show-holiday-styling": showHolidayStyling,
});
if (showHolidayStyling) {
res.sendFile(path.join(__dirname, "public", "holiday.html"));
} else {
res.sendFile(path.join(__dirname, "public", "enterprise.html"));
}
})
.catch((err) => {
console.error("Error evaluating LaunchDarkly flag:", err);
res.status(500).send("Internal server error");
});
});
span.end();
});
const port = 3000;
const server = app.listen(port, function (err) {
if (err) console.log("Error in server setup");
console.log(`Server listening on http://localhost:${port}`);
});
// Graceful shutdown
process.on("SIGTERM", () => {
console.log("SIGTERM received, shutting down gracefully");
server.close(() => {
observability.flush();
observability.close();
console.log("Process terminated");
process.exit(0);
});
});