-
-
Notifications
You must be signed in to change notification settings - Fork 4k
/
generator.ts
180 lines (160 loc) · 6.14 KB
/
generator.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
/**
* Copyright 2013-2024 the original author or authors from the JHipster project.
*
* This file is part of the JHipster project, see https://www.jhipster.tech/
* for more information.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import BaseApplicationGenerator from '../../../base-application/index.js';
import { javaMainPackageTemplatesBlock, javaTestPackageTemplatesBlock } from '../../../java/support/files.js';
const WAIT_TIMEOUT = 3 * 60000;
export default class GatewayGenerator extends BaseApplicationGenerator {
async beforeQueue() {
if (!this.fromBlueprint) {
await this.composeWithBlueprints();
}
if (!this.delegateToBlueprint) {
await this.dependsOnBootstrapApplication();
await this.dependsOnJHipster('jhipster:java:build-tool');
}
}
get initializing() {
return this.asInitializingTaskGroup({
async parseCommand() {
await this.parseCurrentJHipsterCommand();
},
});
}
get [BaseApplicationGenerator.INITIALIZING]() {
return this.delegateTasksToBlueprint(() => this.initializing);
}
get prompting() {
return this.asPromptingTaskGroup({
async promptCommand({ control }) {
if (control.existingProject && this.options.askAnswered !== true) return;
await this.promptCurrentJHipsterCommand();
},
});
}
get [BaseApplicationGenerator.PROMPTING]() {
return this.delegateTasksToBlueprint(() => this.prompting);
}
get configuring() {
return this.asConfiguringTaskGroup({
reactiveByDefault() {
this.jhipsterConfig.reactive = this.jhipsterConfig.reactive ?? true;
if (this.jhipsterConfig.reactive === false) {
const message = 'Spring Cloud Gateway MVC support is experimental and not officially supported.';
if (!this.experimental) {
throw new Error(`${message} To use it, run the generator with the --experimental flag.`);
}
this.log.warn(message);
}
},
});
}
get [BaseApplicationGenerator.CONFIGURING]() {
return this.delegateTasksToBlueprint(() => this.configuring);
}
get loading() {
return this.asLoadingTaskGroup({
async loadConfig({ application }) {
await this.loadCurrentJHipsterCommandConfig(application);
},
});
}
get [BaseApplicationGenerator.LOADING]() {
return this.delegateTasksToBlueprint(() => this.loading);
}
get preparing() {
return this.asPreparingTaskGroup({
prepareGateway({ application }) {
application.gatewayRoutes = (application.routes ?? []).map(routeDef => {
const [route, host = route, serverPort = '8080'] = routeDef.split(':');
return { route, serverPort, host };
});
},
});
}
get [BaseApplicationGenerator.PREPARING]() {
return this.delegateTasksToBlueprint(() => this.preparing);
}
get writing() {
return this.asWritingTaskGroup({
async writing({ application }) {
await this.writeFiles({
blocks: [
javaMainPackageTemplatesBlock({
condition: ctx => ctx.reactive && ctx.authenticationTypeJwt,
templates: ['security/jwt/JWTRelayGatewayFilterFactory.java'],
}),
javaMainPackageTemplatesBlock({
condition: ctx => ctx.reactive && ctx.serviceDiscoveryAny,
templates: ['web/rest/vm/RouteVM.java', 'web/rest/GatewayResource.java', 'web/filter/ModifyServersOpenApiFilter.java'],
}),
javaTestPackageTemplatesBlock({
condition: ctx => ctx.reactive && ctx.serviceDiscoveryAny,
templates: ['web/filter/ModifyServersOpenApiFilterTest.java'],
}),
],
context: application,
});
},
});
}
get [BaseApplicationGenerator.WRITING]() {
return this.delegateTasksToBlueprint(() => this.writing);
}
get postWriting() {
return this.asPostWritingTaskGroup({
addDependencies({ application, source }) {
const { reactive } = application;
source.addJavaDependencies!([
{ groupId: 'org.springframework.cloud', artifactId: `spring-cloud-starter-gateway${reactive ? '' : '-mvc'}` },
]);
},
});
}
get [BaseApplicationGenerator.POST_WRITING]() {
return this.delegateTasksToBlueprint(() => this.postWriting);
}
get postWritingEntities() {
return this.asPostWritingEntitiesTaskGroup({
packageJsonE2eScripts({ application, entities }) {
if (application.applicationTypeGateway) {
const { serverPort, lowercaseBaseName } = application;
const microservices = [...new Set(entities.map(entity => entity.microserviceName))].filter(Boolean).map(ms => ms!.toLowerCase());
const scriptsStorage = this.packageJson.createStorage('scripts');
const waitServices = microservices
.concat(lowercaseBaseName)
.map(ms => `npm run ci:server:await:${ms}`)
.join(' && ');
scriptsStorage.set({
[`ci:server:await:${lowercaseBaseName}`]: `wait-on -t ${WAIT_TIMEOUT} http-get://127.0.0.1:$npm_package_config_backend_port/management/health`,
...Object.fromEntries(
microservices.map(ms => [
`ci:server:await:${ms}`,
`wait-on -t ${WAIT_TIMEOUT} http-get://127.0.0.1:${serverPort}/services/${ms}/management/health/readiness`,
]),
),
'ci:server:await': `echo "Waiting for services to start" && ${waitServices} && echo "Services started"`,
});
}
},
});
}
get [BaseApplicationGenerator.POST_WRITING_ENTITIES]() {
return this.delegateTasksToBlueprint(() => this.postWritingEntities);
}
}