Entry point of a micro-service application











up vote
0
down vote

favorite












This is the main entry point for the my application (a micro-service). I feel like I'm doing a lot of things outside of main(), but even if I brought them all inside of main(), that would just make that massive.



Still relatively new to writing production ready code, and whilst there are no problems with this code, i can't help but feel there are better ways to structure it? Is there a better way of breaking down the code I have already written and making the code more readable/maintainable?



'use strict';

const fs = require('fs');
const http = require('http');
const {Receiver, Publisher} = require('openimprabbit');
const OSSEventEmitter = require('oss-events-emitter');
const Discovery = require('service-discovery');

const authorisation = require('./services/Authorisation');
const jobParser = require('./Parser');

const rabbitMQURL = new URL('amqp://'+process.env.AMQP_HOST);
rabbitMQURL.port = process.env.AMQP_PORT;
rabbitMQURL.username = process.env.AMQP_USER;
rabbitMQURL.password = process.env.AMQP_PASS;

const rabbitMQDetails = {
url: rabbitMQURL.toString(),
exchange: process.env.AMQP_SEND_EXCHANGE,
exchangeType: process.env.AMQP_TOPIC,
routingKey: '',
};

const ossEmitter = new OSSEventEmitter(rabbitMQDetails);
const Logger = require('./AOP/Logger');
Logger.Publisher(ossEmitter);

const QAFlagValidator = Logger.ClassHandler(
require('./job/QAFlagValidator')
);
const SchemaValidator = Logger.ClassHandler(
require('./job/validation/SchemaValidator')
);
const RuleSetCheck = Logger.ClassHandler(
require('./job/validation/RuleSetCheck')
);
const Releases = Logger.ClassHandler(require('./services/Releases'));

const discovery = new Discovery(http, process.env.DISCOVERY_URL);

const receiver = new Receiver(
rabbitMQURL.toString(),
process.env.AMQP_EXCHANGE,
process.env.AMQP_TOPIC,
process.env.AMQP_QUEUE,
process.env.AMQP_PATTERN,
{noAck: true}
);

const releaseAPI = new Releases(authorisation, http, discovery);

const schemaValidator = new SchemaValidator(
fs,
[
{location: './schemas/asset-controller.json', key: 'asset_controller'},
{location: './schemas/label.json', key: 'label'},
{location: './schemas/musicset.json', key: 'musicset'},
{location: './schemas/artist.json', key: 'artist'},
]
);

const rulesCheck = new RuleSetCheck(fs, Logger);

const qaValidator = new QAFlagValidator(
schemaValidator, rulesCheck
);

const main = async () => {
receiver.listen( async (msg) => {
const releaseIDs = jobParser.parse(msg);
const releases = await releaseAPI.getReleases(releaseIDs)
.catch((err) => {
console.error(err);
});

releases.forEach((release) => {
if (Object.keys(release).length) {
const rules = qaValidator.startValidation(release);
// console.log(rules);
}
});
});
};

main()
.catch((err) => {
console.error(err);
});









share|improve this question




























    up vote
    0
    down vote

    favorite












    This is the main entry point for the my application (a micro-service). I feel like I'm doing a lot of things outside of main(), but even if I brought them all inside of main(), that would just make that massive.



    Still relatively new to writing production ready code, and whilst there are no problems with this code, i can't help but feel there are better ways to structure it? Is there a better way of breaking down the code I have already written and making the code more readable/maintainable?



    'use strict';

    const fs = require('fs');
    const http = require('http');
    const {Receiver, Publisher} = require('openimprabbit');
    const OSSEventEmitter = require('oss-events-emitter');
    const Discovery = require('service-discovery');

    const authorisation = require('./services/Authorisation');
    const jobParser = require('./Parser');

    const rabbitMQURL = new URL('amqp://'+process.env.AMQP_HOST);
    rabbitMQURL.port = process.env.AMQP_PORT;
    rabbitMQURL.username = process.env.AMQP_USER;
    rabbitMQURL.password = process.env.AMQP_PASS;

    const rabbitMQDetails = {
    url: rabbitMQURL.toString(),
    exchange: process.env.AMQP_SEND_EXCHANGE,
    exchangeType: process.env.AMQP_TOPIC,
    routingKey: '',
    };

    const ossEmitter = new OSSEventEmitter(rabbitMQDetails);
    const Logger = require('./AOP/Logger');
    Logger.Publisher(ossEmitter);

    const QAFlagValidator = Logger.ClassHandler(
    require('./job/QAFlagValidator')
    );
    const SchemaValidator = Logger.ClassHandler(
    require('./job/validation/SchemaValidator')
    );
    const RuleSetCheck = Logger.ClassHandler(
    require('./job/validation/RuleSetCheck')
    );
    const Releases = Logger.ClassHandler(require('./services/Releases'));

    const discovery = new Discovery(http, process.env.DISCOVERY_URL);

    const receiver = new Receiver(
    rabbitMQURL.toString(),
    process.env.AMQP_EXCHANGE,
    process.env.AMQP_TOPIC,
    process.env.AMQP_QUEUE,
    process.env.AMQP_PATTERN,
    {noAck: true}
    );

    const releaseAPI = new Releases(authorisation, http, discovery);

    const schemaValidator = new SchemaValidator(
    fs,
    [
    {location: './schemas/asset-controller.json', key: 'asset_controller'},
    {location: './schemas/label.json', key: 'label'},
    {location: './schemas/musicset.json', key: 'musicset'},
    {location: './schemas/artist.json', key: 'artist'},
    ]
    );

    const rulesCheck = new RuleSetCheck(fs, Logger);

    const qaValidator = new QAFlagValidator(
    schemaValidator, rulesCheck
    );

    const main = async () => {
    receiver.listen( async (msg) => {
    const releaseIDs = jobParser.parse(msg);
    const releases = await releaseAPI.getReleases(releaseIDs)
    .catch((err) => {
    console.error(err);
    });

    releases.forEach((release) => {
    if (Object.keys(release).length) {
    const rules = qaValidator.startValidation(release);
    // console.log(rules);
    }
    });
    });
    };

    main()
    .catch((err) => {
    console.error(err);
    });









    share|improve this question


























      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      This is the main entry point for the my application (a micro-service). I feel like I'm doing a lot of things outside of main(), but even if I brought them all inside of main(), that would just make that massive.



      Still relatively new to writing production ready code, and whilst there are no problems with this code, i can't help but feel there are better ways to structure it? Is there a better way of breaking down the code I have already written and making the code more readable/maintainable?



      'use strict';

      const fs = require('fs');
      const http = require('http');
      const {Receiver, Publisher} = require('openimprabbit');
      const OSSEventEmitter = require('oss-events-emitter');
      const Discovery = require('service-discovery');

      const authorisation = require('./services/Authorisation');
      const jobParser = require('./Parser');

      const rabbitMQURL = new URL('amqp://'+process.env.AMQP_HOST);
      rabbitMQURL.port = process.env.AMQP_PORT;
      rabbitMQURL.username = process.env.AMQP_USER;
      rabbitMQURL.password = process.env.AMQP_PASS;

      const rabbitMQDetails = {
      url: rabbitMQURL.toString(),
      exchange: process.env.AMQP_SEND_EXCHANGE,
      exchangeType: process.env.AMQP_TOPIC,
      routingKey: '',
      };

      const ossEmitter = new OSSEventEmitter(rabbitMQDetails);
      const Logger = require('./AOP/Logger');
      Logger.Publisher(ossEmitter);

      const QAFlagValidator = Logger.ClassHandler(
      require('./job/QAFlagValidator')
      );
      const SchemaValidator = Logger.ClassHandler(
      require('./job/validation/SchemaValidator')
      );
      const RuleSetCheck = Logger.ClassHandler(
      require('./job/validation/RuleSetCheck')
      );
      const Releases = Logger.ClassHandler(require('./services/Releases'));

      const discovery = new Discovery(http, process.env.DISCOVERY_URL);

      const receiver = new Receiver(
      rabbitMQURL.toString(),
      process.env.AMQP_EXCHANGE,
      process.env.AMQP_TOPIC,
      process.env.AMQP_QUEUE,
      process.env.AMQP_PATTERN,
      {noAck: true}
      );

      const releaseAPI = new Releases(authorisation, http, discovery);

      const schemaValidator = new SchemaValidator(
      fs,
      [
      {location: './schemas/asset-controller.json', key: 'asset_controller'},
      {location: './schemas/label.json', key: 'label'},
      {location: './schemas/musicset.json', key: 'musicset'},
      {location: './schemas/artist.json', key: 'artist'},
      ]
      );

      const rulesCheck = new RuleSetCheck(fs, Logger);

      const qaValidator = new QAFlagValidator(
      schemaValidator, rulesCheck
      );

      const main = async () => {
      receiver.listen( async (msg) => {
      const releaseIDs = jobParser.parse(msg);
      const releases = await releaseAPI.getReleases(releaseIDs)
      .catch((err) => {
      console.error(err);
      });

      releases.forEach((release) => {
      if (Object.keys(release).length) {
      const rules = qaValidator.startValidation(release);
      // console.log(rules);
      }
      });
      });
      };

      main()
      .catch((err) => {
      console.error(err);
      });









      share|improve this question















      This is the main entry point for the my application (a micro-service). I feel like I'm doing a lot of things outside of main(), but even if I brought them all inside of main(), that would just make that massive.



      Still relatively new to writing production ready code, and whilst there are no problems with this code, i can't help but feel there are better ways to structure it? Is there a better way of breaking down the code I have already written and making the code more readable/maintainable?



      'use strict';

      const fs = require('fs');
      const http = require('http');
      const {Receiver, Publisher} = require('openimprabbit');
      const OSSEventEmitter = require('oss-events-emitter');
      const Discovery = require('service-discovery');

      const authorisation = require('./services/Authorisation');
      const jobParser = require('./Parser');

      const rabbitMQURL = new URL('amqp://'+process.env.AMQP_HOST);
      rabbitMQURL.port = process.env.AMQP_PORT;
      rabbitMQURL.username = process.env.AMQP_USER;
      rabbitMQURL.password = process.env.AMQP_PASS;

      const rabbitMQDetails = {
      url: rabbitMQURL.toString(),
      exchange: process.env.AMQP_SEND_EXCHANGE,
      exchangeType: process.env.AMQP_TOPIC,
      routingKey: '',
      };

      const ossEmitter = new OSSEventEmitter(rabbitMQDetails);
      const Logger = require('./AOP/Logger');
      Logger.Publisher(ossEmitter);

      const QAFlagValidator = Logger.ClassHandler(
      require('./job/QAFlagValidator')
      );
      const SchemaValidator = Logger.ClassHandler(
      require('./job/validation/SchemaValidator')
      );
      const RuleSetCheck = Logger.ClassHandler(
      require('./job/validation/RuleSetCheck')
      );
      const Releases = Logger.ClassHandler(require('./services/Releases'));

      const discovery = new Discovery(http, process.env.DISCOVERY_URL);

      const receiver = new Receiver(
      rabbitMQURL.toString(),
      process.env.AMQP_EXCHANGE,
      process.env.AMQP_TOPIC,
      process.env.AMQP_QUEUE,
      process.env.AMQP_PATTERN,
      {noAck: true}
      );

      const releaseAPI = new Releases(authorisation, http, discovery);

      const schemaValidator = new SchemaValidator(
      fs,
      [
      {location: './schemas/asset-controller.json', key: 'asset_controller'},
      {location: './schemas/label.json', key: 'label'},
      {location: './schemas/musicset.json', key: 'musicset'},
      {location: './schemas/artist.json', key: 'artist'},
      ]
      );

      const rulesCheck = new RuleSetCheck(fs, Logger);

      const qaValidator = new QAFlagValidator(
      schemaValidator, rulesCheck
      );

      const main = async () => {
      receiver.listen( async (msg) => {
      const releaseIDs = jobParser.parse(msg);
      const releases = await releaseAPI.getReleases(releaseIDs)
      .catch((err) => {
      console.error(err);
      });

      releases.forEach((release) => {
      if (Object.keys(release).length) {
      const rules = qaValidator.startValidation(release);
      // console.log(rules);
      }
      });
      });
      };

      main()
      .catch((err) => {
      console.error(err);
      });






      javascript object-oriented node.js






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 14 at 17:13









      200_success

      127k15148410




      127k15148410










      asked Nov 14 at 15:20









      Jarede

      199116




      199116



























          active

          oldest

          votes











          Your Answer





          StackExchange.ifUsing("editor", function () {
          return StackExchange.using("mathjaxEditing", function () {
          StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
          StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
          });
          });
          }, "mathjax-editing");

          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "196"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          convertImagesToLinks: false,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














           

          draft saved


          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f207661%2fentry-point-of-a-micro-service-application%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown






























          active

          oldest

          votes













          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes
















           

          draft saved


          draft discarded



















































           


          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f207661%2fentry-point-of-a-micro-service-application%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Ellipse (mathématiques)

          Quarter-circle Tiles

          Mont Emei