0
votes

I am porting a golang service from App Engine standard environment to the flexible environment, and have a question about accessing app.yaml during development/testing.

In my app.yaml I have a section where I set environment variables, that I later access in the code via os.GetEnv(...):

env_variables:
  FORGE_CLIENT_ID: 'my-client-id'
  FORGE_CLIENT_SECRET: 'my-client-secret'

In the App Engine standard environment, this runs fine, as I am using the app engine development server dev-server.py, which I believe takes care of reading the app.yaml file and making these environment variables available.

However, in the flexible environment during development, the service is started simply with go run *.go, and the app doesn't seem to pick up any information in app.yaml, meaning I get an error that my environment variables are not set.

I understand that app.yaml is used during deployment in the flexible environment, but I don't understand how or if it is used during development. How do I access these environment variables from my development server?

Thanks.

2

2 Answers

0
votes

You will need to if/else based on whatever denotes dev vs live, then you can simply parse the yaml file to obtain and set these variables. For instance, one of my services (still running on standard mind you) is still using the old SDK where the version is required in the yaml. I parse it out like so:

import "github.com/ghodss/yaml" // or your choice

type AppVersion struct {
    Version string `json:"version"`
}

func VersionID() (string, error) {
    dat, err := ioutil.ReadFile("app.yaml")
    if err != nil {
        return "", err
    }
    a := &AppVersion{}
    err = yaml.Unmarshal(dat, a)
    if err != nil {
        return "", err
    }
    return a.Version, nil
}

Obviously this is then stored in a global so I don't have to open the file each time, but it wouldn't really matter if it's only parsed on local dev. Your case would probably best be served by os.SetEnv This is just one possible solution that works for me.

0
votes

For Google App Engine flexible, the dev server don't use "app.yaml", as the configuration is set locally. A workaround for this would be to set the environment variables before running the dev server. You can check this question for setting those variables just for that command.