package CountServer;
###########################################
# CountServer
# Mike Schilli, 2014 (m@perlmeister.com)
###########################################
use strict;
use URI::URL ();
use AnyEvent::HTTPD;

my $PORT     = 9090;
my $BASE_URL = "http://localhost:$PORT";

###########################################
sub new {
###########################################
    bless {}, $_[0];
}

###########################################
sub start {
###########################################
  my( $self ) = @_;

  $self->{ httpd } = 
    AnyEvent::HTTPD->new( port => $PORT );

  $self->{ httpd }->reg_cb(
    "" => sub {
      my ($hdr, $req) = @_;

      my $path = $req->url()->as_string;
      ( my $newpath = $path ) =~ 
          s/(\d+)/$1 + 1/ge;

      my $url = URI::URL->new( $BASE_URL );
      $url->path( $newpath );

      $req->respond ({ 
        content => ['text/txt',
        $url->as_string] } );
      }
  );
}

###########################################
sub url {
###########################################
  my( $self ) = @_;

  return $BASE_URL;
}

1;
