add api keys singleton json wrapper
This commit is contained in:
parent
19d7743b1d
commit
1461254a85
|
@ -0,0 +1,31 @@
|
|||
import { openSync, readFileSync } from "fs";
|
||||
import { O_RDONLY } from "constants";
|
||||
import { join } from "path";
|
||||
|
||||
export default class Key{
|
||||
|
||||
// Static
|
||||
static _singleton?: Key;
|
||||
static _config: any;
|
||||
static get(key: string) : any{
|
||||
|
||||
if( Key._singleton == null )
|
||||
Key._singleton = new Key();
|
||||
|
||||
return Key._config[key] || null;
|
||||
}
|
||||
|
||||
|
||||
// instance
|
||||
private constructor(){
|
||||
let path = join(__dirname, '../../cnf/api_keys.json')
|
||||
let fd = openSync(path, O_RDONLY)
|
||||
|
||||
if( fd < 0 )
|
||||
throw new Error(`cannot open file ${path}`)
|
||||
|
||||
Key._config = JSON.parse( readFileSync(fd).toString() )
|
||||
}
|
||||
|
||||
|
||||
}
|
Loading…
Reference in New Issue