What is a good place to store configuration in Google AppEngine (python)

前端 未结 3 1895
死守一世寂寞
死守一世寂寞 2020-12-15 08:00

I am making a Google AppEngine application and am doubting were I should store (sensitive) configuration data like credentials.

Should I make a single bigtable enti

3条回答
  •  孤城傲影
    2020-12-15 08:37

    If it's sensitive data, you should not store it in source code as it will be checked into source control. The wrong people (inside or outside your organization) may find it there. Also, your development environment probably uses different config values from your production environment. If these values are stored in code, you will have to run different code in development and production, which is messy and bad practice.

    In my projects, I put config data in the datastore using this class:

    from google.appengine.ext import ndb
    
    class Settings(ndb.Model):
      name = ndb.StringProperty()
      value = ndb.StringProperty()
    
      @staticmethod
      def get(name):
        NOT_SET_VALUE = "NOT SET"
        retval = Settings.query(Settings.name == name).get()
        if not retval:
          retval = Settings()
          retval.name = name
          retval.value = NOT_SET_VALUE
          retval.put()
        if retval.value == NOT_SET_VALUE:
          raise Exception(('Setting %s not found in the database. A placeholder ' +
            'record has been created. Go to the Developers Console for your app ' +
            'in App Engine, look up the Settings record with name=%s and enter ' +
            'its value in that record\'s value field.') % (name, name))
        return retval.value
    

    Your application would do this to get a value:

    AMAZON_KEY = Settings.get('AMAZON_KEY')
    

    If there is a value for that key in the datastore, you will get it. If there isn't, a placeholder record will be created and an exception will be thrown. The exception will remind you to go to the Developers Console and update the placeholder record.

    I find this takes the guessing out of setting config values. If you are unsure of what config values to set, just run the code and it will tell you!

提交回复
热议问题