-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathstorage.pl
147 lines (131 loc) · 3.87 KB
/
storage.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#!/usr/bin/perl
use strict 'vars';
use warnings;
use Storable qw(retrieve nstore);
use Coro::Twiggy;
use Plack::Request;
use Data::Dumper::Perltidy;
use JSON;
use AnyEvent;
use Graph;
use Graph::D3;
if (!-e 'pages.db') {
nstore { 'index' => 'this is the index' }, 'pages.db';
}
my %stor = %{retrieve 'pages.db' or die};
my $application = sub {
my ($env) = @_;
my $req = Plack::Request->new($env);
warn $req->path;
if ($req->method eq 'OPTIONS') {
return [
200,
[
'Access-Control-Allow-Origin' => '*',
'Access-Control-Allow-Headers' => '*',
'Content-Type' => 'text/plain'
],
[ 'hmm' ]
]
}
if ($req->path eq '/commit') {
nstore \%stor, 'pages.db';
return [
200,
[ 'Access-Control-Allow-Origin' => '*', 'Content-Type' => 'text/plain' ],
[ time ]
]
}
elsif ($req->path eq '/load') {
my $page = $req->param('page');
unless (exists $stor{$page}) { $stor{$page} = '' }
return [
200,
[ 'Access-Control-Allow-Origin' => '*', 'Content-Type' => 'text/plain' ],
[ $stor{$page} ]
]
}
elsif ($req->path eq '/save') {
my %new = %{ $req->body_parameters };
for my $key (keys %new) {
# trying to catch a bug
die if $key eq '';
$stor{$key} = $new{$key};
}
nstore \%stor, 'pages.db' or die;
return [
200,
[ 'Access-Control-Allow-Origin' => '*', 'Content-Type' => 'text/plain' ],
[ 'ok' ]
]
}
elsif ($req->path eq '/graph') {
my @files = keys %stor;
my @links;
for my $file (keys %stor) {
my @items = split /( |(?=\n))/ => $stor{$file};
for (0 ..$#items) {
next if $items[$_] eq "\n";
next if $items[$_] eq " ";
next unless $items[$_] =~ /\w/i;
my($word) = $items[$_] =~ /(\w+)/i;
# it doesn't matter if this is lc since it's only for the edge
if (defined $stor{$word})
{
push @links => [$file, $word];
}
elsif (defined $stor{lc $word}) {
push @links => [$file, lc $word];
}
}
}
my $g = new Graph(
vertices => \@files,
edges => \@links
);
my $d3 = new Graph::D3(graph => $g, type => 'json');
return [
200,
[ 'Access-Control-Allow-Origin' => '*', 'Content-Type' => 'application/json' ],
[ $d3->force_directed_graph() ]
]
}
elsif ($req->path eq '/delete') {
my $page = $req->param('page');
if (defined $stor{$page}) {
delete $stor{$page};
nstore \%stor, 'pages.db' or die;
return [
200,
[ 'Access-Control-Allow-Origin' => '*', 'Content-Type' => 'text/plain' ],
[ 'deleted' ]
]
}
else {
return [
200,
[ 'Access-Control-Allow-Origin' => '*', 'Content-Type' => 'text/plain' ],
[ 'page not found' ]
]
}
}
elsif ($req->path eq '/list') {
return [
200,
[ 'Access-Control-Allow-Origin' => '*', 'Content-Type' => 'application/json' ],
[ encode_json([keys %stor]) ]
]
}
return [
200,
[
'Access-Control-Allow-Origin' => '*',
'Access-Control-Allow-Headers' => '*',
'Content-Type' => 'text/plain'
],
[ 'hmm' ]
]
};
my $server = Coro::Twiggy->new(host => '127.0.0.1', port => 8888);
$server->register_service( $application );
AE::cv->recv;