-
Notifications
You must be signed in to change notification settings - Fork 3
/
server.js
120 lines (102 loc) · 3.66 KB
/
server.js
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
const dotenv = require("dotenv");
const express = require("express");
const path = require("path");
const fetch = require("node-fetch");
const app = express();
dotenv.config();
// https://stackoverflow.com/questions/8817423/why-is-dirname-not-defined-in-node-repl
const dirname = path.resolve();
app.use(express.static(path.join(dirname, "build")));
const moesifManagementToken = process.env.MOESIF_MANAGEMENT_TOKEN;
const templateWorkspaceId = process.env.MOESIF_TEMPLATE_WORKSPACE_ID;
const moesifApiEndPoint =
process.env.MOESIF_API_ENDPOINT || "https://api.moesif.com";
if (!moesifManagementToken) {
console.error(
"No MOESIF_MANAGEMENT_TOKEN found. Please create an .env file with MOESIF_MANAGEMENT_TOKEN & MOESIF_TEMPLATE_WORKSPACE_ID."
);
}
if (!templateWorkspaceId) {
console.error(
"No MOESIF_TEMPLATE_WORKSPACE_ID found. Please create an .env file with MOESIF_MANAGEMENT_TOKEN & MOESIF_TEMPLATE_WORKSPACE_ID."
);
}
app.get("/embed-dash(/:userId)", function (req, res) {
const userId = req.params.userId;
// be sure to authenticate your user here.
// for purpose of this example, do not do any user authentication.
// Here you want to customize the template fields for each one of your users.
// for example, here we configured so that API events
// that are filtered by the user_id field.
const templateData = {
template: {
values: {
user_id: userId
// your template workspace may have
// other dynamic fields. All dynamic fields will be required to be filled here.
}
}
};
// below is required if the templated workspace has dynamic time range set to true.
// and from and to is the dateRange of events allowed in the Sandbox
const from = req.query.from;
const to = req.query.to;
// and from and to should be ISO timestamps if it is dynamic
if (from && to) {
templateData.template.from = from;
templateData.template.to = to;
}
// Set your desired expiration for the generated workspace token.
// Moesif's recommendation is to match or be larger than your user's session time while keeping time period less than 30 days.
const tomorrow = new Date();
tomorrow.setDate(tomorrow.getDate() + 1);
const expiration = tomorrow.toISOString();
console.log(
`now requests access token for template workspace ${templateWorkspaceId}, setting the expiration to ${expiration} with templateData as ${JSON.stringify(templateData)}`
);
const finalApiEndPoint = moesifApiEndPoint.endsWith("/api")
? moesifApiEndPoint + "/portal/~/workspaces/"
: moesifApiEndPoint + "/v1/portal/~/workspaces/";
const finalUrl =
finalApiEndPoint +
templateWorkspaceId +
"/access_token" +
"?expiration=" +
expiration;
console.log('finalUrl: ' + finalUrl);
fetch(finalUrl, {
method: "POST",
headers: {
"Content-Type": "application/json",
Authorization: "Bearer " + moesifManagementToken
},
body: JSON.stringify(templateData)
})
.then((response) => {
if (response.ok) {
return response;
} else {
console.log("Api call to moesif not successful. sever response is");
console.error(response.statusText);
console.log(response);
throw Error(response.statusText);
}
})
.then((response) => {
return response.json();
})
.then((info) => {
console.log(`got token back sending back to frontend`);
res.json(info);
})
.catch((err) => {
console.log(err);
res.status(500).send({
error: "something wrong"
});
});
});
app.get("/", function (req, res) {
res.sendFile(path.join(__dirname, "build", "index.html"));
});
app.listen(process.env.PORT || 3050);