Skip to content

Commit 08047de

Browse files
committed
Applications: custom role samples
1 parent 469ec7b commit 08047de

File tree

11 files changed

+166
-0
lines changed

11 files changed

+166
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
groups:
2+
group001:
3+
replicasets:
4+
replicaset001:
5+
instances:
6+
instance001:
7+
roles: [ greeter ]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
-- greeter.lua --
2+
return {
3+
validate = function() end,
4+
apply = function() require('log').info("Hi from the 'greeter' role!") end,
5+
stop = function() end,
6+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
instance001:
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
-- byeer.lua --
2+
local log = require('log').new("byeer")
3+
4+
return {
5+
dependencies = { 'greeter' },
6+
validate = function() end,
7+
apply = function() log.info("Bye from the 'byeer' role!") end,
8+
stop = function() end,
9+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
groups:
2+
group001:
3+
replicasets:
4+
replicaset001:
5+
instances:
6+
instance001:
7+
roles: [ greeter ]
8+
roles_cfg:
9+
greeter:
10+
greeting: 'Hi'
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
-- greeter.lua --
2+
local log = require('log').new("greeter")
3+
4+
local function validate_config(cfg)
5+
if cfg.greeting then
6+
assert(type(cfg.greeting) == "string", "'greeting' should be a string")
7+
assert(cfg.greeting == "Hi" or cfg.greeting == "Hello", "'greeting' should be 'Hi' or 'Hello'")
8+
end
9+
end
10+
11+
local function apply_config(cfg)
12+
log.info("%s from the 'greeter' role!", cfg.greeting)
13+
end
14+
15+
local function stop_role()
16+
log.info("The 'greeter' role is stopped")
17+
end
18+
19+
return {
20+
validate = validate_config,
21+
apply = apply_config,
22+
stop = stop_role,
23+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
instance001:
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
groups:
2+
group001:
3+
replicasets:
4+
replicaset001:
5+
instances:
6+
instance001:
7+
roles: [ http-api ]
8+
roles_cfg:
9+
http-api:
10+
host: '127.0.0.1'
11+
port: 8080
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
local data = {}
2+
3+
data.add_sample_data = function()
4+
box.watch('box.status', function()
5+
if box.info.ro then
6+
return
7+
end
8+
9+
box.schema.space.create('bands', { if_not_exists = true })
10+
box.space.bands:format({
11+
{ name = 'id', type = 'unsigned' },
12+
{ name = 'band_name', type = 'string' },
13+
{ name = 'year', type = 'unsigned' }
14+
})
15+
box.space.bands:create_index('primary', { parts = { 'id' } })
16+
box.space.bands:create_index('band', { parts = { 'band_name' } })
17+
box.space.bands:create_index('year_band', { parts = { { 'year' }, { 'band_name' } } })
18+
19+
box.space.bands:insert { 1, 'Roxette', 1986 }
20+
box.space.bands:insert { 2, 'Scorpions', 1965 }
21+
box.space.bands:insert { 3, 'Ace of Base', 1987 }
22+
box.space.bands:insert { 4, 'The Beatles', 1960 }
23+
box.space.bands:insert { 5, 'Pink Floyd', 1965 }
24+
box.space.bands:insert { 6, 'The Rolling Stones', 1962 }
25+
box.space.bands:insert { 7, 'The Doors', 1965 }
26+
box.space.bands:insert { 8, 'Nirvana', 1987 }
27+
box.space.bands:insert { 9, 'Led Zeppelin', 1968 }
28+
box.space.bands:insert { 10, 'Queen', 1970 }
29+
end)
30+
end
31+
32+
return data
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
-- http-api.lua --
2+
local httpd
3+
local json = require('json')
4+
5+
local function validate_config(cfg)
6+
if cfg.host then
7+
assert(type(cfg.host) == "string", "'host' should be a string containing a valid IP address")
8+
end
9+
if cfg.port then
10+
assert(type(cfg.port) == "number", "'port' should be a number")
11+
assert(cfg.port >= 1 and cfg.port <= 65536, "'port' should be between 1 and 65536")
12+
end
13+
end
14+
15+
local function apply_config(cfg)
16+
if httpd then
17+
httpd:stop()
18+
end
19+
httpd = require('http.server').new(cfg.host, cfg.port)
20+
local response_headers = { ['content-type'] = 'application/json' }
21+
httpd:route({ path = '/band/:id', method = 'GET' }, function(req)
22+
local id = req:stash('id')
23+
local band_tuple = box.space.bands:get(tonumber(id))
24+
if not band_tuple then
25+
return { status = 404, body = 'Band not found' }
26+
else
27+
local band = { id = band_tuple['id'],
28+
band_name = band_tuple['band_name'],
29+
year = band_tuple['year'] }
30+
return { status = 200, headers = response_headers, body = json.encode(band) }
31+
end
32+
end)
33+
httpd:route({ path = '/band', method = 'GET' }, function(req)
34+
local limit = req:query_param('limit')
35+
if not limit then
36+
limit = 5
37+
end
38+
local band_tuples = box.space.bands:select({}, { limit = tonumber(limit) })
39+
local bands = {}
40+
for _, tuple in pairs(band_tuples) do
41+
local band = { id = tuple['id'],
42+
band_name = tuple['band_name'],
43+
year = tuple['year'] }
44+
table.insert(bands, band)
45+
end
46+
return { status = 200, headers = response_headers, body = json.encode(bands) }
47+
end)
48+
httpd:start()
49+
end
50+
51+
local function stop_role()
52+
httpd:stop()
53+
end
54+
55+
local function init()
56+
require('data'):add_sample_data()
57+
end
58+
59+
init()
60+
61+
return {
62+
validate = validate_config,
63+
apply = apply_config,
64+
stop = stop_role,
65+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
instance001:

0 commit comments

Comments
 (0)