Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env perl
- package HelloWorld;
- use Web::Simple;
- use Params::Validate qw(:all);
- use JSON;
- sub status_ok {
- my %p = Params::Validate::validate( @_, { entity => 1, }, );
- return [ 200, [ 'Content-type', 'application/json' ], [ encode_json($p{entity}) ] ];
- }
- sub status_bad_request {
- my %p = Params::Validate::validate( @_, { message => { type => SCALAR }, }, );
- return [ 400, [ 'Content-type', 'application/json' ], [ encode_json({error => $p{message}}) ] ];
- }
- sub dispatch_request {
- sub (GET) {
- my $resposta = { ola => 'rest', linhas => [ { a => 1}, { a => 2} ] };
- return status_ok( entity => $resposta);
- },
- sub (/user/*/...) {
- my ($self, $user_id) = @_;
- # essa sub retorna mais 2 subs
- sub (PUT + /endereco/*) {
- my ($self, $endereco) = @_;
- return status_ok( entity => { msg => "endereco do $user_id atualizado para $endereco" } );
- },
- sub (DELETE + /endereco/*) {
- my ($self, $endereco) = @_;
- return status_ok( entity => { msg => "endereco $endereco do $user_id foi removido" } );
- }
- },
- sub () {
- return status_bad_request( message => 'metodo nao suportado' );
- }
- }
- HelloWorld->run_if_script;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement