|
| 1 | +const hpropagate = require('hpropagate'); |
| 2 | +const postgres = require('./controllers/postgres.js'); |
| 3 | +const mongo = require('./controllers/mongo.js'); |
| 4 | +const MongoClientWrapper = require('./wrappers/MongoClientWrapper.js'); |
| 5 | +const MongoServerWrapper = require('./wrappers/MongoServerWrapper.js'); |
| 6 | +const PostgresClientWrapper = require('./wrappers/PostgresClientWrapper.js'); |
| 7 | +const PostgresServerWrapper = require('./wrappers/PostgresServerWrapper.js'); |
| 8 | +const { validateInput, addNotifications } = require('./controllers/helpers'); |
| 9 | + |
| 10 | +let userConfig = {}; |
| 11 | +const chronos = {}; |
| 12 | + |
| 13 | +/** |
| 14 | + * ********************************** |
| 15 | + * CMD CONFIG FILE SETUP |
| 16 | + * |
| 17 | + * @field microservice {string} REQUIRED |
| 18 | + * The user specified name for the microservice being tracked |
| 19 | + * |
| 20 | + * @field interval {number} DEFAULT 60000 |
| 21 | + * The interval for every microservice health check in milliseconds |
| 22 | + * This defaults to 60000 ms or 1 minute |
| 23 | + * |
| 24 | + * @field database {Object} |
| 25 | + * Takes two properties |
| 26 | + * - type {string} Either PostgreSQL or MongoDB |
| 27 | + * - URI {string} Database uri |
| 28 | + * |
| 29 | + * @field notifications {array} OPTIONAL |
| 30 | + * Varies per notification method |
| 31 | + * ********************************** |
| 32 | + */ |
| 33 | +chronos.use = config => { |
| 34 | + // Validate all input fields exist and setup notifications |
| 35 | + validateInput(config); |
| 36 | + addNotifications(config); |
| 37 | + |
| 38 | + // Save config globally |
| 39 | + userConfig = config; |
| 40 | +}; |
| 41 | + |
| 42 | +/** |
| 43 | + * Places an unique x-correlating-id into the headers of each request/response. |
| 44 | + * This is used for tracking the life cycle of the request until the response |
| 45 | + */ |
| 46 | +chronos.propagate = () => { |
| 47 | + hpropagate({ propagateInResponses: true }); |
| 48 | +}; |
| 49 | + |
| 50 | +/** |
| 51 | + * ********************************************** |
| 52 | + * MAIN CONTROLLER |
| 53 | + * Only supports MongoDB and PostgreSQL for now! |
| 54 | + * ********************************************** |
| 55 | + */ |
| 56 | +chronos.track = () => { |
| 57 | + const { database, dockerized } = userConfig; |
| 58 | + |
| 59 | + /** |
| 60 | + * If the provided database is Mongo |
| 61 | + * - Connection is made to MongoDB via the provided URI by the user. |
| 62 | + * |
| 63 | + * - 'services' collection will be created if not already and stores every microservice |
| 64 | + * that is apart of the application. |
| 65 | + * |
| 66 | + * - Information is collected if the microservice is containerized |
| 67 | + * |
| 68 | + * - 'communications' collection will be created which creates a new document for every |
| 69 | + * endpoint that the user Request travels through (tracked with hpropograte) for express routes |
| 70 | + */ |
| 71 | + if (database.type === 'MongoDB') { |
| 72 | + mongo.connect(userConfig); |
| 73 | + mongo.services(userConfig); |
| 74 | + mongo.docker(userConfig); |
| 75 | + mongo.health(userConfig); |
| 76 | + if (database.connection === 'REST') { |
| 77 | + return mongo.communications(userConfig); |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + /** |
| 82 | + * If the provided database is PostgreSQL |
| 83 | + * - Connection is made to the postgres client via the provided URI by the user. |
| 84 | + * |
| 85 | + * - 'services' table will be created if not already and stores every microservice |
| 86 | + * that is apart of the application. |
| 87 | + * |
| 88 | + * - Information is collected if the microservice is containerized |
| 89 | + * |
| 90 | + * - 'communications' table will be created which creates a new row entry for every |
| 91 | + * endpoint that the user Request travels through (tracked with hpropograte) |
| 92 | + */ |
| 93 | + if (database.type === 'PostgreSQL') { |
| 94 | + postgres.connect(userConfig); |
| 95 | + postgres.services(userConfig); |
| 96 | + postgres.docker(userConfig); |
| 97 | + postgres.health(userConfig); |
| 98 | + if (database.connection === 'REST') { |
| 99 | + return postgres.communications(userConfig); |
| 100 | + } |
| 101 | + } |
| 102 | + return null; |
| 103 | +}; |
| 104 | + |
| 105 | +chronos.ServerWrapper = (server, proto, methods) => { |
| 106 | + const { database } = userConfig; |
| 107 | + if (database.type === 'MongoDB') { |
| 108 | + return new MongoServerWrapper(server, proto, methods, userConfig); |
| 109 | + } |
| 110 | + if (database.type === 'PostgreSQL') { |
| 111 | + return new PostgresServerWrapper(server, proto, methods, userConfig); |
| 112 | + } |
| 113 | + return null; |
| 114 | +}; |
| 115 | + |
| 116 | +chronos.ClientWrapper = (client, service) => { |
| 117 | + const { database } = userConfig; |
| 118 | + if (database.type === 'MongoDB') { |
| 119 | + return new MongoClientWrapper(client, service, userConfig); |
| 120 | + } |
| 121 | + if (database.type === 'PostgreSQL') { |
| 122 | + return new PostgresClientWrapper(client, service, userConfig); |
| 123 | + } |
| 124 | + return null; |
| 125 | +}; |
| 126 | + |
| 127 | +chronos.link = (client, server) => { |
| 128 | + client.metadata = server.metadataHolder; |
| 129 | +}; |
| 130 | + |
| 131 | +module.exports = chronos; |
0 commit comments