|
1 | 1 | import { extractFqdns } from "../utils/urlUtils"; |
2 | 2 | import { RESOURCE_TYPES } from "../constants/resourceTypes"; |
3 | 3 |
|
| 4 | +const extractFromCompose = (compose, key) => { |
| 5 | + if (!compose || !key) return []; |
| 6 | + const re = new RegExp(`${key}\\s*:\\s*'?([^'\\n]+)'?`, "i"); |
| 7 | + const m = compose.match(re); |
| 8 | + if (!m?.[1]) return []; |
| 9 | + return m[1] |
| 10 | + .split(/[,\s]+/) |
| 11 | + .map((s) => s.trim()) |
| 12 | + .filter(Boolean); |
| 13 | +}; |
| 14 | + |
| 15 | +const pickMinioPreferred = (urls) => { |
| 16 | + if (!urls?.length) return urls; |
| 17 | + const consoleUrls = urls.filter((u) => /console[-.]/i.test(u)); |
| 18 | + if (consoleUrls.length) { |
| 19 | + const rest = urls.filter((u) => !/console[-.]/i.test(u)); |
| 20 | + return [...consoleUrls, ...rest]; |
| 21 | + } |
| 22 | + return urls; |
| 23 | +}; |
| 24 | + |
4 | 25 | export const mapApplication = (app) => ({ |
5 | 26 | ...app, |
6 | 27 | type: RESOURCE_TYPES.APPLICATION, |
7 | 28 | fqdns: extractFqdns(app.fqdn), |
8 | 29 | }); |
9 | 30 |
|
10 | 31 | export const mapService = (service) => { |
11 | | - const serviceFqdns = []; |
| 32 | + let fqdns = []; |
12 | 33 |
|
13 | | - if (service.applications && service.applications.length > 0) { |
14 | | - service.applications.forEach((app) => { |
| 34 | + if (service.applications?.length) { |
| 35 | + for (const app of service.applications) { |
15 | 36 | if (app.fqdn) { |
16 | | - serviceFqdns.push(...extractFqdns(app.fqdn)); |
| 37 | + fqdns.push(...extractFqdns(app.fqdn)); |
17 | 38 | } |
18 | | - }); |
| 39 | + } |
19 | 40 | } |
20 | 41 |
|
21 | 42 | if (service.fqdn) { |
22 | | - serviceFqdns.push(...extractFqdns(service.fqdn)); |
| 43 | + fqdns.push(...extractFqdns(service.fqdn)); |
| 44 | + } |
| 45 | + |
| 46 | + const envUrls = |
| 47 | + service.environment?.COOLIFY_URL || |
| 48 | + service.environment?.COOLIFY_FQDN || |
| 49 | + null; |
| 50 | + if (envUrls) { |
| 51 | + fqdns.push(...extractFqdns(envUrls)); |
| 52 | + } else if (service.docker_compose) { |
| 53 | + fqdns.push(...extractFromCompose(service.docker_compose, "COOLIFY_URL")); |
| 54 | + if (!fqdns.length) { |
| 55 | + fqdns.push(...extractFromCompose(service.docker_compose, "COOLIFY_FQDN")); |
| 56 | + } |
23 | 57 | } |
24 | 58 |
|
| 59 | + const uniq = [...new Set(fqdns)]; |
| 60 | + const serviceType = (service.service_type || service.type || "").toString().toLowerCase(); |
| 61 | + const ordered = serviceType === "minio" ? pickMinioPreferred(uniq) : uniq; |
| 62 | + |
25 | 63 | return { |
26 | 64 | ...service, |
27 | 65 | type: RESOURCE_TYPES.SERVICE, |
28 | | - fqdns: [...new Set(serviceFqdns)], |
| 66 | + fqdns: ordered, |
29 | 67 | status: service.status || service.applications?.[0]?.status, |
30 | 68 | }; |
31 | 69 | }; |
|
0 commit comments