|
3 | 3 | // Modules |
4 | 4 | const _ = require('lodash'); |
5 | 5 |
|
6 | | -/* |
7 | | - * The lowest level lando service |
| 6 | +/** |
| 7 | + * @typedef {import('../lib/factory').ComposeService} ComposeService |
| 8 | + * @typedef {import('./_lando').LandoServiceConfig} LandoServiceConfig |
| 9 | + */ |
| 10 | + |
| 11 | +/** |
| 12 | + * Configuration options for a Lando service. |
| 13 | + * @typedef {Object} ServiceConfig |
| 14 | + * @extends {LandoServiceConfig} |
| 15 | + * @property {string} [webroot] - The webroot path relative to /app |
| 16 | + * @property {number} [port] - The port to expose |
| 17 | + * @property {boolean|number} [portforward] - Whether to forward ports (true) or a specific port number |
| 18 | + * @property {Object} [_app] - Internal app configuration |
| 19 | + * @property {Object} [_app._config] - App configuration settings |
| 20 | + * @property {string} [_app._config.bindAddress] - The address to bind services to |
| 21 | + * @property {Object} [healthcheck] - Health check configuration |
| 22 | + * @property {Object} [creds] - Service credentials |
| 23 | + */ |
| 24 | + |
| 25 | +/** |
| 26 | + * Base service implementation that extends the Lando base service. |
| 27 | + * @type {Object} |
8 | 28 | */ |
9 | 29 | module.exports = { |
| 30 | + /** |
| 31 | + * The name of the service. |
| 32 | + * @type {string} |
| 33 | + */ |
10 | 34 | name: '_service', |
| 35 | + |
| 36 | + /** |
| 37 | + * The parent service type to extend from. |
| 38 | + * @type {string} |
| 39 | + */ |
11 | 40 | parent: '_lando', |
| 41 | + |
| 42 | + /** |
| 43 | + * Creates a new LandoService class extending the parent class. |
| 44 | + * @param {any} parent - The parent class to extend from. |
| 45 | + * @return {any} The LandoService class. |
| 46 | + */ |
12 | 47 | builder: parent => class LandoService extends parent { |
| 48 | + /** |
| 49 | + * Creates a new LandoService instance. |
| 50 | + * @param {string} id - The unique identifier for the service. |
| 51 | + * @param {Partial<ServiceConfig>} options - Service configuration options. |
| 52 | + * @param {...Object} sources - Additional configuration sources to merge. |
| 53 | + */ |
13 | 54 | constructor(id, options = {}, ...sources) { |
| 55 | + // Add the service environment settings |
14 | 56 | sources.push({services: _.set({}, options.name, { |
15 | 57 | environment: { |
| 58 | + /** |
| 59 | + * The webroot path for the service. |
| 60 | + * @type {string} |
| 61 | + */ |
16 | 62 | LANDO_WEBROOT: `/app/${options.webroot}`, |
| 63 | + /** |
| 64 | + * The type of the service. |
| 65 | + * @type {string} |
| 66 | + */ |
17 | 67 | LANDO_SERVICE_TYPE: 'service', |
18 | 68 | }, |
19 | 69 | })}); |
20 | | - // @TODO: add in any envvars for this? |
21 | | - // Add in relevant portforward data |
| 70 | + |
| 71 | + // Add port forwarding settings if specified |
22 | 72 | if (options.portforward) { |
23 | 73 | if (options.portforward === true) { |
| 74 | + // Add port forwarding for the service using the default port |
24 | 75 | sources.push({services: _.set({}, options.name, {ports: [options.port]})}); |
25 | 76 | } else { |
| 77 | + // Add port forwarding with a custom port mapping |
26 | 78 | sources.push({services: _.set({}, options.name, {ports: [`${options.portforward}:${options.port}`]})}); |
27 | 79 | } |
28 | 80 | } |
29 | | - // Add in relevant info |
| 81 | + |
| 82 | + // Merge the info object with default connection settings |
30 | 83 | options.info = _.merge({}, options.info, { |
| 84 | + /** |
| 85 | + * Internal connection details for service-to-service communication. |
| 86 | + * @type {Object} |
| 87 | + * @property {string} host - The service hostname |
| 88 | + * @property {number} port - The service port |
| 89 | + */ |
31 | 90 | internal_connection: { |
32 | 91 | host: options.name, |
33 | 92 | port: options.port, |
34 | 93 | }, |
| 94 | + /** |
| 95 | + * External connection details for host machine access. |
| 96 | + * @type {Object} |
| 97 | + * @property {string} host - The bind address |
| 98 | + * @property {number|string} port - The forwarded port or 'not forwarded' |
| 99 | + */ |
35 | 100 | external_connection: { |
36 | 101 | host: options._app._config.bindAddress, |
37 | 102 | port: _.get(options, 'portforward', 'not forwarded'), |
38 | 103 | }, |
39 | 104 | }); |
40 | | - // Add in a our healthcheck if we have one |
| 105 | + |
| 106 | + // Add healthcheck settings if specified |
41 | 107 | if (options.healthcheck) options.info.healthcheck = options.healthcheck; |
42 | | - // Add in creds if we have them |
| 108 | + |
| 109 | + // Add credentials if specified |
43 | 110 | if (options.creds) options.info.creds = options.creds; |
| 111 | + |
| 112 | + // Call the parent constructor with the merged configuration |
44 | 113 | super(id, options, ...sources); |
45 | 114 | } |
46 | 115 | }, |
|
0 commit comments