GithubHelp home page GithubHelp logo

scgi's Introduction

SCGI for Perl 6

Introduction

This is a simple SCGI library for Perl 6.

It's main influences are the Perl 5 SCGI library, and the Perl 6 HTTP::Daemon library.

It offers a bit of candy coating compared to the Perl 5 version.

By default is uses a PSGI-compliant interface, but can also handle raw HTTP responses.

You don't need to create your own IO::Socket::INET object. Just pass an 'addr' and 'port' attribute to the new() declaration and it'll create the object for you.

Usage

The simplest (and recommended) form of usage is to use the handle() method with PSGI-compliant output. Here's an example:

  use SCGI;

  my $scgi = SCGI.new( :port(8118) );

  my $handler = sub (%env) 
  {
    my $name = %env<QUERY_STRING> || 'world';
    my $status = '200';
    my @headers = 'Content-Type' => 'text/plain';
    my @body = "Hello $name\n";
    @headers.push: 'Content-Length' => @body.join.encode.bytes;
    return [ $status, \@headers, \@body ];
  }

  $scgi.handle: $handler;

There are other ways of using SCGI, such as writing your own run loop, or using a raw HTTP output instead of PSGI. Here's an example doing both:

  use SCGI;

  my $scgi = SCGI.new( :port(8118), :!PSGI );
  while (my $connection = $scgi.accept())
  {
    my $request = $connection.request;
    if $request.success
    {
      my $name = $request.env<QUERY_STRING> || 'world';
      my $return = "Hello $name\n";
      my $len = $return.encode.bytes;
      my $headers = "Content-Type: text/plain\nContent-Length: $len\n";
      $connection.send("$headers\n$return");
    }
    $connection.close;
  }

Test script representing both examples can be found in the 'test' folder.

If you are serious about using SCGI for web application development, see the Perl 6 Web library set, or one of the full blown frameworks built using it.

Configuration

nginx

Make sure you compiled nginx with the SCGI plugin (it is included by default.) Then, in one of your server blocks, add a location mount:

  location /scgi/ {
    scgi_pass 127.0.0.1:8118;
    include scgi_params;
    # Optionally rewrite document URI path
    rewrite ^/scgi/(.*) /$1 break;
    # Some applications may need rewritten URI in PATH_INFO
    scgi_param PATH_INFO $uri;
  }

lighttpd

First, make sure the SCGI library is being loaded.

  server.modules += ( "mod_scgi" )

Next, set up an SCGI handler:

  scgi.server = (
    "/scgi" =>
    ((
      "host" => "127.0.0.1",
      "port" => 8118,
      "check-local" => "disable"
    ))
  )

Apache 2 with mod_scgi:

Add the following line to your site config, changing the details to match your SCGI script configuration:

  SCGIMount /scgi/ 127.0.0.1:8118

Apache 2 with mod_proxy_scgi:

Add the following line to your site config, changes the details to match your SCGI script configuration:

  <Proxy *>
    Order deny,allow
    Allow from all
  </Proxy>
  ProxyPass /scgi/ scgi://localhost:8118/

Requirements

Author

Timothy Totten, supernovus on #perl6, https://github.com/supernovus/

License

Artistic License 2.0

scgi's People

Contributors

supernovus avatar retupmoca avatar softmoth avatar fjwhittle avatar paultcochrane avatar

Watchers

 avatar James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.