@@ -5,7 +5,8 @@ import { FsUtilities } from '../encapsulation';
55import { ComposeBuild , ComposeDefinition , ComposePort , ComposeService , ComposeServiceEnvironment , ComposeServiceVolume } from './compose-file.model' ;
66import { DefaultEnvKeys , EnvUtilities } from '../env' ;
77import { OmitStrict } from '../types' ;
8- import { getPath , toSnakeCase } from '../utilities' ;
8+ import { getPath } from '../utilities' ;
9+ import { DockerTraefikUtilities } from './docker-traefik.utilities' ;
910
1011// eslint-disable-next-line jsdoc/require-jsdoc
1112type ParsedDockerComposeEnvironment = { [ key : string ] : string } | string [ ] ;
@@ -48,73 +49,6 @@ type ParsedDockerCompose = {
4849 */
4950export abstract class DockerUtilities {
5051
51- private static getTraefikLabels (
52- projectName : string ,
53- port : number ,
54- composeFileName : DockerComposeFileName ,
55- subDomain : string | undefined
56- ) : string [ ] {
57- if ( subDomain === 'www' ) {
58- throw new Error ( 'The subdomain "www" is reserved and will be set automatically.' ) ;
59- }
60-
61- switch ( composeFileName ) {
62- // eslint-disable-next-line sonar/no-duplicate-string
63- case 'dev.docker-compose.yaml' : {
64- return [ ] ;
65- }
66- // eslint-disable-next-line sonar/no-duplicate-string
67- case 'local.docker-compose.yaml' : {
68- return this . getTraefikLabelsForLocal ( projectName , subDomain , port ) ;
69- }
70- // eslint-disable-next-line sonar/no-duplicate-string
71- case 'docker-compose.yaml' : {
72- return this . getTraefikLabelsForProd ( projectName , subDomain , port ) ;
73- }
74- }
75- }
76-
77- private static getTraefikLabelsForProd ( projectName : string , subDomain : string | undefined , port : number ) : string [ ] {
78- let host : string = `Host(\`\${${ DefaultEnvKeys . subDomain ( projectName ) } }.\${${ DefaultEnvKeys . PROD_ROOT_DOMAIN } }\`)` ;
79- const labels : string [ ] = [ ] ;
80- if ( ! subDomain ) {
81- host = `Host(\`\${${ DefaultEnvKeys . PROD_ROOT_DOMAIN } }\`) || Host(\`www.\${${ DefaultEnvKeys . PROD_ROOT_DOMAIN } }\`)` ;
82- labels . push (
83- 'traefik.http.middlewares.wwwredirect.redirectregex.regex=^https://www\.(.*)' ,
84- 'traefik.http.middlewares.wwwredirect.redirectregex.replacement=https://$${1}' ,
85- `traefik.http.routers.${ toSnakeCase ( projectName ) } .middlewares=wwwredirect`
86- ) ;
87- }
88- labels . push (
89- 'traefik.enable=true' ,
90- `traefik.http.routers.${ toSnakeCase ( projectName ) } .rule=${ host } ` ,
91- `traefik.http.routers.${ toSnakeCase ( projectName ) } .entrypoints=web_secure` ,
92- `traefik.http.routers.${ toSnakeCase ( projectName ) } .tls.certresolver=ssl_resolver` ,
93- `traefik.http.services.${ toSnakeCase ( projectName ) } .loadbalancer.server.port=${ port } `
94- ) ;
95- return labels ;
96- }
97-
98- private static getTraefikLabelsForLocal ( projectName : string , subDomain : string | undefined , port : number ) : string [ ] {
99- let host : string = `Host(\`\${${ DefaultEnvKeys . subDomain ( projectName ) } }.localhost\`)` ;
100- const labels : string [ ] = [ ] ;
101- if ( ! subDomain ) {
102- host = 'Host(`localhost`) || Host(`www.localhost`)' ;
103- labels . push (
104- 'traefik.http.middlewares.wwwredirect.redirectregex.regex=^http://www\.(.*)' ,
105- 'traefik.http.middlewares.wwwredirect.redirectregex.replacement=http://$${1}' ,
106- `traefik.http.routers.${ toSnakeCase ( projectName ) } .middlewares=wwwredirect`
107- ) ;
108- }
109- labels . push (
110- 'traefik.enable=true' ,
111- `traefik.http.routers.${ toSnakeCase ( projectName ) } .rule=${ host } ` ,
112- `traefik.http.routers.${ toSnakeCase ( projectName ) } .entrypoints=web` ,
113- `traefik.http.services.${ toSnakeCase ( projectName ) } .loadbalancer.server.port=${ port } `
114- ) ;
115- return labels ;
116- }
117-
11852 /**
11953 * Creates the initial docker compose files at the given path.
12054 * @param email - The email that should be used for the letsencrypt certificate.
@@ -257,7 +191,7 @@ export abstract class DockerUtilities {
257191 const composePath : string = getPath ( composeFileName ) ;
258192 const definition : ComposeDefinition = await this . yamlToComposeDefinition ( composePath ) ;
259193
260- const labels : string [ ] = addTraefik ? this . getTraefikLabels ( service . name , port , composeFileName , subDomain ) : [ ] ;
194+ const labels : string [ ] = addTraefik ? DockerTraefikUtilities . getTraefikLabels ( service . name , port , composeFileName , subDomain ) : [ ] ;
261195
262196 definition . services . push ( { ...service , labels : [ ...service . labels ?? [ ] , ...labels ] } ) ;
263197 await FsUtilities . updateFile ( composePath , this . composeDefinitionToYaml ( definition ) , 'replace' ) ;
@@ -281,12 +215,15 @@ export abstract class DockerUtilities {
281215 key : DefaultEnvKeys . baseUrl ( service . name ) ,
282216 value : ( env , fileName ) => {
283217 switch ( fileName ) {
218+ // eslint-disable-next-line sonar/no-duplicate-string
284219 case 'dev.docker-compose.yaml' : {
285220 return `http://localhost:${ 'PORT_PLACEHOLDER' } ` ;
286221 }
222+ // eslint-disable-next-line sonar/no-duplicate-string
287223 case 'local.docker-compose.yaml' : {
288224 return `http://${ 'SUB_DOMAIN_PLACEHOLDER' } .localhost` ;
289225 }
226+ // eslint-disable-next-line sonar/no-duplicate-string
290227 case 'docker-compose.yaml' : {
291228 return `https://${ 'SUB_DOMAIN_PLACEHOLDER' } .${ 'PROD_ROOT_DOMAIN_PLACEHOLDER' } ` ;
292229 }
0 commit comments