Vapor Documentation Beta

Structure Environment

public struct Environment: Equatable

The environment the application is running in, i.e., production, dev, etc. All Containers will have an Environment that can be used to dynamically register and configure services.

switch env {
case .production:
    app.http.server.configuration = ...
default: break
}

The Environment can also be used to retrieve variables from the Process' ENV.

print(Environment.get("DB_PASSWORD"))
%3 Environment Environment Equatable Equatable Environment->Equatable

Nested Types

Environment.Process

The process information of an environment. Wraps ProcessInto.processInfo.

Conforms To

Equatable

Initializers

init(name:​arguments:​)

public init(name: String, arguments: [String] = CommandLine.arguments)

Create a new Environment.

Properties

production

var production: Environment

An environment for deploying your application to consumers.

development

var development: Environment

An environment for developing your application.

testing

var testing: Environment

An environment for testing your application.

Performs an explicit sanitization step because this preset is often used directly in unit tests, without the benefit of the logic usually invoked through either form of detect(). This means that when --env test is explicitly specified, the sanitize logic is run twice, but this should be harmless.

process

var process: Process

The current process of the environment.

name

let name: String

The environment's unique name.

is​Release

var isRelease: Bool

true if this environment is meant for production use cases.

This usually means reducing logging, disabling debug information, and sometimes providing warnings about configuration states that are not suitable for production.

arguments

var arguments: [String]

The command-line arguments for this Environment.

command​Input

var commandInput: CommandInput

Exposes the Environment's arguments property as a CommandInput.

Methods

secret(key:​file​IO:​on:​)

public static func secret(key: String, fileIO: NonBlockingFileIO, on eventLoop: EventLoop) -> EventLoopFuture<String?>

Reads a file's content for a secret. The secret key is the name of the environment variable that is expected to specify the path of the file containing the secret.

Example usage:

func configure(_ app: Application) {
    // ...

    let databasePassword = try Environment.secret(
        key: "DATABASE_PASSWORD_FILE",
        fileIO: app.fileio,
        on: app.eventLoopGroup.next()
    ).wait()

Parameters

key String

The environment variable name

file​IO Non​Blocking​File​IO

NonBlockingFileIO handler provided by NIO

event​Loop Event​Loop

EventLoop for NIO to use for working with the file

secret(path:​file​IO:​on:​)

public static func secret(path: String, fileIO: NonBlockingFileIO, on eventLoop: EventLoop) -> EventLoopFuture<String?>

Load the content of a file at a given path as a secret.

Parameters

path String

Path to the file containing the secret

file​IO Non​Blocking​File​IO

NonBlockingFileIO handler provided by NIO

event​Loop Event​Loop

EventLoop for NIO to use for working with the file

Returns

  • On success, a succeeded future with the loaded content of the file.
  • On any kind of error, a succeeded future with a value of nil. It is not currently possible to get error details.

detect(arguments:​)

public static func detect(arguments: [String] = CommandLine.arguments) throws -> Environment

Detects the environment from CommandLine.arguments. Invokes detect(from:).

Parameters

arguments [String]

Command line arguments to detect environment from.

Returns

The detected environment, or default env.

detect(from:​)

public static func detect(from commandInput: inout CommandInput) throws -> Environment

Detects the environment from CommandInput. Parses the --env flag, with the VAPOR_ENV environment variable as a fallback.

Parameters

arguments

CommandInput to parse --env flag from.

Returns

The detected environment, or default env.

custom(name:​)

public static func custom(name: String) -> Environment

Creates a custom environment.

get(_:​)

public static func get(_ key: String) -> String?

Gets a key from the process environment

==(lhs:​rhs:​)

public static func ==(lhs: Environment, rhs: Environment) -> Bool

See Equatable