Skip to content

Commit

Permalink
feat: support specifying server port using the PORT environment variable
Browse files Browse the repository at this point in the history
This allows it to run on Heroku.
  • Loading branch information
bethesque committed Feb 23, 2022
1 parent f1e2c33 commit 249b53c
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 4 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/build-master.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ name: Build master

on:
push:
pull_request:
workflow_dispatch:
# branches:
# - master

jobs:
build:
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ The standalone server supports the following configuration by `ENV` variables:
| Variable | Description |
| --- | --- |
| `SERVER_HOSTNAME`| Lets the standalone server bind to a specific hostname, by default it binds to `0.0.0.0` |
| `SERVER_PORT`| The port that the standalone server will listen to, defaults to `8080` |
| `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). |
| `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. |
| `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.|

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,12 @@ object StandaloneConfig {
const val JSON_CONFIG_PATH = "JSON_CONFIG_PATH"
const val SERVER_HOSTNAME = "SERVER_HOSTNAME"
const val SERVER_PORT = "SERVER_PORT"
const val PORT = "PORT" // Supports running Docker image on Heroku.

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

fun port(): Int = SERVER_PORT.fromEnv()?.toInt() ?: 8080
fun port(): Int = (SERVER_PORT.fromEnv()?.toInt() ?: PORT.fromEnv()?.toInt())?: 8080

fun oauth2Config(): OAuth2Config = with(jsonFromEnv()) {
if (this != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import io.kotest.matchers.shouldBe
import io.kotest.matchers.types.beInstanceOf
import no.nav.security.mock.oauth2.StandaloneConfig.JSON_CONFIG
import no.nav.security.mock.oauth2.StandaloneConfig.JSON_CONFIG_PATH
import no.nav.security.mock.oauth2.StandaloneConfig.SERVER_PORT
import no.nav.security.mock.oauth2.StandaloneConfig.PORT
import no.nav.security.mock.oauth2.StandaloneConfig.hostname
import no.nav.security.mock.oauth2.StandaloneConfig.oauth2Config
import no.nav.security.mock.oauth2.StandaloneConfig.port
Expand All @@ -31,6 +33,28 @@ internal class StandaloneMockOAuth2ServerKtTest {
port() shouldBe 8080
}

@Test
fun `with the environment variable SERVER_PORT set`() {
withEnvironment(SERVER_PORT to "9292") {
port() shouldBe 9292
}
}

@Test
fun `with the environment variable PORT set`() {
withEnvironment(PORT to "9292") {
port() shouldBe 9292
}
}

@Test
fun `with the environment variables SERVER_PORT and PORT set`() {
withEnvironment(mapOf(SERVER_PORT to "9292", PORT to "9393")) {
port() shouldBe 9292
}
}


@Test
fun `load oauth2Config from file`() {
withEnvironment(JSON_CONFIG_PATH to configFile) {
Expand Down

0 comments on commit 249b53c

Please sign in to comment.