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

Rename dotenv.testLoad to dotenv.loadString #85

Open
lukepighetti opened this issue May 5, 2023 · 1 comment
Open

Rename dotenv.testLoad to dotenv.loadString #85

lukepighetti opened this issue May 5, 2023 · 1 comment

Comments

@lukepighetti
Copy link

lukepighetti commented May 5, 2023

I think we can provide a little more clarity on loading env files from a string by changing the name of this method so it's clear that this is useful outside of test scenarios

Why would you want to load an environment variable from a string in a production app?

Ideally .env files are not baked into the app package assets because they are easy to get access to. We can provide a little bit of obfuscation by loading them into the binary as a compile time string.

ENVIRONMENT=DEVELOPMENT
FASTAPI_KEY=my_public_key
FASTAPI_HOST=localhost
FASTAPI_PORT=8000
// .vscode/launch.json
{
  "version": "0.2.0",
  "configurations": [
    {
      "name": "app",
      "request": "launch",
      "type": "dart",
      "args": ["--dart-define=ENV=$(cat .env | base64)"] // pull the .env file from filesystem and pipe it to const string via base64
    }
  ]
}
// config.dart
void main() {
  const b64env = String.fromEnvironment('ENV'); // load the base64 encoded .env file
  dotenv.testLoad(fileInput: b64env.fromBase64()); // load it into dotenv, << this is where I think we can add some clarity
  config = Config();
}

class Config {
  final environment = Environment.values.byName(dotenv.get('ENVIRONMENT'));
  final fastApiPort = dotenv.getInt('FASTAPI_PORT').assertPortNumber();
  final fastApiHost = dotenv.get('FASTAPI_HOST').assertNotEmpty();
  final fastApiKey = dotenv.get('FASTAPI_KEY').assertNotEmpty();
}

extension StringExtensions on String {
  String fromBase64() {
    return utf8.decode(base64.decode(this));
  }
}
@lukepighetti
Copy link
Author

lukepighetti commented May 5, 2023

If we wanted to get even more sugary, we could add dotenv.loadBase64String()

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

No branches or pull requests

1 participant