Files
P42_wssGateway/configHelper.js
T
2025-09-11 20:58:43 +00:00

54 lines
1.7 KiB
JavaScript

const Ajv = require("ajv")
const confSchema = require('./configSchema.json')
let DynamoDBClient, GetItemCommand, marshall, unmarshall
module.exports = class configHelper {
constructor(options){
this.config = {}
this.localfile = options.localfile
this.fetchConfig = this.fetchConfigFile
this.refreshAccessRights = this.refreshAccessRightsFile
const ajv = new Ajv({
allowUnionTypes: true
})
this.configValidator = ajv.compile(confSchema)
}
isValidConfig(conf){
if(this.configValidator(conf)) return(true)
console.error('Invalid configuration: ', this.configValidator.errors)
return(false)
}
async fetchConfigFile(){
let curConfig = this.config
this.config = await require(this.localfile)
if(this.isValidConfig(this.config)) return(this.config)
console.error(this.isValidConfig.errors)
//revert if invalid conf
this.config = curConfig
}
async refreshAccessRightsDynamo(){
let ar = await this.dynamoGet('accessRights')
this.config.accessRights = ar
}
async refreshAccessRightsFile(){
delete require.cache[require.resolve(this.localfile)]
let tmp
try { tmp = require(this.localfile) }
catch(err) {
console.error('Error Reloading config !! (bad json?) => Keeping current accessRights !')
return
}
if(!tmp.accessRights) {
console.error('Error Reloading config !! (no accessRights !) => Keeping current accessRights !')
return
}
this.config.accessRights = tmp.accessRights
}
}