Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ constructor(private val proxy: ProxyServer, private val logger: Logger) {
// Looked up per invocation rather than captured: the registry is written during startup and
// a reference taken now could be the one from before another plugin registered.
val service = { ProxyServiceRegistry.get(ProxyService::class.java) }
val region = { System.getenv("REGION")?.trim()?.takeIf(String::isNotEmpty) }
val region = { RegionCatalog.currentCode() }
proxy.commandManager.register("online", OnlineCommand(service))
proxy.commandManager.register("region", RegionCommand(catalog, region, service))

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,18 @@ class RegionCatalog(val regions: List<Region>, val problems: List<String>) {
get() = regions.map { it.code }

companion object {
/**
* Which catalogue code this proxy answers for. The catalogue is keyed by continent (`eu`),
* not by datacentre (`de-nbg1`), so `CONTINENT` is what a code compares against. `REGION`
* is the fallback for a catalogue still keyed by datacentre, where the two are the same.
*/
fun currentCode(
continent: String? = System.getenv("CONTINENT"),
region: String? = System.getenv("REGION"),
): String? =
continent?.trim()?.takeIf(String::isNotEmpty)
?: region?.trim()?.takeIf(String::isNotEmpty)

fun fromEnvironment(
raw: String? = System.getenv("REGIONS"),
defaultPort: Int = ProxyService.DEFAULT_MINECRAFT_PORT,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,16 @@ class RegionCatalogTest {
assertTrue(catalog.problems.isEmpty())
assertNull(catalog["nl-ams1"])
}

/** The catalogue is keyed by continent; comparing with the datacentre never matches. */
@Test
fun `current code is the continent, not the datacentre`() {
assertEquals("eu", RegionCatalog.currentCode(continent = "eu", region = "de-nbg1"))
}

@Test
fun `current code falls back to the region when no continent is set`() {
assertEquals("nl-ams1", RegionCatalog.currentCode(continent = " ", region = "nl-ams1"))
assertNull(RegionCatalog.currentCode(continent = null, region = null))
}
}