|
1 |
| -net_box = require('net.box') |
2 |
| -local conn = net_box.connect('127.0.0.1:4401') |
3 | 1 | local log = require('log')
|
4 |
| -conn:watch('config.storage:/myapp/config/all', function(key, value) |
5 |
| - log.info("Configuration stored by the '/myapp/config/all' key is changed") |
6 |
| -end) |
| 2 | +config = require('config') |
| 3 | + |
| 4 | +storage_client = config.storage_client.connect({ |
| 5 | + { |
| 6 | + uri = '127.0.0.1:4401', |
| 7 | + login = 'sampleuser', |
| 8 | + password = '123456', |
| 9 | + }, |
| 10 | + { |
| 11 | + uri = '127.0.0.1:4402', |
| 12 | + login = 'sampleuser', |
| 13 | + password = '123456', |
| 14 | + }, |
| 15 | + { |
| 16 | + uri = '127.0.0.1:4403', |
| 17 | + login = 'sampleuser', |
| 18 | + password = '123456', |
| 19 | + }, |
| 20 | +}) |
| 21 | + |
| 22 | +function register_watchers() |
| 23 | + storage_client:watch('/myapp/config/all', function(_, revision) |
| 24 | + log.info("Configuration stored by the '/myapp/config/all' key is " .. |
| 25 | + "changed. New revision number is %d.", revision) |
| 26 | + end) |
| 27 | +end |
| 28 | + |
| 29 | +register_watchers() |
| 30 | + |
| 31 | +function connect_to_configured_storage() |
| 32 | + -- `config.storage.endpoints` configuration section can be |
| 33 | + -- passed directly as `connect()` options to connect to the |
| 34 | + -- configured config.storage cluster. |
| 35 | + local endpoints = config:get('config.storage.endpoints') |
| 36 | + local storage_client = config.storage_client.connect(endpoints) |
| 37 | + |
| 38 | + return storage_client |
| 39 | +end |
| 40 | + |
| 41 | +function put_config() |
| 42 | + local fio = require('fio') |
| 43 | + local cluster_config_handle = fio.open('../../source.yaml', {'O_RDONLY'}) |
| 44 | + local cluster_config = cluster_config_handle:read() |
| 45 | + local response = storage_client:put('/myapp/config/all', cluster_config) |
| 46 | + cluster_config_handle:close() |
| 47 | + |
| 48 | + return response |
| 49 | +end |
| 50 | + |
| 51 | +function get_config_by_path() |
| 52 | + local response = storage_client:get('/myapp/config/all') |
| 53 | + |
| 54 | + return response |
| 55 | +end |
| 56 | + |
| 57 | +function get_config_by_prefix() |
| 58 | + local response = storage_client:get('/myapp/') |
| 59 | + |
| 60 | + return response |
| 61 | +end |
| 62 | + |
| 63 | +function make_txn_request() |
| 64 | + -- Execute an atomic request on the config.storage cluster. |
| 65 | + local response = storage_client:txn({ |
| 66 | + predicates = { { 'value', '==', 'v0', '/myapp/config/all' } }, |
| 67 | + on_success = { { 'put', '/myapp/config/all', 'v1' } }, |
| 68 | + on_failure = { { 'get', '/myapp/config/all' } } |
| 69 | + }) |
| 70 | + |
| 71 | + return response |
| 72 | +end |
| 73 | + |
| 74 | +function delete_config() |
| 75 | + local response = storage_client:delete('/myapp/config/all') |
| 76 | + |
| 77 | + return response |
| 78 | +end |
| 79 | + |
| 80 | +function delete_all_configs() |
| 81 | + local response = storage_client:delete('/') |
| 82 | + |
| 83 | + return response |
| 84 | +end |
0 commit comments