Serving dynamic content in Play 2.x

2019-08-13 13:50发布

问题:

On some level, I'm doing something very similar to this post: how to serve dynamic content in playframework 2

Which is that I have a web app where clients can specify various parameters and we have the Play! server initiate a process that generates a custom image file...which then needs to be served back to that client, preferably by Play!

Anticipated image life expectancy could be anywhere from several seconds to several minutes (maybe even hours). From that angle, we have reasons to believe that attempting to send back the image data directly with the response is not a viable approach...and instead are going to send back a URL to that dynamic image.

I would also very strongly prefer to NOT rely on a separate HTTP server being set up to serve up these dynamic images for a variety of reasons. I believe the reasons are many, including but not limited to... maintaining a simpler architecture for both the developer work environments as well as the production servers. We have a very small/restricted user base with very few concurrent users (I don't believe serving up these images needs to be terribly high performance anyway - assuming Play! can serve up these dynamic images, I find it hard to imagine that the performance wouldn't be perfectly acceptable considering the simplicity tradeoff).

I've read that Play! assets in the public/ folder are compiled into an .jar file build/compile time which seems to explain why my tests of dynamic image generation and serving back don't work as expected - the results served back are always from the previous build.

Can anyone suggest a way of serving dynamic assets back without relying on another server?

回答1:

Regardless of whether this is even a good idea, there is no real problem with Play! serving up assets in the public/ folder.

The only thing that can cause it to appear to not work is that any assets already existing in the public/ folder when the app gets compiled will get compiled into a .jar file. If you write a new file with a name/path that matches one that was compiled into that .jar file, you simply get the compiled file served back, not the new file. If the file does not exist in the .jar, it is served back just fine.

This may not be a terribly good idea but I think this is an acceptable solution for what we are attempting to do.