Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

validate port? #72

Closed
kennedymwavu opened this issue Oct 9, 2024 · 2 comments · Fixed by #75
Closed

validate port? #72

kennedymwavu opened this issue Oct 9, 2024 · 2 comments · Fixed by #75
Assignees

Comments

@kennedymwavu
Copy link
Contributor

explanation

should we do checks on the provided port?
most of the times during deployment, the port number is fetched from an env file.
however, if the env var is not set (mostly just forgetting), Sys.getenv() returns an empty string. if passed to ambiorix, it is coerced into NA by as.integer().
this has bitten me a few times.

reprex

library(ambiorix)

home_get <- \(req, res) {
  res$send("Ambiorix")
}

Ambiorix$
  new(port = "")$
  get("/", home_get)$
  start(open = TRUE)

when you run the above, you get this on the console:

09-10-2024 21:43:46 Listening on http://0.0.0.0:NA

question

should we validate that the given port is indeed an integer and not NA_integer_? throw error maybe?

hint

this should suffice:

port <- as.integer(given_port)
if (identical(port, NA_integer_)) {
  stop(sprintf("Invalid port: '%s'. Port must be a valid integer.", given_port))
}

this change can be made just before passing the port to httpuv::startServer(), here

@JohnCoene
Copy link
Collaborator

JohnCoene commented Oct 9, 2024

You're correct that better checks should be passed but we should also take that opportunity to rethink this a bit, namely in light of your discovery that it can be deployed on shiny server.

Perhaps the port argument could default to a convenience function that resolved the port somewhat intelligently, e.g.: port = get_port() which:

  1. Check if environment variable is declared, if so uses that
  2. Check if Ambiorix port is set, if so..
  3. Check shiny port option, ...
    4.return random port

What do you think?

@kennedymwavu
Copy link
Contributor Author

that'd be great!
AMBIORIX_PORT would be a good name for the env var, no?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants