diff --git a/src/index.js b/src/index.js index 078a99d..5fee14a 100755 --- a/src/index.js +++ b/src/index.js @@ -7,51 +7,50 @@ const config_router = require('./router_config').config_router const dotenv = require('dotenv') const express = require('express') -function PurpleApi(opts = {}) -{ - if (!(this instanceof PurpleApi)) - return new PurpleApi(opts) - - const queue = {} - const db = lmdb.open({ path: '.' }) - const translations = db.openDB('translations') - const accounts = db.openDB('accounts') - const dbs = {translations, accounts} - const router = express() - - // translation data - this.translation = {queue} - - this.db = db - this.dbs = dbs - this.opts = opts - this.router = router - - return this +function PurpleApi(opts = {}) { + if (!(this instanceof PurpleApi)) + return new PurpleApi(opts) + + const queue = {} + const db = lmdb.open({ path: '.' }) + const translations = db.openDB('translations') + const accounts = db.openDB('accounts') + const dbs = { translations, accounts } + const router = express() + + // translation data + this.translation = { queue } + + this.db = db + this.dbs = dbs + this.opts = opts + this.router = router + + return this } PurpleApi.prototype.serve = async function pt_serve(port) { - const theport = this.opts.port || port || 8989 + const theport = this.opts.port || port || 8989 this.router.listen(theport) - console.log(`Server listening on ${theport}`) + console.log(`Server listening on ${theport}`) } PurpleApi.prototype.close = async function pt_close() { - // close lmdb - await this.db.close() + // close lmdb + await this.db.close() } PurpleApi.prototype.register_routes = function pt_register_routes() { - config_router(this) + config_router(this) } module.exports = PurpleApi if (require.main == module) { - // Load .env file if it exists - dotenv.config() - - let translate = new PurpleApi() - translate.register_routes() - translate.serve(process.env.PORT) + // Load .env file if it exists + dotenv.config() + + let translate = new PurpleApi() + translate.register_routes() + translate.serve(process.env.PORT) } diff --git a/src/user_management.js b/src/user_management.js index 0958de3..c7b7abb 100644 --- a/src/user_management.js +++ b/src/user_management.js @@ -1,45 +1,45 @@ const { current_time } = require('./utils') function check_account(api, pubkey) { - const id = Buffer.from(pubkey) - const account = api.dbs.accounts.get(id) + const id = Buffer.from(pubkey) + const account = api.dbs.accounts.get(id) - if (!account) - return { ok: false, message: 'Account not found' } + if (!account) + return { ok: false, message: 'Account not found' } - if (!account.expiry || current_time() >= account.expiry) - return { ok: false, message: 'Account expired' } + if (!account.expiry || current_time() >= account.expiry) + return { ok: false, message: 'Account expired' } - return { ok: true, message: null } + return { ok: true, message: null } } function create_account(api, pubkey, expiry) { - const id = Buffer.from(pubkey) - const account = api.dbs.accounts.get(id) + const id = Buffer.from(pubkey) + const account = api.dbs.accounts.get(id) - if (account) - return { request_error: 'account already exists' } + if (account) + return { request_error: 'account already exists' } - const new_account = { - pubkey: pubkey, - created_at: current_time(), - expiry: expiry, - } + const new_account = { + pubkey: pubkey, + created_at: current_time(), + expiry: expiry, + } - api.dbs.accounts.put(id, new_account) - return { account: new_account, request_error: null } + api.dbs.accounts.put(id, new_account) + return { account: new_account, request_error: null } } function get_account_info_payload(account) { - if (!account) - return null - - return { - pubkey: account.pubkey, - created_at: account.created_at, - expiry: account.expiry ? account.expiry : null, - active: (account.expiry && current_time() < account.expiry) ? true : false, - } + if (!account) + return null + + return { + pubkey: account.pubkey, + created_at: account.created_at, + expiry: account.expiry ? account.expiry : null, + active: (account.expiry && current_time() < account.expiry) ? true : false, + } } module.exports = { check_account, create_account, get_account_info_payload }