-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.pl
66 lines (52 loc) · 1.48 KB
/
app.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#!/usr/bin/env perl
use Mojolicious::Lite;
use Method::Signatures::Simple;
use MojoX::Redis;
my $redis = MojoX::Redis->new(server => '127.0.0.1:6379');
my $ttl = 300; # server-side ttl is 5 minutes. client is expected
# to ping again at about 60% of ttl.
any '/ping' => method () {
my $ip = $self->tx->remote_address;
my $port = $self->param('port') // 0;
my $new = $self->param('new') // 0;
my $yaml = $self->param('yaml') // '';
$redis->incr('games') if $new;
my $key = "$ip:$port";
$redis->set($key => $yaml);
$redis->expire($key => $ttl);
$redis->sadd(active => $key);
$self->render(text => 'It works!');
};
get '/games' => method () {
$redis->get(games => func ($redis, $res) {
$self->render(text => $res->[0]);
});
};
my $gamelist_lua = q/
local active_games = redis.call('smembers','active')
local results = {}
table.foreach(active_games, function(k,v)
local z = redis.call('get',v)
if z then
table.insert(results, v)
table.insert(results, redis.call('ttl',v))
table.insert(results, z)
end
end)
return results/;
get '/list' => method () {
$redis->execute(eval => [$gamelist_lua, 0] => func($redis, $res) {
my $text = '';
my $id = 0;
while (scalar @$res) {
my $address = (shift @$res)->[0];
my $ttl = (shift @$res)->[0];
my $yaml = (shift @$res)->[0];
$text .= "Game\@$id:\n\tAddress:$address\n\tTTL:$ttl\n\t";
$text .= join("\n\t", split('\n',$yaml));
++$id;
}
$self->render(text => $text);
});
};
app->start;