Group Group Group Group Group Group Group Group Group

Environment variables are nil [Vapor]

Environment variables are nil, what am I doing wrong?

public func configure(
    _ env: inout Environment,
    _ config: inout Config,
    _ services: inout Services
) throws {
    // MARK: - Router
    let router = EngineRouter.default()
    try routes(router)
    services.register(router, as: Router.self)
    // MARK: - Leaf
    try services.register(LeafProvider())
    config.prefer(LeafRenderer.self, for: ViewRenderer.self)
    // MARK: - Database
    try services.register(FluentMySQLProvider())
        let hostName = Environment.get("DB_HOSTNAME"),
        let dbPort = Environment.get("DB_PORT"),
        let intDBPort = Int(dbPort),
        let userName = Environment.get("DB_USERNAME"),
        let password = Environment.get("DB_PASSWORD"),
        let database = Environment.get("DB")
    else {
        throw Abort(.internalServerError)
    let databaseConfig = MySQLDatabaseConfig(hostname: hostName,
                                            port: intDBPort,
                                            username: userName,
                                            password: password,
                                            database: database,
                                            transport: .unverifiedTLS)
    var migrationConfig = MigrationConfig()
    migrationConfig.add(model: SheetData.self, database: .mysql)
    migrationConfig.add(model: Language.self, database: .mysql)
    migrationConfig.add(model: Sheet.self, database: .mysql)

environment.plist has the required info though

@0xtim Can you please help with this when you get a chance? Thank you - much appreciated! :]

1 Like

@lganti plists don’t work like they do on iOS. You have a few options - the two best being you can define environment variables in your targets scheme in Xcode or use dotEnv files with a community package like in this example - hope that helps!

1 Like