2012 Feed

Introduction to OX

OX - 2012-12-01

Overview

OX is a new web framework, designed to allow you to easily glue together existing components (or write new, highly decoupled components) for your application, without requiring additional plugin systems, glue layers, or global state. It is available on CPAN, and on GitHub. You can install it via cpanm OX. If you have any questions or suggestions, we hang out on the #ox channel on irc.perl.org, so feel free to drop by!

OX is based on three main components:

Plack

Plack is an implementation of the PSGI protocol. By implementing PSGI, OX can run on any server platform that PSGI supports (which is most of them). Plack also provides extra functionality such as middleware, which OX exposes to application authors (as will be described in future articles).

Bread::Board

Bread::Board is a dependency injection framework, used to structure your application initialization. It allows your application objects to access each other without the need for globals or repetitive argument passing. With Bread::Board, you can write your application classes however makes sense to you, and then tie them all together.

Path::Router

Path::Router provides simple, reversible routing for URL paths. In addition to basic path matching, it supports variable path components (with optional validation), the ability to look up paths based on the descriptions they were defined with, and useful debugging and testing tools. It's used to determine your application's URL structure.

Hello World


1: 
2: 
3: 
4: 
5: 

 

use OX;

router as {
    route '/' => sub { "Hello world!" };
};

 

This defines a simple "Hello world" application. This snippet is a valid .psgi file (the format used by Plack), since the router keyword returns a PSGI application coderef if necessary.

If you download this example here, you can run it with plackup hello.psgi, and view the results in your browser.

The router block is the most important part of an OX application. It defines the entire URL structure in one place, making tracing the code that is run for a given request as easy as possible to find. Simple routes can define the code to be run directly, but for anything non-trivial, you'll want to organize the code into models, controllers, etc., as shown in this next example.

Another example


1: 
2: 
3: 
4: 
5: 
6: 
7: 
8: 
9: 
10: 
11: 
12: 
13: 
14: 
15: 
16: 
17: 
18: 
19: 
20: 
21: 
22: 
23: 
24: 
25: 
26: 
27: 
28: 
29: 
30: 
31: 
32: 
33: 
34: 
35: 
36: 
37: 
38: 
39: 
40: 
41: 
42: 
43: 
44: 
45: 
46: 
47: 
48: 
49: 
50: 
51: 
52: 
53: 
54: 
55: 
56: 
57: 
58: 
59: 
60: 
61: 
62: 
63: 
64: 
65: 

 

package Counter::Model;
use Moose;

has counter => (
    traits => ['Counter'],
    is => 'ro',
    isa => 'Int',
    default => 0,
    handles => {
        inc => 'inc',
        dec => 'dec',
        reset => 'reset',
    },
);

package Counter::Controller;
use Moose;

has model => (
    is => 'ro',
    isa => 'Counter::Model',
    required => 1,
);

sub index {
    my $self = shift;
    return $self->model->counter;
}

sub inc {
    my $self = shift;
    return $self->model->inc;
}

sub dec {
    my $self = shift;
    return $self->model->dec;
}

sub reset {
    my $self = shift;
    return $self->model->reset;
}

package Counter;
use OX;

has model => (
    is => 'ro',
    isa => 'Counter::Model',
    lifecycle => 'Singleton',
);

has controller => (
    is => 'ro',
    isa => 'Counter::Controller',
    infer => 1,
);

router as {
    route '/' => 'controller.index';
    route '/inc' => 'controller.inc';
    route '/dec' => 'controller.dec';
    route '/reset' => 'controller.reset';
};

 

This is a slightly more involved example. Here, paths are routed to methods on controller objects, and OX automatically figures out (via Bread::Board) how to create all of the objects necessary to make the request work.

This example can be downloaded here, and run via plackup counter.psgi. Access the /inc, /dec, and /reset paths to manipulate the counter.

One interesting thing to note is how the controller and model are both just normal Moose classes - there is nothing special about them at all. Your application can be structured in any way that you want, and OX will do the work of introspecting and tying together all of the pieces. We will explain how exactly this works over the coming weeks.

Gravatar Image This article contributed by: Jesse Luehrs <jesse.luehrs@iinteractive.com>