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

Static content support module. #2705

Merged

Conversation

tomas-langer
Copy link
Member

@tomas-langer tomas-langer commented Jan 29, 2021

Resolves #1869
Resolves #1236
Resolves #2631
Resolves #2764

There is a new module: helidon-webserver-static-content.
Entry point is io.helidon.webserver.staticcontent.StaticContentSupport.builder(..)

To achieve a fallback for static content, you can use the following when setting up routing:

Routing.builder()
  .register("/static", StaticContentSupport.builder("web")
    .welcomeFileName("index.txt")
    .build())
  .register("/static", StaticContentSupport.builder("fallback")
    .pathMapper(path -> "index.txt")
    .build())

This will resolve any request to /static as follows:

  1. Attempt to find a resource on the classpath under web, using index.txt as the welcome file for directory requests
  2. Use fallback/index.txt for any request that does not find a resource in the first step

The default pathMapper does not modify the path (so for static/css/company.css it returns static/css/company.css).

There is a known duplicate use of temporary directory - each instance of static content support based on classpath would extract the files separately. If we used the same classpath location to read the "fallback" file, it would be extracted and cached twice.
As this use-case is based on the need to have a single backup file, the impact is minimal (extracting one extra file). If this is a major performance issue for your application, please submit a new issue with your use case.

@tomas-langer tomas-langer added webserver 2.x Issues for 2.x version branch labels Jan 29, 2021
@tomas-langer tomas-langer added this to the 2.3.0 milestone Jan 29, 2021
@tomas-langer tomas-langer self-assigned this Jan 29, 2021
Copy link
Contributor

@romain-grecourt romain-grecourt left a comment

Choose a reason for hiding this comment

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

Shouldn't this PR be targeted for 3.x ? It breaks compatibility right ?

@tomas-langer
Copy link
Member Author

I have only created a new module and deprecated the old classes. Everything still works as it used to.

@tomas-langer
Copy link
Member Author

Will need to incorporate changes from #2686 if we decide to merge them

@tomas-langer
Copy link
Member Author

Check #2631

We should support a fallback to a single file from static content.

Request to /static/webserver/index.html - which does not exist, should return /static/index.html.

This would allow for routing like this:

Routing.builder()
  .register("/static", StaticContentSupport.create("/static")
 .register("/static", SingleFileContent.create("/index.html")

Signed-off-by: Tomas Langer <tomas.langer@oracle.com>
Signed-off-by: Tomas Langer <tomas.langer@oracle.com>
 - fixes false positives due to try with resources
 - fixed MediaType as it triggered a bug

Signed-off-by: Tomas Langer <tomas.langer@oracle.com>
Support for single file handling.

Signed-off-by: Tomas Langer <tomas.langer@oracle.com>
@tomas-langer
Copy link
Member Author

Refactored implementation to have nicer builders, added integration tests to validate implementation.

Signed-off-by: Tomas Langer <tomas.langer@oracle.com>
Signed-off-by: Tomas Langer <tomas.langer@oracle.com>
Signed-off-by: Tomas Langer <tomas.langer@oracle.com>
Signed-off-by: Tomas Langer <tomas.langer@oracle.com>
return Routing.builder()
.register("/simple", classpath)
.register("/fallback", classpath)
.register("/fallback", StaticContentSupport.builder("fallback")
Copy link

Choose a reason for hiding this comment

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

Maybe a minor thing, but fallback/index.html will be extracted twice to the tmp directory when using two separate instances.

Copy link
Member Author

Choose a reason for hiding this comment

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

I do not think this is the case. There is one based on classpath, one based on path.

Copy link
Contributor

@romain-grecourt romain-grecourt Feb 11, 2021

Choose a reason for hiding this comment

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

This is a side-effect of the current implementation of the classpath based StaticContentSupport.

Signed-off-by: Tomas Langer <tomas.langer@oracle.com>
Signed-off-by: Tomas Langer <tomas.langer@oracle.com>
Signed-off-by: Tomas Langer <tomas.langer@oracle.com>
Signed-off-by: Tomas Langer <tomas.langer@oracle.com>
@tomas-langer tomas-langer merged commit 1d84bbb into helidon-io:master Feb 13, 2021
@tomas-langer tomas-langer deleted the 1869-static-content-module branch February 13, 2021 23:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
2.x Issues for 2.x version branch webserver
Projects
None yet
3 participants