Serving files from hidden directory

Hello everyone!

I am trying to get my Play Application to serve files from a hidden directory, but it doesn’t work. I have read the Play documentation [Working with public assets(https://www.playframework.com/documentation/2.6.x/AssetsOverview). And I couldn’t find anything else.

An example request made to my web server would be:

GET /.test/test.txt

The result that I get when doing this request:

Action Not Found

For request 'GET /.test/text.txt'

These routes have been tried, in this order:
GET/.test/$file<.+>controllers.Assets.at(path:String = "/public/.test", file:String)

For now, here is what I have done:

  • In my public folder, I created the hidden directory .test, and I created a test.txt file in it.

  • In the file conf/routes, I have the following route: GET /.test/*file controllers.Assets.at(path="/public/.test", file)

  • In the file conf/application.conf, I have the following configuration: play.assets { path = "/public" urlPrefix = "/" }

The reason I want to serve a file from a hidden directory is that I am trying to get a certificate from Let’s Encrypt to deploy HTTPS on the server, thanks to the client Certbot.
Here are the instructions that I got from Certbot to get certificates:
Cerbot Instructions

As you can see, getting the certificate requires to answer a challenge to prove that you are in control of the domain for which you are asking a certificate. To answer this challenge, I must serve a particular file from a hidden directory, which explains the reason of this post.

Thank you very much for your time!

How are you running your server and testing it? Can you verify that the .test directory and its contents makes it into the compiled resources for the application? Play will look for files on its classpath so they need to be copied across. Perhaps sbt ignores files inside . directories.

For now, I am running it in development mode, but I would like to put it into production mode soon. Once I put it in production, I’ll keep in mind what you said about sbt ignoring files.

But for now, if it is in development, there shouldn’t be any issue with compiled resources I think.

If it’s not working you might want to double-check the resources are there even if you’re in development mode.

Yes, I double-checked to be sure. Everything is in place, and I can access other resources that are in the public folder, such as Images.
I think this might be a problem of configuration of Play, but I cannot find any solution.

If you rename .test to test, does it work?

Yes it works, I already tried with a file test/test.txt (and not .test/test.txt) and everything went fine.
Do we know if it is possible at all to serve hidden directories with Play? Has this been done already?

I’m not sure, but I don’t see why you couldn’t serve hidden directories. Play just uses the standard Java APIs for reading and writing files.

I thought that sbt is more likely to be at fault. It seems like quite a sensible default to strip out hidden files when compiling or distributing the application, but you said that you double-checked this.

If I were you I’d watch each stage of the build, check the file are in the JAR, etc. Then I’d put a debugger or some instrumentation into my app and debug the asset loading behaviour step by step.

The issue here is that sbt filters out hidden files by default, so they do not end up on the classpath. This is alluded to in the sbt documentation.

You can override the excludeFilter to include whichever hidden files you want, for example:

excludeFilter in Assets := HiddenFileFilter -- ".test"

or to include all assets:

excludeFilter in Assets := (_ => false)
2 Likes

Ok thank you very much! I finally had to find another way to get a certificate, but I’ll keep in mind what you said for next time!