app.psgi 840 B

123456789101112131415161718192021222324
  1. use v5.16;
  2. use Plack::Builder;
  3. use Plack::Request;
  4. use JSON::XS 'encode_json';
  5. use DBI;
  6. my $dbh = DBI->connect_cached(
  7. 'dbi:mysql:database=hello_world;host=localhost;port=3306',
  8. qw(benchmarkdbuser benchmarkdbpass)
  9. ) || die $!;
  10. my $sth = $dbh->prepare_cached('SELECT randomNumber FROM World WHERE id = ?');
  11. my $header = [qw(Content-Type application/json)];
  12. builder {
  13. mount '/json' => sub { [ 200, $header, [ encode_json({ message => 'Hello, World!' })] ] },
  14. mount '/db' => sub {
  15. my @rs = map { id => $_->[0] + 0, randomNumber => $_->[1] },
  16. grep exists $_->[1],
  17. map [$_, $sth->execute($_) && $sth->fetchrow_array],
  18. map int rand 10000 + 1,
  19. 1..Plack::Request->new(shift)->param('queries')//1;
  20. [ 200, $header, [ encode_json( @rs > 1 ? \@rs : $rs[0] ) ] ] } };