| OpenResty documentation | Contained in the OpenResty distribution. |
OpenResty::Tutorial::GettingStarted::Perl - Zero to OpenResty for Perl programmers
This tutorial should give you everything you need to start with an OpenResty account using Perl.
You should already have an account on an OpenResty server. You can
either set up an OpenResty server on your own machine or just request an
account on our Yahoo! China's production server by sending an email to
agentzh@yahoo.cn. If you're running your own instance of the OpenResty
server, you can use the following command to create an account (named
foo) for yourself:
$ bin/openresty adduser foo
You'll be prompted to enter a password for the Admin role of your
foo account.
Throughout this tutorial, we'll assume you own an account named foo
whose Admin role's password is hello1234. And the account belongs
to the server api.openresty.org.
Because OpenResty's API is totally RESTful, that is, it's totally HTTP based. So it's completely okay to directly use a general HTTP client libary like LWP::UserAgent. But to make things even easier, we'll stick with a CPAN module, WWW::OpenResty::Simple, throughout the tutorial. In case you don't know, installing the WWW::OpenResty::Simple module is as simple as
$ sudo cpan WWW::OpenResty::Simple
Commands will differ slightly if you're on Win32:
C:\>cpan WWW::OpenResty::Simple
Note that if you use an account on others' OpenResty servers (like ours), you need not install the hairy OpenResty module on CPAN.
There's various different ways to login to your OpenResty account. But in a Perl script, we usually use the default Admin role with full priviledges:
use strict;
use warnings;
use utf8;
use WWW::OpenResty::Simple;
my $resty = WWW::OpenResty::Simple->new(
{ server => 'api.openresty.org' }
);
$resty->login('foo', 'hello1234');
The first statement loads the WWW::OpenResty::Simple module which
we'll be using exclusively to manipulate our account.
And in the second one, we created an instance of the
WWW::OpenResty::Simple class with the domain of the OpenResty
server we're using. It might be a different value on your side
(i.e. localhost) if you're running your own instance of server.
It's not good practice to hard code your password explicitly in your scripts. I wrote the sample code this way merely for the demonstration purpose.
Usually we use OpenResty as a RESTy database. As with traditional
relational database systems like mysql and PostgreSQL, we start by
creating a "data schema". In OpenResty, Models often resembles database
tables (but they could be something else as well). You can define a new
Model like this:
$resty->post(
'/=/model/Post',
{
description => 'blog posts',
columns => [
{ name => 'author', default => 'Anonymous' },
{ name => 'title', default => 'No title' },
{ name => 'content' },
{ name => 'created',
type => 'timestamp (0) with time zone',
default => ['now()'] },
{ name => 'comments', type => 'integer', default => 0 }
]
}
);
WWW::OpenResty::Simple objects' post method will issue an HTTP POST command behind the scene.
Agent Zhang (agentzh) agentzh@yahoo.cn
Copyright (c) 2007 by Yahoo! China EEEE Works, Alibaba Inc.
| OpenResty documentation | Contained in the OpenResty distribution. |