SweetPea - Pre-Release Overview

50
SweetPea / SweetPea::Application Pre-release Overview by Al Newkirk (awncorp)

description

A Perl web application framework that is as flexible as your imagination... For my information visit #sweetpea on irc.perl.org

Transcript of SweetPea - Pre-Release Overview

Page 1: SweetPea -  Pre-Release Overview

SweetPea / SweetPea::Application

Pre-release Overview

by Al Newkirk (awncorp)

Page 2: SweetPea -  Pre-Release Overview

Yet Another Web Application Framework

Page 3: SweetPea -  Pre-Release Overview

WHY?

Frameworks are like religions,

I could be Christian, you could be

Muslim, or we both could be Christian,

you Catholic and me Baptist.

Page 4: SweetPea -  Pre-Release Overview

Meaning What?

One size does not fit all.

Page 5: SweetPea -  Pre-Release Overview

And Your Point Is?

TIMTOWTDI

(There is more than one way to do it)

When given a choice, people are more

likely to adopt frameworks (like

religions) that converge with their

existing concepts.

Page 6: SweetPea -  Pre-Release Overview

The SweetPea Concept

A Perl web application framework that

doesn't get in the way or suck.

Page 7: SweetPea -  Pre-Release Overview

Get In The Way?

If the task is to create a web application

using a framework to ease the tedium,

but the framework over complicates the

process, then that framework is in the

way, and that sucks.

Page 8: SweetPea -  Pre-Release Overview

Define Over Complicate

Web application frameworks that

produce all or some of these issues are

over complicating the initial goal (which

is to create a web application):

Page 9: SweetPea -  Pre-Release Overview

complex, overly simple, loosely coupled,

or incomplete API, inadequate

documentation, steep learning curve,

unscalable, missing core functionality,

core functionality as plugins, complex

syntax, inconsistent design concepts,

and/or massive prerequisites irrelevant

to the task.

Page 10: SweetPea -  Pre-Release Overview

The Solution

Develop something truly scalable.

Provide the obvious and step aside.

Care about the developer (end-user),

his/her environment, end goal, and

existing competency.

Page 11: SweetPea -  Pre-Release Overview

Develop Something Scalable

SweetPea has no dependencies which

makes it extremely portable as a micro

web framework. Designed to be used

easily on shared hosting environments.

When your ready for Virtual, Dedicated

or Self Hosting, it scales like a good

web application framework should.

Page 12: SweetPea -  Pre-Release Overview

Scales How?...from the cli

sweetpea make -–script

Generates a minimalist application.

(no deps beyond SweetPea and CGI).

...from the cli

sweetpea make

Generates an MVC fashioned application over top of the

existing application. (refactoring optional)

Page 13: SweetPea -  Pre-Release Overview

Provide The Obvious

Web application frameworks should be

designed for the majority not the

minority. The majority of application

types, developers, environments, etc.

This should be the number one

consideration in framework

development.

Page 14: SweetPea -  Pre-Release Overview

Care About The End-UserSweetPea and SweetPea::Application provide a

unified API for dealing with the "majority" of web

application development issues. The API syntax is

one of common sense, see if this makes sense to

you:

$s->email->message(...)

$s->session->param(...)

$s->validate->table(...)

$s->template->render, etc.

Page 15: SweetPea -  Pre-Release Overview

Lead By Example

SweetPea and SweetPea::Application

are web application frameworks with

the same ideology and architecture for

different situations. SweetPea for small

jobs, and SweetPea::Application for

larger projects. Scale from SweetPea

to SweetPea::Application with ease

and without refactoring.

Page 16: SweetPea -  Pre-Release Overview

SweetPeaSweetPea is a micro web application framework, meaning it

only contains simple web application functionality such as URL

routing, parameter access, etc.

Remember the "contact-form" script? For all intents and

purposes it is a web application that simply looks for input

parameters from the browser and sends an email to the

defined email address.

Implementing this using Catalyst, Mojolicious, and

SweetPea::Application would be overkill. SweetPea

offers a means of starting small and scaling with minimal (to

no) refactoring.

Page 17: SweetPea -  Pre-Release Overview

RTFM?

Sure, end-users should read the

accompanying documentation although

a user-friendly framework will possess

a coding-style and API that is of

common-sense. e.g.

my $s = sweet;

$s->error->message(“Bug...");

Page 18: SweetPea -  Pre-Release Overview

Not A DictatorAll showcased functionality is lazy-loaded via the SweetPea

"plug" method which means that it exists for developers that

prefer it but can be ignored or overwritten by developers that

prefer something else. e.g. SweetPea::Application has a built in

ORM (object relational mapper),

$s->dbo->table->read->first->{column},

...

Page 19: SweetPea -  Pre-Release Overview

…If you don't like SweetPea::Application::ORM or prefer

DBIx::Class, Rose::DB or something else, integrate it via

$s->plug('dbo', sub{

require 'qw(MyDB::Schema)';

return MyDB::Schema->connect($dbi_dsn, $user, $pass, \%dbi_params);

});

then use it in Models, Views and Controllers as follows:

e.g. $s->dbo->resultset('Table')->all

Page 20: SweetPea -  Pre-Release Overview

Convention Over Configuration"Designing for the majority and not the minority" as stated

earlier, is simply another way of expressing the convention

over configuration ideology. This is an important philosophy for

a web framework that cares about its end-users because

beginners and intermediate-level developers with varying

competency need to achieve the same results as experienced

developers. I figure, if your elite enough to prefer

configuration over convention, your smart enough to

figure out how to configure a framework that uses

convention.

Page 21: SweetPea -  Pre-Release Overview

What Can SweetPea Do For You?

Besides be the wind beneath your

wings, SweetPea itself will produce a

fast, solid and scalable application, but

you should see for yourself.....

Page 22: SweetPea -  Pre-Release Overview

I Am SweetPea, This Is What I Do...

Page 23: SweetPea -  Pre-Release Overview

Route URLs to Actionsmy $s = sweet;

$s->route({

'/' => sub {

my $s = shift;

...

},

'/example' => sub {

...

},

'/other' => sub {

shift->forward('/other_controller');

},

});

Page 24: SweetPea -  Pre-Release Overview

Define Global Startup and Shutdown Routinesmy $s = sweet;

$s->route({

'/root/_startup' => sub {

my $s = shift;

# this happens all the time :)

},

'/root/_shutdown' => sub {

my $s = shift;

...

}

});

Page 25: SweetPea -  Pre-Release Overview

Forward and Detach like Catalystmy $s = sweet;

$s->route({

'/test1' => sub {

my $s = shift;

$s->forward('/test2');

},

'/test2' => sub {

my $s = shift;

$s->detach('/test3');

}

'/test3' => sub {

my $s = shift;

...

}

});

Page 26: SweetPea -  Pre-Release Overview

Access Any Parametermy $s = sweet;

$s->route({

'/:url_param' => sub {

my $s = shift;

$s->param('url_param');

},

'/test/*' => sub {

my $s = shift;

$s->param('*');

}

'/test1' => sub {

my $s = shift;

$s->param('input'); # get or post param

$s->param('session'); # session param

}

});

Page 27: SweetPea -  Pre-Release Overview

I Am SweetPea::Application, Now See What I Can Do...

Page 28: SweetPea -  Pre-Release Overview

Don't Need Routes Defined

I run as a script under any web server.

By default, URLs are matched using

http:://localhost/(controller)/(action)

my $s = sweet;

$s->run;

Page 29: SweetPea -  Pre-Release Overview

Promotes Code Reuse Via MVCpackage Controller::YourController;

package View::YourView;

package Model::YourModel;

Page 30: SweetPea -  Pre-Release Overview

Execute Common Code Globally or Locally

Automatically execute code locally

or globally with each request...

sub _startup {

# in Controller/Root.pm ...

}

sub _shutdown {

# in Controller/Root.pm ...

}

Page 31: SweetPea -  Pre-Release Overview

Local Auto-Executing…

in Controller/YourController.pm

sub _begin {

...

}

sub _end {

...

}

Page 32: SweetPea -  Pre-Release Overview

Hide Actions From The BrowserController actions prefixed with an underscore are denied

access from the browser.

http://localhost/admin/_login = access denied

package Controller::Admin;

sub login {

my ($self, $s) = @_;

$self->_login($s) if ...;

}

sub _login {

my ($self, $s) = @_;

...

}

Page 33: SweetPea -  Pre-Release Overview

MVC All The Way HomeThe SweetPea MVC architecture promotes clean readable

and reusable code, e.g.

package Controller::Foo;

sub _index {

my ($self, $s) = @_;

# reuse default data updating function (centralized)

$s->model('Schema')->update_log;

# reuse default template rendering (centralized)

$s->view('Default')->render;

}

Page 34: SweetPea -  Pre-Release Overview

Most Web Applications Send Email

Since most web applications send email(s), doesn't it

make sense to include that ability, ..., the answer is yes.

$s->email->message({

to => ...,

from => ...,

subject => ...,

message => ...,

})->send('sendmail');

Page 35: SweetPea -  Pre-Release Overview

Send Emails How?# send using sendmail,

$s->email->message({...})->send('sendmail');

# or SMTP

$s->email->message({...})->send('smtp');

# or Both, or use one for Bulk Messages

$s->email->message({...})->send('sendmail');

$s->email->message({...})->send('smtp');

Page 36: SweetPea -  Pre-Release Overview

Send Pretty EmailsMost modern web applications want to send HTML emails

for marketing, etc. Send application pages as email

attachments without WWW. Yes, no sub HTTP request

using LWP or WWW::Mechanize (which means access to

pages can be secured using built-in authorization info

(cookies), etc)

Page 37: SweetPea -  Pre-Release Overview

…$s->email->message({

...,

message => ...,

webpages => [

'/example/email/letter' => 'welcome_letter.html'

],

attachments => [

'file_and_location' => 'attachment_name'

]

})->send('sendmail');

Page 38: SweetPea -  Pre-Release Overview

Won't Cost An ORMSweetPea::Application ships with an ORM (object-

relational-mapper) that performs basic crud. e.g. The auto-

generated database tables contains a users table and a

permissions table and the ORM is aware of that.

my $user = $s->dbo->users;

$user->read;

for (0..$user->count) {

print $user->column;

$user->column('new stuff');

$user->update($user->current, $user->id);

}

Page 39: SweetPea -  Pre-Release Overview

Validate Input Manually or Automagically... if $s->validate->table('users');

... if $s->validate->profile('login');

my $input = $s->cgi->Vars;

my $profile = { required => ['foo','bar','baz'] };

... if $s->validate->input($input, $profile)

Page 40: SweetPea -  Pre-Release Overview

HTML Form and Table Tedium Eliminated(Or at least severely hindered...)

my $profile = 'table/users'; # or whatever

$s->builder->form($profile)->render;

$s->builder->grid($profile)->render;

Page 41: SweetPea -  Pre-Release Overview

Leave The Security To The Framework (It Works)Role-Based Access Control (Super Flexible)

my $access = $s->rbac;

if ($access->authorized) {

if ($access->role('manager')) {

if ($access->can('/manage accounts/create account')) {

# well I suppose we need to allow access

}

}

}

Page 42: SweetPea -  Pre-Release Overview

Alter Configuration From The Application

You provide the interface, we'll

provide the ...

my $configuration = $s->config->get('/application');

$configuration->{datastore} = 'production';

$s->config->set('/application');

Page 43: SweetPea -  Pre-Release Overview

Templates Within Templatesmy $t = $s->template;

$t->render('navigation')->to('sidebar');

$t->render({

template => 'index',

layout => 'default'

});

... Templates even have access to the unified API

<a href="[% s.url('/static/index.html') %]">Link</a>

[% content %] <div>[% t.sidebar %]</div>

Page 44: SweetPea -  Pre-Release Overview

English Is Not The Only Languagemy $l = $s->locale;

$l->language('en');

my $text = $l->text('hello_message');

Page 45: SweetPea -  Pre-Release Overview

I'm Sold, How Do I Get Started?

Page 46: SweetPea -  Pre-Release Overview

cpan SweetPea::Application

Or

cpan SweetPea

Page 47: SweetPea -  Pre-Release Overview

Make Something# a minimalist app

sweetpea-app make -–script

# or a standard sweetpea app

sweetpea-app make –-basic

# or a full-stack app

sweetpea-app make --stack

Page 48: SweetPea -  Pre-Release Overview

Using A Database?sweetpea-app data –-create

--dsn=dbi:pg:dbname=something --user=root

Page 49: SweetPea -  Pre-Release Overview

Be Fruitful and Multiplysweetpea-app model --name=NewModel

sweetpea-app model --name=NewModel/Other

sweetpea-app view --name=Email

sweetpea-app view --name=Email/NewsLetter

sweetpea-app ctrl --name=Admin

sweetpea-app ctrl --name=Admin/Dashboard

Page 50: SweetPea -  Pre-Release Overview

In Summation$Awesome if

'SweetPea' or 'SweetPea::Application';

By Al NewkirkCPAN: awncorp

IRC: perletc or awnstudio

Twitter: newkirka