保持web应用程序配置的最佳方式是什么?

时间:2021-10-15 00:16:58

If I have some config for web admin to set e.g. number of post per page, some enum showing choice. How should I keep this settings in db ? Should I serialize it and save as blob.

如果我有一些设置为网络管理员设置,例如每个页面的帖子数量,一些枚举显示选择。如何将这些设置保存在db中?我应该序列化它并保存为blob。

Thanks,

谢谢,


I using rails and I want it to dynamically change this setting through web interface, so I think environment.rb would not fit this situation. So I should have a extra table with two tuples as name, value ?

我使用rails,我想让它通过web界面动态地改变这个设置,所以我认为是环境。rb不适合这种情况。所以我应该有一个包含两个元组的表作为名称,值?

6 个解决方案

#1


1  

Most languages/frameworks have a config file of sorts. such as the web.config in ASP or the environment.rb files in RoR. You could use one of these.

大多数语言/框架都有各种配置文件。如网络。配置在ASP或环境中。在RoR rb文件。你可以用其中一个。

Or failing that have a key value pair table in your database.

否则,在数据库中有一个键值对表。

If you're wanting to do this dynamically through the website I would definitely go for the key value pair table.

如果你想在网站上动态地做这个,我肯定会选择键值对表。

#2


1  

For the dynamic config values, you should create a model called Configuration with keys and values. I generally have multiple value columns (for number, string, and date) and then call the appropriate method for the configuration.

对于动态配置值,应该创建一个名为Configuration的模型,其中包含键和值。我通常有多个值列(用于数字、字符串和日期),然后为配置调用适当的方法。

For "enums" you should create lookup tables with foreign key relationships to where they attach. For example if you have a Post model and you want an enumeration of Category, you should make the Post belong_to :category and Category has_many :posts.

对于“enums”,应该创建具有外键关系的查找表。例如,如果您有一个Post模型,并且您想要对Category进行枚举,那么应该将Post belong_to: Category和Category设置为has_many:posts。

#3


1  

Use a YAML file. YAML is way simpler than XML.

使用YAML文件。YAML比XML简单得多。

Make a file called "config.yml" in "config" directory. And load the file using YAML::load(). You can make a setting for each environment by naming the first level as environment (e.g., production, development, test).

创建一个名为“config”的文件。yml”在“配置”目录。并使用YAML::load()加载文件。您可以为每个环境创建一个设置,方法是将第一个级别命名为environment(例如,生产、开发、测试)。

See this episode of RailsCasts for details.

详情请参阅本期的铁路广播。

#4


0  

If you are using asp.net you can use the Web.Config file.

如果你正在使用asp.net,你可以使用Web。配置文件。

See Asp .net Web.config Configuration File

看到Asp . net Web。配置配置文件

#5


0  

You could to create a single table in your database to store key-value pairs.

您可以在数据库中创建一个表来存储键-值对。

#6


0  

This is what I use. Got the idea from elsewhere, but the implementation is mine. Pulled from a production project of mine:

这就是我用的。从其他地方得到这个想法,但是实现是我的。来自我的一个生产项目:

class AppConfig
  # Loads a YAML configuration file from RAILS_ROOT/config/. The default file
  # it looks for is 'application.yml', although if this doesn't match your
  # application, you can pass in an alternative value as an argument 
  # to AppConfig.load.
  # After the file has been loaded, any inline ERB is evaluated and unserialized
  # into a hash. For each key-value pair in the hash, class getter and setter methods
  # are defined i.e., AppConfig.key => "value" 
  # This allows you to store your application configuration information e.g., API keys and 
  # authentication credentials in a convenient manner, external to your application source
  #
  # application.yml example
  #
  # :defaults: &defaults
  #  :app_name: Platform
  #  :app_domain: dev.example.com
  #  :admin_email: admin@example.com
  # :development:
  #  <<: *defaults
  # :test:
  #  <<: *defaults
  # :production:
  #  <<: *defaults
  #  :app_domain: example.com
  #
  # For example will result in AppConfig.app_domain => "dev.example.com"
  # when Rails.env == "development" 
  #

  class << self
    def load(file='application.yml')
      configuration_file = File.join Rails.root, 'config', file
      File.open(configuration_file) do |configuration|
        configuration = ERB.new(configuration.read).result
        configuration = YAML.load(configuration)[Rails.env.to_sym]
        configuration.each do |key, value|
          cattr_accessor key
          send "#{key}=", value
        end
      end if File.exists? configuration_file
    end
  end
end
AppConfig.load

Create config/initializers/app_config.rb and paste the above code into it. I'm going to make this into a gem. I figure other people will find it useful.

创建配置/初始化/ app_config。rb并将上面的代码粘贴到其中。我要把它做成宝石。我想其他人会觉得它有用。

EDIT: Just saw you wish to edit the config as the app runs via a web based interface. You could do this with this method as both getter and setter methods are defined for each attribute.

编辑:当应用程序通过基于web的界面运行时,看到您希望编辑配置。您可以使用这个方法,因为每个属性都定义了getter和setter方法。

in your controller:

在你的控制器:

def update
  params[:configuration].each { |k,v| AppConfig.send "#{k}=", v }
  …
end

I don't find a model is the right solution here. Forget about the DB overheard, the idea of being able to instantiate something that controls app configuration doesn't make sense. What's more how you implement it? An instance for each tuple?! It should be a singleton class.

我不认为模型是正确的解。忘掉DB窃听吧,能够实例化控制应用程序配置的东西是没有意义的。你如何实现它?每个元组的实例?它应该是一个单例类。

#1


1  

Most languages/frameworks have a config file of sorts. such as the web.config in ASP or the environment.rb files in RoR. You could use one of these.

大多数语言/框架都有各种配置文件。如网络。配置在ASP或环境中。在RoR rb文件。你可以用其中一个。

Or failing that have a key value pair table in your database.

否则,在数据库中有一个键值对表。

If you're wanting to do this dynamically through the website I would definitely go for the key value pair table.

如果你想在网站上动态地做这个,我肯定会选择键值对表。

#2


1  

For the dynamic config values, you should create a model called Configuration with keys and values. I generally have multiple value columns (for number, string, and date) and then call the appropriate method for the configuration.

对于动态配置值,应该创建一个名为Configuration的模型,其中包含键和值。我通常有多个值列(用于数字、字符串和日期),然后为配置调用适当的方法。

For "enums" you should create lookup tables with foreign key relationships to where they attach. For example if you have a Post model and you want an enumeration of Category, you should make the Post belong_to :category and Category has_many :posts.

对于“enums”,应该创建具有外键关系的查找表。例如,如果您有一个Post模型,并且您想要对Category进行枚举,那么应该将Post belong_to: Category和Category设置为has_many:posts。

#3


1  

Use a YAML file. YAML is way simpler than XML.

使用YAML文件。YAML比XML简单得多。

Make a file called "config.yml" in "config" directory. And load the file using YAML::load(). You can make a setting for each environment by naming the first level as environment (e.g., production, development, test).

创建一个名为“config”的文件。yml”在“配置”目录。并使用YAML::load()加载文件。您可以为每个环境创建一个设置,方法是将第一个级别命名为environment(例如,生产、开发、测试)。

See this episode of RailsCasts for details.

详情请参阅本期的铁路广播。

#4


0  

If you are using asp.net you can use the Web.Config file.

如果你正在使用asp.net,你可以使用Web。配置文件。

See Asp .net Web.config Configuration File

看到Asp . net Web。配置配置文件

#5


0  

You could to create a single table in your database to store key-value pairs.

您可以在数据库中创建一个表来存储键-值对。

#6


0  

This is what I use. Got the idea from elsewhere, but the implementation is mine. Pulled from a production project of mine:

这就是我用的。从其他地方得到这个想法,但是实现是我的。来自我的一个生产项目:

class AppConfig
  # Loads a YAML configuration file from RAILS_ROOT/config/. The default file
  # it looks for is 'application.yml', although if this doesn't match your
  # application, you can pass in an alternative value as an argument 
  # to AppConfig.load.
  # After the file has been loaded, any inline ERB is evaluated and unserialized
  # into a hash. For each key-value pair in the hash, class getter and setter methods
  # are defined i.e., AppConfig.key => "value" 
  # This allows you to store your application configuration information e.g., API keys and 
  # authentication credentials in a convenient manner, external to your application source
  #
  # application.yml example
  #
  # :defaults: &defaults
  #  :app_name: Platform
  #  :app_domain: dev.example.com
  #  :admin_email: admin@example.com
  # :development:
  #  <<: *defaults
  # :test:
  #  <<: *defaults
  # :production:
  #  <<: *defaults
  #  :app_domain: example.com
  #
  # For example will result in AppConfig.app_domain => "dev.example.com"
  # when Rails.env == "development" 
  #

  class << self
    def load(file='application.yml')
      configuration_file = File.join Rails.root, 'config', file
      File.open(configuration_file) do |configuration|
        configuration = ERB.new(configuration.read).result
        configuration = YAML.load(configuration)[Rails.env.to_sym]
        configuration.each do |key, value|
          cattr_accessor key
          send "#{key}=", value
        end
      end if File.exists? configuration_file
    end
  end
end
AppConfig.load

Create config/initializers/app_config.rb and paste the above code into it. I'm going to make this into a gem. I figure other people will find it useful.

创建配置/初始化/ app_config。rb并将上面的代码粘贴到其中。我要把它做成宝石。我想其他人会觉得它有用。

EDIT: Just saw you wish to edit the config as the app runs via a web based interface. You could do this with this method as both getter and setter methods are defined for each attribute.

编辑:当应用程序通过基于web的界面运行时,看到您希望编辑配置。您可以使用这个方法,因为每个属性都定义了getter和setter方法。

in your controller:

在你的控制器:

def update
  params[:configuration].each { |k,v| AppConfig.send "#{k}=", v }
  …
end

I don't find a model is the right solution here. Forget about the DB overheard, the idea of being able to instantiate something that controls app configuration doesn't make sense. What's more how you implement it? An instance for each tuple?! It should be a singleton class.

我不认为模型是正确的解。忘掉DB窃听吧,能够实例化控制应用程序配置的东西是没有意义的。你如何实现它?每个元组的实例?它应该是一个单例类。