From 1461254a859d22bf08906f145aed81ff7a1482c9 Mon Sep 17 00:00:00 2001 From: xdrm-brackets Date: Mon, 22 Oct 2018 17:42:25 +0200 Subject: [PATCH] add api keys singleton json wrapper --- src/api/keys.ts | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 src/api/keys.ts diff --git a/src/api/keys.ts b/src/api/keys.ts new file mode 100644 index 0000000..5303b33 --- /dev/null +++ b/src/api/keys.ts @@ -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() ) + } + + +} \ No newline at end of file