-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmoth-api.pl
More file actions
80 lines (63 loc) · 1.49 KB
/
moth-api.pl
File metadata and controls
80 lines (63 loc) · 1.49 KB
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#!/usr/bin/env perl
use strict; use warnings;
use Dancer2;
use Data::Dumper;
use Moth;
# We output JSON for this
set serializer => 'JSON';
# TODO Make these configurable
my $crate_host = 'localhost';
my $crate_port = 4200;
my $moth = Moth->new($crate_host, $crate_port);
# Needs a newer version of Dancer2, to support 'send_as HTML => $content'
get '/' => sub {
my $response = <<HTML;
<html>
<head>
<title>Moth API</title>
</head>
<body>
<h1>Moth API</h1>
<p>This is the Moth API. Use it to store IoT sensor data in streams</p>
</body>
</html>
HTML
return $response;
};
get '/streams' => sub {
$moth->get_streams();
};
get '/streams/:stream' => sub {
$moth->get_stream( params->{stream} );
};
get '/streams/:stream/sensors' => sub {
$moth->get_stream_sensors( params->{stream} );
};
get '/streams/:stream/samples' => sub {
$moth->get_stream_samples( params->{stream} );
};
get '/sensors' => sub {
$moth->get_sensors();
};
get '/sensors/:sensor_id' => sub {
$moth->get_sensor( params->{sensor_id} );
};
get '/sensors/:sensor_id/samples' => sub {
$moth->get_sensor_samples( params->{sensor_id} );
};
post '/streams' => sub {
$moth->add_stream( params('body') );
};
post '/streams/:stream/sensors' => sub {
$moth->add_stream_sensor(
params->{stream},
params('body'),
);
};
post '/streams/:stream/samples' => sub {
$moth->add_sample(
params->{stream},
params('body'),
);
};
dance();