简体   繁体   中英

Read YAML config through Rest API

I have a really complicated system which use multiple languages and frameworks (Java Python Scala Bash). In each module I need to retrieve configuration values which are similar and change frequently. Currently I'm maintaining multiple conf files which holds lots of duplicates. I wonder if there is out of the box RestAPI which can retrieve variables by demand from remote location.

All I manage to find by now are ways to load the entire file from remote source which is half a solution from me:

YAML.parse(open('https://link_to_file/file.yaml'))

My goal, which I fail to find a lead to it, is to make a direct call.

MyRemoteAPI.get("level1.level2.x")

PS YAML is not mandatory solution for me, I'm Open for suggestions.

I don't know about an out-of-the-box API, but it's fairly trivial to build. Make a service that will read the YAML file and traverse to the appropriate key. eg using a dynamic language like Ruby (+Rails), you could do something like

def value
  config = YAML.load_file '/local/path/to/config.yaml'
  render plain: config.dig(params[:key].split('.'))
end

dig essentially traverses a structure and safely returns nil if a key isn't found, so this returns the value at the "leaf" of the requested path.

You might also want to cache the structure in memory to prevent constantly reading from the file, eg could do something like @@config ||= YAML.parse(open('https://link_to_file/file.yaml')) or config = Rails.cache.fetch('config', expire_in: 1.hour) { ... } . And/or cache the API's HTTP response.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM