#
# $Id: Rest.pm,v 5ff3fac424d5 2015/11/09 18:06:36 gomor $
#
# server::rest Brik
#
package Metabrik::Server::Rest;
use strict;
use warnings;

use base qw(Metabrik::Server::Http);

sub brik_properties {
   return {
      revision => '$Revision: 5ff3fac424d5 $',
      tags => [ qw(unstable server rest api) ],
      attributes => {
         datadir => [ qw(datadir) ],
         hostname => [ qw(listen_hostname) ],
         port => [ qw(listen_port) ],
         output_mode => [ qw(json|xml) ],
      },
      attributes_default => {
         hostname => 'localhost',
         port => 8888,
         output_mode => 'json',
      },
      commands => {
         start => [ qw(get_handlers post_handlers|OPTIONAL) ],
      },
      require_modules => {
         'Metabrik::String::Json' => [ ],
         'Metabrik::String::Xml' => [ ],
      },
   };
}

sub start {
   my $self = shift;
   my ($get_handlers, $post_handlers) = @_;

   my $hostname = $self->hostname;
   my $port = $self->port;
   my $root = $self->datadir;
   $post_handlers ||= [];
   my $output_mode = $self->output_mode;
   if (! defined($hostname) || ! defined($port) || ! defined($root) || ! defined($get_handlers)) {
      return $self->log->error($self->brik_help_run('start'));
   }

   my $http = HTTP::Server::Brick->new(
      port => $port,
      host => $hostname,
      timeout => $self->global->rtimeout,
   );

   $http->mount('/' => { path => $root });

   my $se;
   if ($self->output_mode eq 'json') {
      $se = Metabrik::String::Json->new_from_brik_init($self) or return;
   }
   elsif ($self->output_mode eq 'xml') {
      $se = Metabrik::String::Xml->new_from_brik_init($self) or return;
   }
   else {
      return $self->log->error("start: output_mode not supported [$output_mode]");
   }

   for my $get (@$get_handlers) {
      $http->mount($get->{url} => {
         handler => sub {
            my ($req, $res) = @_;
            my $hash = &{$get->{sub}}($req, $res) || {
               error => 1,
               error_string => 'unknown'
            };
            $res->add_content($se->encode($hash));
            return 1;
         },
         wildcard => 1,
      });
   }

   for my $post (@$post_handlers) {
   }

   return $self->_http($http)->start;
}

1;

__END__

=head1 NAME

Metabrik::Server::Rest - server::rest Brik

=head1 COPYRIGHT AND LICENSE

Copyright (c) 2014-2015, Patrice E<lt>GomoRE<gt> Auffret

You may distribute this module under the terms of The BSD 3-Clause License.
See LICENSE file in the source distribution archive.

=head1 AUTHOR

Patrice E<lt>GomoRE<gt> Auffret

=cut
