Skip to content

Commit

Permalink
VaadinBootBase: post-process contextRoot to get rid of trailing slash…
Browse files Browse the repository at this point in the history
… etc
  • Loading branch information
mvysny committed Sep 15, 2024
1 parent febf1e8 commit bacd1ff
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 12 deletions.
12 changes: 7 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -534,11 +534,13 @@ new VaadinBoot().localhostOnly().setPort(8081).run();

On top of that, the following Vaadin Boot properties are configurable via environment variables and also Java system properties:

| Vaadin Boot config property | Env variable | Java system property |
|-----------------------------|------------------------------|-----------------------------|
| port | SERVER_PORT | server.port |
| listen interface | SERVER_ADDRESS | server.address |
| context root | SERVER_SERVLET_CONTEXT-PATH | server.servlet.context-path |
| Vaadin Boot config property | Env variable | Java system property | Example Value |
|-----------------------------|-----------------------------|-----------------------------|---------------|
| port | SERVER_PORT | server.port | 18080 |
| listen interface | SERVER_ADDRESS | server.address | localhost |
| context root | SERVER_SERVLET_CONTEXT_PATH | server.servlet.context-path | /admin |

> Note: Vaadin Boot 13.1 and older honored `SERVER_SERVLET_CONTEXT-PATH` instead of `SERVER_SERVLET_CONTEXT_PATH`.
You can not pass the Java system properties to your app run scripts directly, since they will be treated as
program parameters. Instead, pass them via the `JAVA_OPTS` env variable (only works with script created by Gradle):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,22 @@ public abstract class VaadinBootBase<THIS extends VaadinBootBase<THIS>> {

/**
* The context root to run under. Defaults to "".
* Change this to e.g. /foo to host your app on a different context root
* Change this to e.g. "/foo" to host your app on a different context root
* <br/>
* Can be configured via the <code>SERVER_SERVLET_CONTEXT_PATH</code> environment variable, or <code>-Dserver.servlet.context-path=</code> Java system property.
* <br/>
* Can be configured via the <code>SERVER_SERVLET_CONTEXT-PATH</code> environment variable, or <code>-Dserver.servlet.context-path=</code> Java system property.
* Example values:
* <ul>
* <li>""</li>
* <li>"/" - will be changed to ""</li>
* <li>"/foo"</li>
* <li>"foobar" - will be changed to "/foobar"</li>
* <li>"foo/bar/baz" - will be changed to "/foo/bar/baz"</li>
* </ul>
*/
@NotNull
@VisibleForTesting
String contextRoot = Env.getProperty("SERVER_SERVLET_CONTEXT-PATH", "server.servlet.context-path", "");
String contextRoot = postprocessContextRoot(Env.getProperty("SERVER_SERVLET_CONTEXT_PATH", "server.servlet.context-path", ""));

/**
* When the app launches, open the browser automatically when in dev mode.
Expand Down Expand Up @@ -117,13 +126,25 @@ public THIS localhostOnly() {
*/
@NotNull
public THIS withContextRoot(@NotNull String contextRoot) {
this.contextRoot = Objects.requireNonNull(contextRoot);
if (this.contextRoot.equals("/")) {
this.contextRoot = "";
}
this.contextRoot = postprocessContextRoot(contextRoot);
return getThis();
}

@NotNull
private static String postprocessContextRoot(@NotNull String contextRoot) {
Objects.requireNonNull(contextRoot);
if (contextRoot.equals("/")) {
contextRoot = "";
}
if (!contextRoot.startsWith("/")) {
contextRoot = "/" + contextRoot;
}
if (contextRoot.endsWith("/")) {
contextRoot = contextRoot.substring(0, contextRoot.length() - 1);
}
return contextRoot;
}

/**
* When the app launches, open the browser automatically when in dev mode.
* @param openBrowserInDevMode defaults to true.
Expand Down

0 comments on commit bacd1ff

Please sign in to comment.