Added configurable store for local-storage or redis
This commit is contained in:
parent
be1495d8b3
commit
c51ed1ba8f
1
.env
1
.env
@ -9,4 +9,5 @@ EO_API_UDG_URL = unified-data-gateway/default/graphql
|
|||||||
JWT_SECRET = 12356789
|
JWT_SECRET = 12356789
|
||||||
PORT = 3000
|
PORT = 3000
|
||||||
|
|
||||||
|
STORE = local-storage
|
||||||
REDIS_URL = redis://localhost:6379
|
REDIS_URL = redis://localhost:6379
|
||||||
27
package-lock.json
generated
27
package-lock.json
generated
@ -25,6 +25,7 @@
|
|||||||
"local-storage": "^2.0.0",
|
"local-storage": "^2.0.0",
|
||||||
"morgan": "^1.10.0",
|
"morgan": "^1.10.0",
|
||||||
"nodemon": "^3.0.1",
|
"nodemon": "^3.0.1",
|
||||||
|
"promise": "^8.3.0",
|
||||||
"query-string": "^7.1.1",
|
"query-string": "^7.1.1",
|
||||||
"redis": "^4.6.7",
|
"redis": "^4.6.7",
|
||||||
"winston": "^3.9.0"
|
"winston": "^3.9.0"
|
||||||
@ -483,6 +484,11 @@
|
|||||||
"url": "https://github.com/sponsors/ljharb"
|
"url": "https://github.com/sponsors/ljharb"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
"node_modules/asap": {
|
||||||
|
"version": "2.0.6",
|
||||||
|
"resolved": "https://registry.npmjs.org/asap/-/asap-2.0.6.tgz",
|
||||||
|
"integrity": "sha512-BSHWgDSAiKs50o2Re8ppvp3seVHXSRM44cdSsT9FfNEUUZLOGWVCsiWaRPWM1Znn+mqZ1OfVZ3z3DWEzSp7hRA=="
|
||||||
|
},
|
||||||
"node_modules/asn1.js": {
|
"node_modules/asn1.js": {
|
||||||
"version": "5.4.1",
|
"version": "5.4.1",
|
||||||
"resolved": "https://registry.npmjs.org/asn1.js/-/asn1.js-5.4.1.tgz",
|
"resolved": "https://registry.npmjs.org/asn1.js/-/asn1.js-5.4.1.tgz",
|
||||||
@ -3128,6 +3134,14 @@
|
|||||||
"url": "https://github.com/prettier/prettier?sponsor=1"
|
"url": "https://github.com/prettier/prettier?sponsor=1"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
"node_modules/promise": {
|
||||||
|
"version": "8.3.0",
|
||||||
|
"resolved": "https://registry.npmjs.org/promise/-/promise-8.3.0.tgz",
|
||||||
|
"integrity": "sha512-rZPNPKTOYVNEEKFaq1HqTgOwZD+4/YHS5ukLzQCypkj+OkYx7iv0mA91lJlpPPZ8vMau3IIGj5Qlwrx+8iiSmg==",
|
||||||
|
"dependencies": {
|
||||||
|
"asap": "~2.0.6"
|
||||||
|
}
|
||||||
|
},
|
||||||
"node_modules/proxy-addr": {
|
"node_modules/proxy-addr": {
|
||||||
"version": "2.0.7",
|
"version": "2.0.7",
|
||||||
"resolved": "https://registry.npmjs.org/proxy-addr/-/proxy-addr-2.0.7.tgz",
|
"resolved": "https://registry.npmjs.org/proxy-addr/-/proxy-addr-2.0.7.tgz",
|
||||||
@ -4345,6 +4359,11 @@
|
|||||||
"es-shim-unscopables": "^1.0.0"
|
"es-shim-unscopables": "^1.0.0"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
"asap": {
|
||||||
|
"version": "2.0.6",
|
||||||
|
"resolved": "https://registry.npmjs.org/asap/-/asap-2.0.6.tgz",
|
||||||
|
"integrity": "sha512-BSHWgDSAiKs50o2Re8ppvp3seVHXSRM44cdSsT9FfNEUUZLOGWVCsiWaRPWM1Znn+mqZ1OfVZ3z3DWEzSp7hRA=="
|
||||||
|
},
|
||||||
"asn1.js": {
|
"asn1.js": {
|
||||||
"version": "5.4.1",
|
"version": "5.4.1",
|
||||||
"resolved": "https://registry.npmjs.org/asn1.js/-/asn1.js-5.4.1.tgz",
|
"resolved": "https://registry.npmjs.org/asn1.js/-/asn1.js-5.4.1.tgz",
|
||||||
@ -6295,6 +6314,14 @@
|
|||||||
"integrity": "sha512-tdN8qQGvNjw4CHbY+XXk0JgCXn9QiF21a55rBe5LJAU+kDyC4WQn4+awm2Xfk2lQMk5fKup9XgzTZtGkjBdP9Q==",
|
"integrity": "sha512-tdN8qQGvNjw4CHbY+XXk0JgCXn9QiF21a55rBe5LJAU+kDyC4WQn4+awm2Xfk2lQMk5fKup9XgzTZtGkjBdP9Q==",
|
||||||
"dev": true
|
"dev": true
|
||||||
},
|
},
|
||||||
|
"promise": {
|
||||||
|
"version": "8.3.0",
|
||||||
|
"resolved": "https://registry.npmjs.org/promise/-/promise-8.3.0.tgz",
|
||||||
|
"integrity": "sha512-rZPNPKTOYVNEEKFaq1HqTgOwZD+4/YHS5ukLzQCypkj+OkYx7iv0mA91lJlpPPZ8vMau3IIGj5Qlwrx+8iiSmg==",
|
||||||
|
"requires": {
|
||||||
|
"asap": "~2.0.6"
|
||||||
|
}
|
||||||
|
},
|
||||||
"proxy-addr": {
|
"proxy-addr": {
|
||||||
"version": "2.0.7",
|
"version": "2.0.7",
|
||||||
"resolved": "https://registry.npmjs.org/proxy-addr/-/proxy-addr-2.0.7.tgz",
|
"resolved": "https://registry.npmjs.org/proxy-addr/-/proxy-addr-2.0.7.tgz",
|
||||||
|
|||||||
@ -32,6 +32,7 @@
|
|||||||
"local-storage": "^2.0.0",
|
"local-storage": "^2.0.0",
|
||||||
"morgan": "^1.10.0",
|
"morgan": "^1.10.0",
|
||||||
"nodemon": "^3.0.1",
|
"nodemon": "^3.0.1",
|
||||||
|
"promise": "^8.3.0",
|
||||||
"query-string": "^7.1.1",
|
"query-string": "^7.1.1",
|
||||||
"redis": "^4.6.7",
|
"redis": "^4.6.7",
|
||||||
"winston": "^3.9.0"
|
"winston": "^3.9.0"
|
||||||
|
|||||||
@ -78,13 +78,13 @@ router.all("/", (req, res, next) => {
|
|||||||
`Adding agent ${verified.sub} to store for environment ${authDataString}}`
|
`Adding agent ${verified.sub} to store for environment ${authDataString}}`
|
||||||
);
|
);
|
||||||
store
|
store
|
||||||
.hSet(verified.sub, authData)
|
.set(verified.sub, authData)
|
||||||
.then(() => {
|
.then(() => {
|
||||||
if (sessionIdentifier && sessionIdentifier.length > 0) {
|
if (sessionIdentifier && sessionIdentifier.length > 0) {
|
||||||
logger.info(
|
logger.info(
|
||||||
`Adding sessionIdentifier ${sessionIdentifier} to store for environment ${authDataString}`
|
`Adding sessionIdentifier ${sessionIdentifier} to store for environment ${authDataString}`
|
||||||
);
|
);
|
||||||
return store.hSet(sessionIdentifier, authData);
|
return store.set(sessionIdentifier, authData);
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
.then(() => {
|
.then(() => {
|
||||||
|
|||||||
@ -33,7 +33,7 @@ router.get("/", async (req, res) => {
|
|||||||
// FIX for 'endTime' parameter cannot be later than one minute prior to now.
|
// FIX for 'endTime' parameter cannot be later than one minute prior to now.
|
||||||
endTime.setMinutes(endTime.getMinutes() - 1);
|
endTime.setMinutes(endTime.getMinutes() - 1);
|
||||||
|
|
||||||
const auth = await store.hGetAll(req.query.authKey);
|
const auth = await store.get(req.query.authKey);
|
||||||
const { host, tenant, token } = auth;
|
const { host, tenant, token } = auth;
|
||||||
axios
|
axios
|
||||||
.post(
|
.post(
|
||||||
|
|||||||
@ -6,7 +6,7 @@ import createHttpError from "http-errors";
|
|||||||
const router = Router();
|
const router = Router();
|
||||||
|
|
||||||
router.get("/", async (req, res, next) => {
|
router.get("/", async (req, res, next) => {
|
||||||
const auth = await store.hGetAll(req.query.authKey);
|
const auth = await store.get(req.query.authKey);
|
||||||
|
|
||||||
if (isEmpty(auth)) {
|
if (isEmpty(auth)) {
|
||||||
next(
|
next(
|
||||||
|
|||||||
@ -1,14 +1,51 @@
|
|||||||
import { createClient } from "redis";
|
import { createClient } from "redis";
|
||||||
|
import localStorage from "local-storage";
|
||||||
import config from "../config/index.js";
|
import config from "../config/index.js";
|
||||||
import { logger } from "./index.js";
|
import { logger } from "./index.js";
|
||||||
|
import Promise from "promise";
|
||||||
|
|
||||||
const client = createClient({
|
const store = await createStore();
|
||||||
url: config.redis.url,
|
|
||||||
});
|
|
||||||
|
|
||||||
client.on("error", (err) => logger.error("Redis Client Error", err));
|
async function createStore() {
|
||||||
|
if (process.env.STORE === "redis") {
|
||||||
|
return {
|
||||||
|
client: await createRedisClient(),
|
||||||
|
|
||||||
await client.connect();
|
set: async function (key, value) {
|
||||||
logger.info(`Connect store to ${config.redis.url}`);
|
return this.client.hSet(key, value);
|
||||||
|
},
|
||||||
|
get: async function (key) {
|
||||||
|
return this.client.hGetAll(key);
|
||||||
|
},
|
||||||
|
};
|
||||||
|
} else {
|
||||||
|
return {
|
||||||
|
set: function (key, value) {
|
||||||
|
return new Promise(function (resolve, reject) {
|
||||||
|
if (localStorage(key, value)) resolve(true);
|
||||||
|
else reject(Error("unable to set using local-storage"));
|
||||||
|
});
|
||||||
|
},
|
||||||
|
get: function (key) {
|
||||||
|
return new Promise(function (resolve) {
|
||||||
|
resolve(localStorage(key));
|
||||||
|
});
|
||||||
|
},
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
export default client;
|
async function createRedisClient() {
|
||||||
|
const client = createClient({
|
||||||
|
url: config.redis.url,
|
||||||
|
});
|
||||||
|
|
||||||
|
client.on("error", (err) => logger.error("Redis Client Error", err));
|
||||||
|
|
||||||
|
await client.connect();
|
||||||
|
logger.info(`Connect store to ${config.redis.url}`);
|
||||||
|
|
||||||
|
return client;
|
||||||
|
}
|
||||||
|
|
||||||
|
export default store;
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user