Skip to content

Commit

Permalink
core: frontend: vue.config: Add getBlueOSReachableAddress
Browse files Browse the repository at this point in the history
Signed-off-by: Patrick José Pereira <[email protected]>
  • Loading branch information
patrickelectric authored and Williangalvani committed Apr 21, 2023
1 parent 3a12f83 commit 0b62aaa
Showing 1 changed file with 46 additions and 1 deletion.
47 changes: 46 additions & 1 deletion core/frontend/vue.config.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,20 @@
/* eslint-disable */
const { name } = require('./package.json')
const { PRIMARY } = require('./src/assets/colors/default')
const { StatusCodes } = require('http-status-codes')
const http = require('http')

process.env.PROJECT_NAME = name
process.env.VUE_APP_BUILD_DATE = new Date().toLocaleString()
const SERVER_ADDRESS = process.env.BLUEOS_ADDRESS ?? 'http://blueos.local/'
const DEFAULT_ADDRESS = 'http://blueos.local/'
const SEARCHABLE_MDNS_ADDRESS = [
DEFAULT_ADDRESS,
'http://blueos-avahi.local/',
'http://blueos-wifi.local/',
'http://blueos-hotspot.local/',
]

const SERVER_ADDRESS = process.env.BLUEOS_ADDRESS ?? getBlueOSReachableAddress() ?? DEFAULT_ADDRESS

module.exports = {
devServer: {
Expand Down Expand Up @@ -135,3 +145,38 @@ module.exports = {
})
}
}

async function checkUrlReachable(url) {
return new Promise((resolve, reject) => {
const req = http.get(url, (res) => {
if (res.statusCode >= StatusCodes.OK && res.statusCode < StatusCodes.BAD_REQUEST) {
resolve({ reachable: true, statusCode: res.statusCode });
} else {
resolve({ reachable: false, statusCode: res.statusCode });
}
});

req.on('error', (err) => {
reject(err);
});

req.end();
});
}

async function getBlueOSReachableAddress() {
const promises = SEARCHABLE_MDNS_ADDRESS.map((url) => checkUrlReachable(url));
const results = await Promise.allSettled(promises);

const address = results
.map((result, index) => result?.value?.reachable === true ? SEARCHABLE_MDNS_ADDRESS[index] : undefined)
.filter((address) => address !== undefined)
?.[0]

if (address) {
// The new line is necessary to show the value while running yarn
console.log(`Found BlueOS on: ${address}\n`)
}

return address
}

0 comments on commit 0b62aaa

Please sign in to comment.