Hansimov commited on
Commit
d7a32b8
β€’
1 Parent(s): a7f8b93

:zap: [Enhance] Use template for agents and endpoints json

Browse files
.gitignore CHANGED
@@ -1,6 +1,9 @@
 
1
  node_modules
2
  package-lock.json
3
- .vscode
4
- live.js
5
- # configs/endpoints.json
6
- configs/secrets.json
 
 
 
1
+ .vscode
2
  node_modules
3
  package-lock.json
4
+
5
+ configs/endpoints.json
6
+ configs/agents.json
7
+ configs/secrets.json
8
+
9
+ live.js
configs/{agents.json β†’ agents_template.json} RENAMED
File without changes
configs/{endpoints.json β†’ endpoints_template.json} RENAMED
File without changes
server.js CHANGED
@@ -13,6 +13,13 @@ app.get("/", (req, res) => {
13
  res.sendFile(path.join(__dirname + "/index.html"));
14
  });
15
 
 
 
 
 
 
 
 
16
  app.get("/endpoints", async (req, res) => {
17
  try {
18
  let endpoints_configs_path = path.join(
@@ -20,6 +27,14 @@ app.get("/endpoints", async (req, res) => {
20
  "configs",
21
  "endpoints.json"
22
  );
 
 
 
 
 
 
 
 
23
  const data = await fs.readFile(endpoints_configs_path, "utf-8");
24
  const local_points = JSON.parse(data);
25
  res.json(local_points);
@@ -38,6 +53,14 @@ app.get("/agents", async (req, res) => {
38
  "configs",
39
  "agents.json"
40
  );
 
 
 
 
 
 
 
 
41
  const data = await fs.readFile(agents_configs_path, "utf-8");
42
  const local_agents = JSON.parse(data);
43
  res.json(local_agents);
 
13
  res.sendFile(path.join(__dirname + "/index.html"));
14
  });
15
 
16
+ async function is_file_exists(file) {
17
+ return fs
18
+ .access(file, fs.constants.F_OK)
19
+ .then(() => true)
20
+ .catch(() => false);
21
+ }
22
+
23
  app.get("/endpoints", async (req, res) => {
24
  try {
25
  let endpoints_configs_path = path.join(
 
27
  "configs",
28
  "endpoints.json"
29
  );
30
+ if (!(await is_file_exists(endpoints_configs_path))) {
31
+ endpoints_configs_path = path.join(
32
+ __dirname,
33
+ "configs",
34
+ "endpoints_template.json"
35
+ );
36
+ console.log("agents.json not found. Use template.");
37
+ }
38
  const data = await fs.readFile(endpoints_configs_path, "utf-8");
39
  const local_points = JSON.parse(data);
40
  res.json(local_points);
 
53
  "configs",
54
  "agents.json"
55
  );
56
+ if (!(await is_file_exists(agents_configs_path))) {
57
+ agents_configs_path = path.join(
58
+ __dirname,
59
+ "configs",
60
+ "agents_template.json"
61
+ );
62
+ console.log("endpoints.json not found. Use template.");
63
+ }
64
  const data = await fs.readFile(agents_configs_path, "utf-8");
65
  const local_agents = JSON.parse(data);
66
  res.json(local_agents);