Conversation
|
Great idea!!! I was thinking about this feature for a long time as it is very useful for API server to serve resources(Static content or Javascript SPA). Previously, I was thinking to create a generic handler in light-4j to do the job but never got a chance to do that. Here is an example I did long time ago with injection in path handler provider. I am not sure if this is a good idea as it is a little bit harder for the codegen. Let me take a look at your implementation and we can have a chat when you are available. |
|
@stevehu i think your suggestion of a generic resource handler in light4j makes more sense then this change. The way i understand the generic solution would be as follows (as an example with codegen-web):
resourceManagerProvider: com.networknt.codegen.CodegenResourceManagerProvider
@Override
public HttpHandler getHandler() {
return Handlers.routing().add(Methods.GET, "/v1/resources", new ResourcesHandler())
}The
Does this sound better? It certainly gives the client a lot of control (resource serving path, which resources are made available, and which type of ResourceManager they provide), while allowing it to be driven by the standard light-4j configuration. I'm interested to hear your thoughts! |
|
There are other things that need to be considered. Like how to handle the url rewrite for the SPA. How to serve static content like /images etc. That is why PredicatedHandlerParser is used in the following handler. |
|
@stevehu that's a great point! One that I certainly haven't yet covered in this pr, but would be easily integrated with the solution in my last comment. |
|
Yes. We also need to test with single page application proxy as well. Most time, when using Webpack, a proxy will be started on the Nodejs and connect to backend Java API. So CORS handler needs to be enabled on the Java API. |
|
@NicholasAzar I have reviewed your code in both light-hybrid-4j and light-4j. The idea is great with this level of injection. It is better than the example I provided as it has to rely on the light-codegen. This approach gives users most flexibility. By comparing with the current develop branch in light-hybrid-4j, I see some conflicts. The latest change in the develop branch is to make each handler path configurable, so it should be easy to merge from develop to resources-handler. Thanks a lot for the effort. |
|
@stevehu sorry i couldn't get to this sooner.. conflicts should be resolved now. I hope it's ok to make the checks for configured paths a bit more concise.. i don't know if the double handler possibility of before was desired? |
|
@NicholasAzar I have merged this PR and also created a pull request for your changes in light-4j. Automatic build passed with all tests. Thanks a lot for your help. |
Not ready for merge
Hi @stevehu ,
I just wanted to get a sense from you for this solution. In order to provide hybrid services the ability to serve static resources on the same server, i'm hoping this would get the job done.
Each hybrid service within the server could have multiple classes implement the
RpcResourcePathsProviderinterface. At startup, the hook will run through identically to how you search for classes annotated with ServiceHandler, and will append to a list of safe paths.Any requests coming into the
/resources/*path, are then compared to that safe list (undertow built in) to see if they are available to serve.For example:
If your server jar had the following directories:
And there was a class in the jar that implemented
RpcResourcePathsProviderand returned["view"]from thegetSafeResourcePathsmethod. Then theindex.htmlwould be retrievable at the/resources/view/index.htmlpath, however nothing in the/configor/comfolders would be.Does this seem like a viable solution to continue working on?