Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- use strict;
- use warnings;
- use HTTP::Daemon;
- use HTTP::Status;
- use JSON;
- my $httpd = HTTP::Daemon->new(
- LocalAddr => 'localhost',
- LocalPort => 3000,
- ) or die "não foi possível iniciar o servidor: $!\n";
- my @coins;
- print "servidor rodando em http://localhost:3000\n";
- while (my $client = $httpd->accept) {
- while (my $request = $client->get_request) {
- if ($request->method eq 'POST' && $request->url->path eq '/add') {
- my $content = $request->content;
- my $coin = decode_json($content);
- push @coins, $coin;
- $client->send_response(
- HTTP::Response->new(
- HTTP_OK,
- undef,
- [ 'Content-Type' => 'application/json' ],
- encode_json({ message => 'moeda adicionada com sucesso' })
- )
- );
- } elsif ($request->method eq 'GET' && $request->url->path eq '/coins') {
- $client->send_response(
- HTTP::Response->new(
- HTTP_OK,
- undef,
- [ 'Content-Type' => 'application/json' ],
- encode_json(\@coins)
- )
- );
- } else {
- $client->send_response(
- HTTP::Response->new(
- HTTP_NOT_FOUND,
- undef,
- [ 'Content-Type' => 'application/json' ],
- encode_json({ message => 'endpoint não encontrado' })
- )
- );
- }
- }
- $client->close;
- undef $client;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement