Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

replace default server port when property has custom value #8825

Merged
merged 7 commits into from
Feb 3, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ public class AngularModuleFactory {
private static final JHipsterSource COMMON_ESLINT = from("client/common/eslint");

private static final String CACHE_NEEDLE = " \"cacheDirectories\":";
private static final String SERVER_PORT_NEEDLE = "8080";
private static final PackageName ANGULAR_CORE_PACKAGE = packageName("@angular/core");

public JHipsterModule buildModule(JHipsterModuleProperties properties) {
Expand Down Expand Up @@ -100,11 +101,18 @@ public JHipsterModule buildModule(JHipsterModuleProperties properties) {
.in(path("package.json"))
.add(lineBeforeText(CACHE_NEEDLE), jestSonar(properties.indentation()))
.and()
.in(path("proxy.conf.json"))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rather than doing a replacement, you should turn proxy.conf.json to a template that will use the serverPort properties:

  • rename proxy.conf.json in src/main/resources/generator/client/angular/core/ to proxy.conf.json.mustache and replace 8080 by {{serverPort}}
  • change line .add(SOURCE.file("proxy.conf.json"), to("proxy.conf.json")) to .addTemplate(SOURCE.file("proxy.conf.json"), to("proxy.conf.json"))

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done. I have did the same to vte.config.ts in React Module. However, as I was not able to add the addTemple without the batch, I had to modify it to:

...
        .batch(SOURCE, to("."))
          .addTemplate("proxy.conf.json")
          .and()
...

.add(text(SERVER_PORT_NEEDLE), serverPort(properties))
.and()
.and()
.build();
//@formatter:on
}

private static String serverPort(JHipsterModuleProperties properties) {
return String.valueOf(properties.getOrDefaultInteger("serverPort", 8080));
}

private static String jestSonar(Indentation indentation) {
return new StringBuilder()
.append(indentation.spaces())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ public class ReactCoreModulesFactory {
private static final JHipsterDestination PRIMARY_APP_DESTINATION = APP_DESTINATION.append(PRIMARY_APP);

private static final String TEST_PRIMARY = "src/test/javascript/spec/common/primary/app";
private static final String SERVER_PORT_NEEDLE = "8080";

public JHipsterModule buildModule(JHipsterModuleProperties properties) {
//@formatter:off
Expand Down Expand Up @@ -84,7 +85,16 @@ public JHipsterModule buildModule(JHipsterModuleProperties properties) {
.addFile("ReactLogo.png")
.and()
.and()
.mandatoryReplacements()
.in(path("vite.config.ts"))
.add(text(SERVER_PORT_NEEDLE), serverPort(properties))
.and()
.and()
.build();
//@formatter:on
}

private String serverPort(JHipsterModuleProperties properties) {
return String.valueOf(properties.getOrDefaultInteger("serverPort", 8080));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ public JHipsterModule buildModule(JHipsterModuleProperties properties) {
.addScript(scriptKey("watch:css"), scriptCommand("onchange 'src/main/resources/static/css/**/*.css' -- npm run build:css"))
.addScript(scriptKey("watch:js"), scriptCommand("onchange 'src/main/resources/static/js/**/*.js' -- npm run build:js"))
.addScript(scriptKey("watch:svg"), scriptCommand("onchange 'src/main/resources/static/svg/**/*.svg' -- npm run build:svg"))
.addScript(scriptKey("watch:serve"), scriptCommand("browser-sync start --proxy localhost:8080 --files 'target/classes/templates' 'target/classes/static'"))
.addScript(scriptKey("watch:serve"), scriptCommand("browser-sync start --proxy localhost:%s --files 'target/classes/templates' 'target/classes/static'".formatted(getServerPort(properties))))
.and()
.files()
.add(RESOURCES_SOURCE.append(TEMPLATES).template("index.html"), toSrcMainResources().append(TEMPLATES).append("index.html"))
Expand All @@ -132,7 +132,7 @@ public JHipsterModule buildTailwindcssModule(JHipsterModuleProperties properties
.packageJson()
.addDevDependency(packageName("tailwindcss"), COMMON)
.addScript(scriptKey("watch:html"), scriptCommand("onchange 'src/main/resources/templates/**/*.html' -- npm-run-all --serial build:css build:html"))
.addScript(scriptKey("watch:serve"), scriptCommand("browser-sync start --no-inject-changes --proxy localhost:8080 --files 'target/classes/templates' 'target/classes/static'"))
.addScript(scriptKey("watch:serve"), scriptCommand("browser-sync start --no-inject-changes --proxy localhost:%s --files 'target/classes/templates' 'target/classes/static'".formatted(getServerPort(properties))))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's no reason to redefine the default 8080 port in every module, it's already defined in JHipsterServerPort so you can just use:

Suggested change
.addScript(scriptKey("watch:serve"), scriptCommand("browser-sync start --no-inject-changes --proxy localhost:%s --files 'target/classes/templates' 'target/classes/static'".formatted(getServerPort(properties))))
.addScript(scriptKey("watch:serve"), scriptCommand("browser-sync start --no-inject-changes --proxy localhost:%s --files 'target/classes/templates' 'target/classes/static'".formatted(properties.serverPort().get())))

and remove the getServerPort() method below.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! I was not sure if the serverPort property is generated as well when no spring-boot-mvc-empty module is added. I have removed unnecessary method

.and()
.mandatoryReplacements()
.in(path(POSTCSS_CONFIG_JS))
Expand All @@ -153,6 +153,12 @@ public JHipsterModule buildTailwindcssModule(JHipsterModuleProperties properties
//@formatter:on
}

private int getServerPort(JHipsterModuleProperties properties) {
Assert.notNull(PROPERTIES, properties);

return properties.getOrDefaultInteger("serverPort", 8080);
}

public JHipsterModule buildHtmxWebjarsModule(JHipsterModuleProperties properties) {
Assert.notNull(PROPERTIES, properties);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ The basic auth just allows you to authenticate one user configured in `applicati
## How it works

- Be sure to have selected and applied the `springdoc-jwt` module
- Access the swagger url: [http://localhost:8080/swagger-ui/index.html#](http://localhost:8080/swagger-ui/index.html#)
- Access the swagger url: [http://localhost:{{serverPort}}/swagger-ui/index.html#](http://localhost:{{serverPort}}/swagger-ui/index.html#)
- Execute the `POST /api/authenticate` endpoint passing the username and password to retrieve the token
- Enter only the token after clicking on the `Authorize` button at the top right
- Execute the `GET /api/account` to test if the token works
Original file line number Diff line number Diff line change
Expand Up @@ -884,8 +884,8 @@
{
"id": "6e8deddb-b4d6-4e2e-b389-b397d3f74fcd",
"clientId": "web_app",
"rootUrl": "http://localhost:8080",
"adminUrl": "http://localhost:8080",
"rootUrl": "http://localhost:{{serverPort}}",
"adminUrl": "http://localhost:{{serverPort}}",
"surrogateAuthRequired": false,
"enabled": true,
"alwaysDisplayInConsole": false,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,4 +98,32 @@ void shouldCreateAngularModule() {
)
.hasPrefixedFiles("src/main/webapp", "index.html", "main.ts", "styles.css");
}

@Test
void shouldProxyBeUpdatedWhenServerPortPropertyNotDefault() {
JHipsterModuleProperties properties = JHipsterModulesFixture
.propertiesBuilder(TestFileUtils.tmpDirForTest())
.projectBaseName("jhiTest")
.put("serverPort", 8081)
.build();

JHipsterModule module = factory.buildModule(properties);
assertThatModuleWithFiles(module, packageJsonFile(), lintStagedConfigFile())
.hasFile("proxy.conf.json")
.containing("\"target\": \"http://localhost:8081\"")
.notContaining("\"target\": \"http://localhost:8080\"");
}

@Test
void shouldProxyBeDefaultWhenServerPortPropertyMissing() {
JHipsterModuleProperties properties = JHipsterModulesFixture
.propertiesBuilder(TestFileUtils.tmpDirForTest())
.projectBaseName("jhiTest")
.build();

JHipsterModule module = factory.buildModule(properties);
assertThatModuleWithFiles(module, packageJsonFile(), lintStagedConfigFile())
.hasFile("proxy.conf.json")
.containing("\"target\": \"http://localhost:8080\"");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import tech.jhipster.lite.UnitTest;
import tech.jhipster.lite.module.domain.JHipsterModule;
import tech.jhipster.lite.module.domain.JHipsterModulesFixture;
import tech.jhipster.lite.module.domain.properties.JHipsterModuleProperties;

@UnitTest
class ReactCoreModulesFactoryTest {
Expand Down Expand Up @@ -70,6 +71,27 @@ void shouldBuildModuleWithStyle() {
.hasPrefixedFiles("src/main/webapp/assets", "ReactLogo.png", "JHipster-Lite-neon-blue.png");
}

@Test
void shouldViteConfigBeUpdatedWhenServerPortPropertyNotDefault() {
JHipsterModule module = factory.buildModule(
JHipsterModulesFixture.propertiesBuilder(TestFileUtils.tmpDirForTest()).projectBaseName("jhipster").put("serverPort", 8081).build()
);

assertThatModuleWithFiles(module, packageJsonFile())
.hasFile("vite.config.ts")
.containing("localhost:8081")
.notContaining("localhost:8080");
}

@Test
void shouldViteConfigBeDefaultWhenServerPortPropertyMissing() {
JHipsterModule module = factory.buildModule(
JHipsterModulesFixture.propertiesBuilder(TestFileUtils.tmpDirForTest()).projectBaseName("jhipster").build()
);

assertThatModuleWithFiles(module, packageJsonFile()).hasFile("vite.config.ts").containing("localhost:8080");
}

private String nodeScript(String key, String value) {
return "\"" + key + "\": \"" + value + "\"";
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,24 @@ void shouldCreateTailwindcssModule() {
//@formatter:on
}

@Test
void shouldProxyBeUpdatedWhenServerPortPropertyNotDefault() {
JHipsterModuleProperties properties = JHipsterModulesFixture
.propertiesBuilder(TestFileUtils.tmpDirForTest())
.projectBaseName("jhipster")
.put("serverPort", 8081)
.build();

JHipsterModule module = factory.buildModule(properties);

//@formatter:off
assertThatModuleWithFiles(module, packageJsonFile())
.hasFile("package.json")
.containing("browser-sync start --proxy localhost:8081 --files 'target/classes/templates' 'target/classes/static'")
.notContaining("browser-sync start --proxy localhost:8080 --files 'target/classes/templates' 'target/classes/static'");
//@formatter:on
}

private static ModuleFile appPostcssFile() {
return new ModuleFile("src/test/resources/projects/thymeleaf/postcss.config.js.template", "postcss.config.js");
}
Expand Down
Loading