Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Play Framework: Unable to read URLs from application.conf

I need to configure some URLs for my Play application so I added them to application.conf:

application.url="http://www.mydomain.com"
application.url.images="http://www.anotherdomain.com"
application.url.images.logo="${application.url.images}/logo.png"
...

Here below is the code I use in my views to access the entries above:

@(title: String)
@import play.api.Play.current

<!DOCTYPE html>

<html>
    ...

    <img src="@{ currrent.configuration.getString("application.url.images.logo") }" />

    ...
</html>

Well... I'm getting crazy since whenever I run the application I always get the following error message:

/home/j3d/Projects/test-app/conf/application.conf: 14-19: application.url.images.logo has type OBJECT rather than STRING

Any idea? Am I missing something? Or is it a bug?

Thank you very much.

like image 326
j3d Avatar asked Dec 08 '22 17:12

j3d


1 Answers

Config in Typesafe Configuration library used in Play represents JSON-like structure. Dot notation is syntax sugar to create nested objects ({ ... } in JSON). For example:

application.url="http://example.com"
application.images.logo="http://example.com/img/1.png"
application.images.header="http://example.com/img/3.png"

is equivalent to following JSON:

{
  "application": {
    "url": "http://example.com",
    "images": {
      "logo": "http://example.com/img/1.png",
      "header": "http://example.com/img/3.png"
    }
  }
}

In your example you first assign string to application.url, then trying to add keys to it (key url in application.url.images), like it is JSON object, not string. I don't know exact behavior of Typesafe Config in this case, and why it don't raise error immediately when reading config file.

Try rearranging hierarchy of config keys, i.e.:

application.url.prefix="http://www.mydomain.com"
application.url.images.prefix="http://www.anotherdomain.com"
application.url.images.logo="${application.url.images}/logo.png"

Here application.url will be object with keys prefix and images, and application.url.images will be object with keys prefix and logo.

like image 146
kolen Avatar answered Feb 11 '23 04:02

kolen