2012 Feed

Mounts

OX - 2012-12-06

The mount keyword

The mount keyword allows you to include other PSGI applications inside your application, under a given request path. For instance, if you have static files that need to be served, you can create a Plack::App::File instance and mount it under /static, so that any request for a path underneath /static will be delegated to the Plack::App::File app.

You can specify an app in one of four ways:

  • A PSGI coderef


    1: 
     

    mount '/static' => Plack::App::File->new(root => "static")->to_app;
     

    In this case, the coderef is delegated to directly.

  • An object


    1: 
     

    mount '/static' => Plack::App::File->new(root => "static");
     

    The object will have to_app called on it, and the result of that will be used for the mount.

  • The name of a class


    1: 
    2: 
    3: 
    4: 

     

    mount '/static' => "Plack::App::File", (
        root => 'static_root',
        encoding => literal('latin1'),
    );

     

    This will first instantiate a new instance of the class, and then use that instance as described in the previous bullet point. The instance will be instantiated via Bread::Board, so it can depend on services that your application has declared. The dependencies should be listed as a hash after the name of the class.

  • Another router


    1: 
    2: 
    3: 
    4: 

     

    mount '/auth' => router as {
        route '/login' => 'auth.login';
        # ...
    };

     

    This will allow you to create separate sections of your application under certain request paths. These will be able to define all of their own middleware, routes, and mounts, but will share the attributes defined in the main app. In this example, a request for /auth/login will be handled by the auth.login action.

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