staticbackendhq / core

Backend server API handling user mgmt, database, storage and real-time component
https://staticbackend.com
MIT License
682 stars 66 forks source link

Using environment variables from .env file under vscode #93

Closed g41797 closed 1 year ago

g41797 commented 1 year ago

Is your feature request related to a problem? Please describe. go code reads os(shell) environment variables via os.Getenv("name") API. In order to get values for development saved in .env file, path to this file should be added to json config files of IDE. Cumbersome process.

Describe the solution you'd like

Use new config.Getenv("name")

  1. [Once] Read .env file and inject values to environment (using godotenv )
  2. call os.Getenv

Describe alternatives you've considered Add env variables to .vscode/launch.json and save it in repository something like:

            "env": {
                "DEV_ENV": "true",
                "DOCKER_ENV": "true",
            }

Additional context Example of godotenv usage

dstpierre commented 1 year ago

Maybe instead of bringing another dependency the config.LoadConfig() function could check for presence of .env and load the AppConfig based on those values instead of getting them from the system environment variables.

Something like:

config/config.go line:85

func LoadConfig() AppConfig {
  if .env exists {
    return loadFromEnvFile()
  }

  // load from system environement variables
}

Format of .env is fairly straightforward to parse, maybe into a map[string]string spliting by \n and by = would give key, value, from there the AppConfig could be filled with all values.

What do you think?

g41797 commented 1 year ago

see Example of vscode conf

Now I think to add .vscode/launch.json with some default configurations will be simplest solution