Skip to content

Commit 233a34f

Browse files
committed
Added a method to initialize the influx database
1 parent cce97ce commit 233a34f

File tree

2 files changed

+96
-4
lines changed

2 files changed

+96
-4
lines changed

back-end/database/db_init.js

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
const path = require('path'); // Import the path module
2+
const ENV_FILE_PATH = path.resolve(__dirname, '../.env');
3+
4+
const dotenv = require('dotenv');
5+
dotenv.config({ path: ENV_FILE_PATH});
6+
7+
const axios = require('axios');
8+
const fs = require('fs');
9+
10+
11+
const API_TOKEN_ENV_VAR = 'INFLUXDB_API_TOKEN';
12+
console.log("File path : " + ENV_FILE_PATH);
13+
14+
15+
const adminUsername = process.env.ADMIN_USERNAME;
16+
const influxHost = 'http://influxdb:8086';
17+
const adminPassword = process.env.ADMIN_PASSWORD;
18+
const org = process.env.ORG_NAME;
19+
const bucket = process.env.BUCKET_NAME;
20+
21+
console.log(adminUsername)
22+
console.log(adminPassword)
23+
console.log(org)
24+
console.log(bucket)
25+
26+
async function setupInfluxDB() {
27+
try {
28+
// Step 1: Set up the initial admin user, org, and bucket
29+
30+
const setupResponse = await axios.post(`${influxHost}/api/v2/setup`, {
31+
username: adminUsername,
32+
password: adminPassword,
33+
org: org,
34+
bucket: bucket,
35+
retentionRules: [{
36+
type: 'expire',
37+
everySeconds: 0 // Retention policy (0 means infinite)
38+
}]
39+
});
40+
41+
console.log('Setup Response:', setupResponse.data);
42+
43+
// Step 2: Generate an API token
44+
const tokenResponse = await axios.post(`${influxHost}/api/v2/authorizations`, {
45+
orgID: setupResponse.data.org.id,
46+
permissions: [
47+
{
48+
action: 'read',
49+
resource: { type: 'buckets' }
50+
},
51+
{
52+
action: 'write',
53+
resource: { type: 'buckets' }
54+
}
55+
],
56+
description: "Regular user token"
57+
}, {
58+
headers: {
59+
'Authorization': `Token ${setupResponse.data.auth.token}`
60+
}
61+
});
62+
63+
const apiToken = tokenResponse.data.token;
64+
console.log('API Token:', apiToken);
65+
fs.appendFileSync(ENV_FILE_PATH, `\n${API_TOKEN_ENV_VAR}=${apiToken}\n`);
66+
dotenv.config({ path: ENV_FILE_PATH});
67+
68+
} catch (error) {
69+
console.error('Error setting up InfluxDB:', error.response ? error.response.data : error.message);
70+
}
71+
}
72+
73+
74+
module.exports = { setupInfluxDB }

back-end/index.js

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,28 @@ const mqttClient = require('./mqtt/subscriber');
99
const CONFIG = require('../config.json');
1010
const cron = require('node-cron');
1111

12-
const frontEndHost = CONFIG["front-end"].host;
13-
const frontEndPort = CONFIG["front-end"].port;
14-
const backEndHost = CONFIG['back-end'].host
15-
const backEndPort = CONFIG['back-end'].port
12+
const frontEndHost = 'front-end';
13+
const frontEndPort = 3001;
14+
15+
16+
const backEndHost = 'back-end';
17+
const backEndPort = 3000;
18+
19+
20+
// Check and Setup InfluxDB
21+
(async () => {
22+
let result = process.env.INFLUXDB_API_TOKEN;
23+
24+
if (!result) {
25+
try {
26+
await setupInfluxDB(); // Ensure setupInfluxDB completes before proceeding
27+
} catch (error) {
28+
console.error("Error setting up InfluxDB:", error);
29+
}
30+
} else {
31+
console.log("A user exists");
32+
}
33+
})();
1634

1735
let previous_sleep_value = null;
1836

0 commit comments

Comments
 (0)