Python和YAML- GAE应用设置文件 [英] Python and YAML- GAE app settings file

查看:83
本文介绍了Python和YAML- GAE应用设置文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在阅读有关使用YAML在GAE应用程序中存储设置的信息,我想我想采用这种方式.我说的是我自己的常量(例如API密钥和临时变量),而不是标准的GAE配置文件(例如app.yaml).

I was reading about using YAML to store settings in a GAE application and I think I want to go this way. I'm talking about my own constants like API keys and temp variables, NOT about standard GAE config files like app.yaml.

到目前为止,我为此使用了一个简单的settings.py文件(该文件包含用于生产/测试/等环境的单独配置),但似乎做得还不够好.

So far I used a simple settings.py file for it (which included separate configs for production/testing/etc environment), but it doesn't seem to do the job well enough.

当git merge覆盖了某些设置(难以控制)时,我什至遇到了一些严重的问题.

I even had some serious problems when a git merge overwrote some settings (hard to control it).

最终,我想在数据存储区中存储尽可能多的数据,但就目前而言,我正在寻找想法.

Eventually I want to store as much data as I can in the data store but as for now I'm looking for ideas.

那么有人对简单存储和访问有时受保护的配置数据有任何想法或示例吗?

So does anybody have any ideas or examples for simple storing and access of, sometimes protected, config data?

推荐答案

在静态文件中存储API密钥和配置变量通常 总是一个坏主意,原因有几个:

Storing the API keys and configuration variables in static files is usually always a bad idea for a few reasons:

  • 难以更新:您需要部署新应用才能更新值
  • 不安全:您可以将其提交到版本控制系统,并且无法退回
  • 不扩展:通常您为不同的域使用不同的密钥
  • Hard to update: you need to deploy a new app in order to update the values
  • Not secure: you might commit it to the version control system and there is no way back
  • Don't scale: very often you have different keys for different domains

所以为什么不从一开始就将所有这些值立即安全地存储在数据存储中,尤其是在实际上如此简单得多并且可以使用以下方法时:

So why not storing all these values right from the very beginning securely in the datastore, especially when it's actually so much easier and here is how:

您要做的就是为您的配置值创建一个新模型,并且其中仅包含一条记录.通过使用NDB,您可以立即使用缓存,并且由于此配置文件的性质,您甚至可以针对每个正在运行的实例缓存它,以避免对数据存储区进行定期读取.

All you have to do is to create one new model for your configuration values and have only one record in it. By using NDB, you have caching out of the box and because of the nature of this configuration file you can even cache it per running instance to avoid regular reads to the datastore.

class Config(ndb.Model):
  analytics_id = ndb.StringProperty(default='')
  brand_name = ndb.StringProperty(default='my-awesome-app')
  facebook_app_id = ndb.StringProperty(default='')
  facebook_app_secret = ndb.StringProperty(default='')

  @classmethod
  def get_master_db(cls):
    return cls.get_or_insert('master')

我还猜测您已经有一个文件很可能叫做config.py,其中有一些像这样的常量:

I'm also guessing that you already have one file that most likely is called config.py where you have some constants like these ones:

PRODUCTION = os.environ.get('SERVER_SOFTWARE', '').startswith('Google App Engine')
DEVELOPMENT = not PRODUCTION

如果您不创建一个文件,或者也将其添加到该文件中,则:

If you don't create one or if you do just add this to that file as well:

import model   # Your module that contains the models

CONFIG_DB = model.Config.get_master_db()

最后,要读取配置值,您可以在应用程序中的任何位置(当然,在导入config.py之后)都可以简单地做到这一点:

and finally to be able to read your config values you can simply do that, wherever in your app (after importing the config.py of course):

config.CONFIG_DB.brand_name

从现在开始,您甚至不必创建任何特殊表单来更新这些值(尽管建议这样做),因为您可以从本地管理控制台或生产环境中的仪表板进行操作.

From now on you don't even have to create any special forms to update these values (it is recommended though), because you can do that from the admin console locally or the Dashboard on the production.

请记住,如果要将记录存储在本地变量中,则必须重新启动

Just remember that if you're going to store that record in a local variable you will have to restart the instances after updating the values to see the changes.

以上所有内容,您都可以在我的一个名为的开源项目中看到. gae-init .

All of the above you can see it in action, in one of my open sourced projects called gae-init.

这篇关于Python和YAML- GAE应用设置文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆