dart_frog icon indicating copy to clipboard operation
dart_frog copied to clipboard

feat: Add support for kDebugMode

Open paurakhsharma opened this issue 2 years ago • 1 comments

Description Add support for kDebugMode like in Flutter. So, that I can have different logic if I am running the server in debug mode or in production mode.

Additional Context I am using a database with dart_frog, I want to load the environment variables using a .env file while running the app in development mode. But I want to use the environment variables from Platform environment i.e server console when the app is running in production mode.

Currently I am doing something like this:

final env = DotEnv(includePlatformEnvironment: true);

final _db = DatabaseConnection(env);

Handler middleware(Handler handler) {
  try {
    env.load();
  } catch (e) {
    // ignore error as it is expected in production
  }

  return handler
      .use(requestLogger())
      .use(provider<DatabaseConnection>((_) => _db));
}

This works just fine. But it would be better if I could do something like this instead of catching the error.

final env = DotEnv(includePlatformEnvironment: !kDebugMode);

final _db = DatabaseConnection(env);

Handler middleware(Handler handler) {
  if (kDebugMode) {
    env.load();
  }

  return handler
      .use(requestLogger())
      .use(provider<DatabaseConnection>((_) => _db));
}

Note: May be it is better to call it kDevMode instead.

paurakhsharma avatar Feb 07 '23 03:02 paurakhsharma

I'm doing the same, using a .env file in dev and no file in prd.

An alternative without try/catch:

env = DotEnv(includePlatformEnvironment: true)
            ..load(File('.env').existsSync() ? const ['.env'] : const []);

In DotEnv load() you can pass an array with files you want to load (or no file with an empty array)

GabrielRozendo avatar Feb 08 '23 10:02 GabrielRozendo