Skip to content

Instantly share code, notes, and snippets.

@aferreira
Last active May 22, 2017 19:15
Show Gist options
  • Select an option

  • Save aferreira/7767f71ce49425465bd518aafa2f0051 to your computer and use it in GitHub Desktop.

Select an option

Save aferreira/7767f71ce49425465bd518aafa2f0051 to your computer and use it in GitHub Desktop.
My take on "How to write checkip.amazonaws.com service with Mojo?"
# Take a look at https://github.com/aferreira/acme-docker-checkip
package CheckIP;
use Mojo::Base 'Mojo';
use Mojolicious::Commands;
use Scalar::Util;
has commands => sub {
my $commands = Mojolicious::Commands->new( app => shift );
Scalar::Util::weaken $commands->{app};
return $commands;
};
sub handler {
my ( $self, $tx ) = @_;
my $ip = $tx->remote_address;
# Response
$tx->res->code(200);
$tx->res->headers->content_type('text/plain');
$tx->res->body("$ip\n");
# Resume transaction
$tx->resume;
}
sub start {
my $self = shift;
return $self->commands->run( @_ ? @_ : @ARGV );
}
1;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment