Skip to content

Commit

Permalink
Include system environment in application command parse (#20)
Browse files Browse the repository at this point in the history
* Include system environment in application command parse

* add environment variable configuration options

* fix syntax
  • Loading branch information
jisantuc authored Oct 16, 2020
1 parent 661c014 commit cccf1e0
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ object Server extends IOApp {
override def run(args: List[String]): IO[ExitCode] = {
import Commands._

applicationCommand.parse(args) map {
applicationCommand.parse(args, env = sys.env) map {
case RunServer(apiConfig, dbConfig) =>
createServer(apiConfig, dbConfig)
.use(_ => IO.never)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,34 +7,35 @@ import com.monovore.decline.refined._

trait ApiOptions {

private val externalPort = Opts
.option[PosInt]("external-port",
help = "Port users/clients hit for requests")
private val externalPortHelp = "Port users/clients hit for requests"
private val externalPort = (Opts
.option[PosInt]("external-port", help = externalPortHelp)
.orElse(Opts.env[PosInt]("API_EXTERNAL_PORT", help = externalPortHelp)))
.withDefault(PosInt($default_api_port$))

private val internalPort = Opts
.option[PosInt](
"internal-port",
help =
"Port server listens on, this will be different from 'external-port' when service is started behind a proxy"
)
private val internalPortHelp =
"Port server listens on, this will be different from 'external-port' when service is started behind a proxy"
private val internalPort = (Opts
.option[PosInt]("internal-port", help = internalPortHelp)
.orElse(Opts.env[PosInt]("API_INTERNAL_PORT", help = internalPortHelp)))
.withDefault(PosInt($default_api_port$))

private val apiHost = Opts
.option[String]("api-host",
help = "Hostname $name$ is hosted it (e.g. localhost)")
private val apiHostHelp = "Hostname $name$ is hosted it (e.g. localhost)"
private val apiHost = (Opts
.option[String]("api-host", help = apiHostHelp)
.orElse(Opts.env[String]("API_HOST", help = apiHostHelp)))
.withDefault("localhost")

private val apiSchemeHelp = "Scheme server is exposed to end users with"
private val apiScheme =
Opts
.option[String]("api-scheme",
"Scheme server is exposed to end users with")
(Opts
.option[String]("api-scheme", help = apiSchemeHelp)
.orElse(Opts.env[String]("API_SCHEME", help = apiSchemeHelp)))
.withDefault("http")
.validate("Scheme must be either 'http' or 'https'")(s =>
(s == "http" || s == "https"))
(s == "http" || s == "https")
)

val apiConfig: Opts[ApiConfig] = (externalPort,
internalPort,
apiHost,
apiScheme) mapN ApiConfig
val apiConfig: Opts[ApiConfig] =
(externalPort, internalPort, apiHost, apiScheme) mapN ApiConfig
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,24 +15,34 @@ import scala.util.Try

trait DatabaseOptions {

private val databasePort = Opts
.option[PosInt]("db-port", help = "Port to connect to database on")
private val databasePortHelp = "Port to connect to database on"
private val databasePort = (Opts
.option[PosInt]("db-port", help = databasePortHelp)
.orElse(Opts.env[PosInt]("POSTGRES_PORT", help=databasePortHelp)))
.withDefault(PosInt($default_db_port$))

private val databaseHost = Opts
.option[String]("db-host", help = "Database host to connect to")
private val databaseHostHelp = "Database host to connect to"
private val databaseHost = (Opts
.option[String]("db-host", help = databaseHostHelp)
.orElse(Opts.env[String]("POSTGRES_HOST", help=databaseHostHelp)))
.withDefault("localhost")

private val databaseName = Opts
.option[String]("db-name", help = "Database name to connect to")
private val databaseNameHelp = "Database name to connect to"
private val databaseName = (Opts
.option[String]("db-name", help = databaseNameHelp)
.orElse(Opts.env[String]("POSTGRES_NAME", help = databaseNameHelp)))
.withDefault("$name;format="norm"$")

private val databasePassword = Opts
.option[String]("db-password", help = "Database password to use")
private val databasePasswordHelp = "Database password to use"
private val databasePassword = (Opts
.option[String]("db-password", help = databasePasswordHelp)
.orElse(Opts.env[String]("POSTGRES_PASSWORD", help = databasePasswordHelp)))
.withDefault("$name;format="norm"$")
private val databaseUser = Opts
.option[String]("db-user", help = "User to connect with database with")
private val databaseUserHelp = "User to connect with database with"
private val databaseUser = (Opts
.option[String]("db-user", help = databaseUserHelp)
.orElse(Opts.env[String]("POSTGRES_USER", help = databaseUserHelp)))
.withDefault("$name;format="norm"$")
def databaseConfig(
Expand Down

0 comments on commit cccf1e0

Please sign in to comment.