Rnet
Rnet

Reputation: 5040

Conditional proxy server in python/perl/shell script

I've an application which queries a hostName:port for some service, a webservice. During development some of the services are not available or under construction by a third party, hence to develop our application I'm thinking of using mockservices with soapUI as a stub for some services.

Problem is I can point my application only in one direction once, so if I point to the mockservice, I need to make sure the mockservice is emulating all the services (>50), this is too much overhead. Hence I was thinking of pointing my application to a proxy server which would redirect some requests to the mockservices and remaining to the actual service provider. Is there any way I could do this through a simple script?

Upvotes: 0

Views: 494

Answers (1)

hobbs
hobbs

Reputation: 239930

Sure. A rough sketch using Perl:

#!perl
use strict;
use warnings;
use Plack::App::Proxy;

my $live_proxy = Plack::App::Proxy->new(
    remote => "http://live.soap.service.com:80/"
);

my $soapui_proxy = Plack::App::Proxy->new(
    remote => "http://localhost:4567/"
);

sub {
    my $env = shift;
    if ($env->{REQUEST_URI} =~ m[^/some/path]) {
        return $soapui_proxy->($env);
    } else {
        return $live_proxy->($env);
    }
};

install Plack and run it as plackup filename. It constructs two proxy apps, one which forwards all requests to the live service URL, and one which forwards all requests to your mock service URL. Then the wrapper app chooses which app to dispatch any given request to based on the URL (or based on something else, if need be).

Upvotes: 2

Related Questions