Server config using properties file

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

Server config using properties file

syepes
Hello,

I have started using Ratpack and I just cant figure out how to change the Ratpack's Server settings using an external file.

I found how to change it using the environment variables or hard coding it in the code:

application.properties
ratpack.server.address=192.168.0.1
ratpack.server.port=9090

ratpack.groovy
ratpack {
  final Logger log = LoggerFactory.getLogger(this.class)

  bindings {
    ConfigData configData = ConfigData.of { c ->
      c.props("$serverConfig.baseDir.file/application.properties")
      c.sysProps()
      c.env()
      c.build()
    }
    // Is there a way of changing the server port and address using the application.properties file?
    //serverConfig { port(6060) }

    handlers {
      get { render 'Hello' }
    }
  }
}

Regards and thanks for any help,
Sebastian
rus
Reply | Threaded
Open this post in threaded view
|

Re: Server config using properties file

rus
Hi Sebastian, try the following:

ratpack {
  final Logger log = LoggerFactory.getLogger(this.class)

  serverConfig {
    props("application.properties")
  }

  bindings {
    ConfigData configData = ConfigData.of { c ->
      c.props("$serverConfig.baseDir.file/application.properties")
      c.sysProps()
      c.env()
      c.build()
    }
   
    handlers {
      get { render 'Hello' }
    }
  }
}
Reply | Threaded
Open this post in threaded view
|

Re: Server config using properties file

Marcin Erdmann
Yeah, as Rus's example demonstrates, server config is a config source as well and you can configure the server via properties with "server." prefix. So in your properties file you will need to have:

server.port=5051
Reply | Threaded
Open this post in threaded view
|

Re: Server config using properties file

syepes
Thank you guys very much for the replies, they were very useful.

Now I have a similar issue but with the location of the actual application.properties file.
What I am trying to do is. If the properties file is only found inside the fatJar it should be used, but if there is a properties file present in the classpath this one should override the one embedded in the jar.

I have been trying to do the with the baseDir option and placing the properties file in the same location as the ".ratpack" file, but it seems to not work ;-(

    ConfigData configData = ConfigData.of { c ->
      c.props(ClassLoader.getSystemResource('application.properties') as URL)
      c.onError(Action.noop()).props("$serverConfig.baseDir.file/application.properties")
      c.sysProps()
      c.env()
      c.build()
    }

Is is possible to achieve this behavior for the configData and serverConfig properties?

Thanks in advance for the help,
Sebastian