add api keys singleton json wrapper

This commit is contained in:
Adrien Marquès 2018-10-22 17:42:25 +02:00
parent 19d7743b1d
commit 1461254a85
1 changed files with 31 additions and 0 deletions

31
src/api/keys.ts Normal file
View File

@ -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() )
}
}