Skip to content

Commit 3cd7414

Browse files
committed
feat: support specifying server port using the PORT environment variable
This allows it to run on Heroku.
1 parent f1e2c33 commit 3cd7414

File tree

4 files changed

+29
-4
lines changed

4 files changed

+29
-4
lines changed

.github/workflows/build-master.yml

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ name: Build master
22

33
on:
44
push:
5-
pull_request:
6-
workflow_dispatch:
5+
branches:
6+
- master
77

88
jobs:
99
build:

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,7 @@ The standalone server supports the following configuration by `ENV` variables:
213213
| Variable | Description |
214214
| --- | --- |
215215
| `SERVER_HOSTNAME`| Lets the standalone server bind to a specific hostname, by default it binds to `0.0.0.0` |
216-
| `SERVER_PORT`| The port that the standalone server will listen to, defaults to `8080` |
216+
| `SERVER_PORT` or `PORT` | The port that the standalone server will listen to, defaults to `8080`. The `PORT` environment variable may be used to [run the Docker image on Heroku](https://devcenter.heroku.com/articles/container-registry-and-runtime#pushing-an-existing-image) as per the documentation [here](https://devcenter.heroku.com/articles/setting-the-http-port-for-java-applications). |
217217
| `JSON_CONFIG_PATH`| The absolute path to a json file containing configuration about the OAuth2 part of the server (`OAuth2Config`). More details on the format below. |
218218
| `JSON_CONFIG`| The actual JSON content of `OAuth2Config`, this ENV var takes precedence over the `JSON_CONFIG_PATH` var. More details on the format below.|
219219

src/main/kotlin/no/nav/security/mock/oauth2/StandaloneMockOAuth2Server.kt

+2-1
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,12 @@ object StandaloneConfig {
1616
const val JSON_CONFIG_PATH = "JSON_CONFIG_PATH"
1717
const val SERVER_HOSTNAME = "SERVER_HOSTNAME"
1818
const val SERVER_PORT = "SERVER_PORT"
19+
const val PORT = "PORT" // Supports running Docker image on Heroku.
1920

2021
fun hostname(): InetAddress = SERVER_HOSTNAME.fromEnv()
2122
?.let { InetAddress.getByName(it) } ?: InetSocketAddress(0).address
2223

23-
fun port(): Int = SERVER_PORT.fromEnv()?.toInt() ?: 8080
24+
fun port(): Int = (SERVER_PORT.fromEnv()?.toInt() ?: PORT.fromEnv()?.toInt())?: 8080
2425

2526
fun oauth2Config(): OAuth2Config = with(jsonFromEnv()) {
2627
if (this != null) {

src/test/kotlin/no/nav/security/mock/oauth2/StandaloneMockOAuth2ServerKtTest.kt

+24
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ import io.kotest.matchers.shouldBe
99
import io.kotest.matchers.types.beInstanceOf
1010
import no.nav.security.mock.oauth2.StandaloneConfig.JSON_CONFIG
1111
import no.nav.security.mock.oauth2.StandaloneConfig.JSON_CONFIG_PATH
12+
import no.nav.security.mock.oauth2.StandaloneConfig.SERVER_PORT
13+
import no.nav.security.mock.oauth2.StandaloneConfig.PORT
1214
import no.nav.security.mock.oauth2.StandaloneConfig.hostname
1315
import no.nav.security.mock.oauth2.StandaloneConfig.oauth2Config
1416
import no.nav.security.mock.oauth2.StandaloneConfig.port
@@ -31,6 +33,28 @@ internal class StandaloneMockOAuth2ServerKtTest {
3133
port() shouldBe 8080
3234
}
3335

36+
@Test
37+
fun `with the environment variable SERVER_PORT set`() {
38+
withEnvironment(SERVER_PORT to "9292") {
39+
port() shouldBe 9292
40+
}
41+
}
42+
43+
@Test
44+
fun `with the environment variable PORT set`() {
45+
withEnvironment(PORT to "9292") {
46+
port() shouldBe 9292
47+
}
48+
}
49+
50+
@Test
51+
fun `with the environment variables SERVER_PORT and PORT set`() {
52+
withEnvironment(mapOf(SERVER_PORT to "9292", PORT to "9393")) {
53+
port() shouldBe 9292
54+
}
55+
}
56+
57+
3458
@Test
3559
fun `load oauth2Config from file`() {
3660
withEnvironment(JSON_CONFIG_PATH to configFile) {

0 commit comments

Comments
 (0)