http-server

Creating a simple HTTP application

Cro HTTP applications are hosted using its built-in HTTP server. To start out, we import it:

use Cro::HTTP::Server;

The server hosts an application, which is something that transforms a Cro::HTTP::Request instance representing the request into a Cro::HTTP::Response instance representing the response. While it's possible to write that "by hand," it is far more convenient to use the HTTP router.

use Cro::HTTP::Router;

The Cro::HTTP::Router module provides a convenient API for mapping incoming requests to appropriate handlers. It also provides routines to make it quick and easy to produce the most common kinds of responses. Here is a simple example:

my $application = route {
    get -> {
        content 'text/html', 'Hello World!';
    }
}

Routines like get, post, and delete indicate the HTTP method, while the signature part represents the target of the request. The absence of parameters counts as /. Let's see some more examples:

my $application = route {
    get -> {
        content 'text/html', 'Home';
    }
    post -> 'poster' {
        content 'text/html', 'Post request to /poster page';
    }
    get -> 'articles', $author, $name {
        content 'text/html', "<h1>{$name}<h1><em>By {$author}</em>";
    }
}

An application created with route can be easily used with Cro::HTTP::Server:

# Create the HTTP service object
my Cro::Service $service = Cro::HTTP::Server.new(
    :host('localhost'), :port(2314), :$application
);

# Run it
$service.start;

# Cleanly shut down on Ctrl-C
react whenever signal(SIGINT) {
    $service.stop;
    exit;
}

With this the application is up and running, so it's time to improve it: add more logic and use other features. See Cro::HTTP::Router and Cro::HTTP::Server to learn more.

cro v0.8.8

Libraries and tools for building reactive services in Raku. This installs the cro command line and web tool, along with HTTP (including HTTPS and HTTP/2.0) and WebSocket support.

Authors

  • Jonathan Worthington

License

Artistic-2.0

Dependencies

IO::Socket::Async::SSLJSON::FastMETA6Shell::CommandFile::FindTerminal::ANSIColorOO::MonitorsYAMLishCro::WebSocket:ver<0.8.8>Docker::FileFile::Ignore

Test Dependencies

Provides

  • Cro::Tools::CLI
  • Cro::Tools::CroFile
  • Cro::Tools::Link::Editor
  • Cro::Tools::LinkTemplate
  • Cro::Tools::LinkTemplate::HTTP
  • Cro::Tools::LinkTemplate::HTTPS
  • Cro::Tools::LinkTemplate::WS
  • Cro::Tools::LinkTemplate::WSS
  • Cro::Tools::Runner
  • Cro::Tools::Serve
  • Cro::Tools::Services
  • Cro::Tools::Template
  • Cro::Tools::Template::Common
  • Cro::Tools::Template::HTTPService
  • Cro::Tools::Template::ReactReduxSPA
  • Cro::Tools::Template::ZeroMQWorkSinkService
  • Cro::Tools::Template::ZeroMQWorkerService
  • Cro::Tools::TemplateLocator
  • Cro::Tools::Web

The Camelia image is copyright 2009 by Larry Wall. "Raku" is trademark of the Yet Another Society. All rights reserved.