You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

61 regels
1.6 KiB

  1. const fs = require("fs");
  2. const path = require("path");
  3. const redis = require("@redis/client");
  4. const bench_path = path.resolve(__dirname, "..", "..");
  5. const dns = require("dns");
  6. // Since node17, node resolves to ipv6 unless system is configured otherwise.
  7. // In XhiveFramework context using ipv4 - 127.0.0.1 is fine.
  8. dns.setDefaultResultOrder("ipv4first");
  9. function get_conf() {
  10. // defaults
  11. var conf = {
  12. socketio_port: 9000,
  13. };
  14. var read_config = function (file_path) {
  15. const full_path = path.resolve(bench_path, file_path);
  16. if (fs.existsSync(full_path)) {
  17. var bench_config = JSON.parse(fs.readFileSync(full_path));
  18. for (var key in bench_config) {
  19. if (bench_config[key]) {
  20. conf[key] = bench_config[key];
  21. }
  22. }
  23. }
  24. };
  25. // get ports from bench/config.json
  26. read_config("config.json");
  27. read_config("sites/common_site_config.json");
  28. // set overrides from environment
  29. if (process.env.XHIVEFRAMEWORK_SITE) {
  30. conf.default_site = process.env.XHIVEFRAMEWORK_SITE;
  31. }
  32. if (process.env.XHIVEFRAMEWORK_REDIS_CACHE) {
  33. conf.redis_cache = process.env.XHIVEFRAMEWORK_REDIS_CACHE;
  34. }
  35. if (process.env.XHIVEFRAMEWORK_REDIS_QUEUE) {
  36. conf.redis_queue = process.env.XHIVEFRAMEWORK_REDIS_QUEUE;
  37. }
  38. if (process.env.XHIVEFRAMEWORK_SOCKETIO_PORT) {
  39. conf.socketio_port = process.env.XHIVEFRAMEWORK_SOCKETIO_PORT;
  40. }
  41. return conf;
  42. }
  43. function get_redis_subscriber(kind = "redis_queue", options = {}) {
  44. const conf = get_conf();
  45. const host = conf[kind];
  46. return redis.createClient({ url: host, ...options });
  47. }
  48. module.exports = {
  49. get_conf,
  50. get_redis_subscriber,
  51. };