use spring configuration for the tracker properties

This commit is contained in:
Zlatin Balevsky
2020-04-29 02:58:48 +01:00
parent fd9866c519
commit ad698cf1b9
3 changed files with 40 additions and 32 deletions

View File

@ -23,7 +23,6 @@ class Tracker {
private static Core core
private static TrackerService trackerService
private static WebServerCustomizer wsCustomizer
public static void main(String [] args) {
println "Launching MuWire Tracker version $VERSION"
@ -69,8 +68,8 @@ class Tracker {
// json rcp props go in tracker.properties
def jsonProps = new Properties()
jsonProps['jsonrpc.iface'] = props['jsonrpc.iface']
jsonProps['jsonrpc.port'] = props['jsonrpc.port']
jsonProps['tracker.jsonRpc.iface'] = props['jsonrpc.iface']
jsonProps['tracker.jsonRpc.port'] = props['jsonrpc.port']
trackerProps.withPrintWriter { jsonProps.store(it, "") }
}
@ -81,11 +80,6 @@ class Tracker {
p = new Properties()
trackerProps.withInputStream { p.load(it) }
InetAddress toBind = InetAddress.getByName(p['jsonrpc.iface'])
int port = Integer.parseInt(p['jsonrpc.port'])
wsCustomizer = new WebServerCustomizer(toBind, port)
core = new Core(muSettings, home, VERSION)
@ -100,26 +94,9 @@ class Tracker {
core.eventBus.publish(new UILoadedEvent())
} as Runnable)
coreStarter.start()
SpringApplication.run(Tracker.class, args)
}
private static class WebServerCustomizer implements WebServerFactoryCustomizer<ConfigurableWebServerFactory> {
private final InetAddress toBind
private final int port
WebServerCustomizer(InetAddress toBind, int port) {
this.toBind = toBind
this.port = port
}
@Override
public void customize(ConfigurableWebServerFactory factory) {
factory.setPort(port)
factory.setAddress(toBind)
}
System.setProperty("spring.config.location", trackerProps.getAbsolutePath())
SpringApplication.run(Tracker.class, args)
}
@Bean
@ -127,11 +104,6 @@ class Tracker {
core
}
@Bean
WebServerFactoryCustomizer<ConfigurableWebServerFactory> wsCustomizer() {
wsCustomizer
}
@Bean
public TrackerService trackerService() {
trackerService

View File

@ -0,0 +1,16 @@
package com.muwire.tracker
import org.springframework.boot.context.properties.ConfigurationProperties
import org.springframework.stereotype.Component
@Component
@ConfigurationProperties("tracker")
class TrackerProperties {
final JsonRpc jsonRpc = new JsonRpc()
public static class JsonRpc {
InetAddress iface
int port
}
}

View File

@ -0,0 +1,20 @@
package com.muwire.tracker
import org.springframework.boot.web.server.ConfigurableWebServerFactory
import org.springframework.boot.web.server.WebServerFactoryCustomizer
import org.springframework.stereotype.Component
@Component
class WebServerConfiguration implements WebServerFactoryCustomizer<ConfigurableWebServerFactory> {
private final TrackerProperties trackerProperties
WebServerConfiguration(TrackerProperties trackerProperties) {
this.trackerProperties = trackerProperties;
}
@Override
public void customize(ConfigurableWebServerFactory factory) {
factory.setAddress(trackerProperties.jsonRpc.getIface())
factory.setPort(trackerProperties.jsonRpc.port)
}
}