Tools for ProductivityRESTful Web Services with Mojolicious and DBIx::                     Class
About MeTudor Constantin● Perl Developer @ Evozon● http://programming.tudorconstantin.com/● http://stackoverflow.  com/use...
Content●   Objectives●   Solutions●   Implementation●   Conclusions
Objectives● Find a way to create generic, distributed DB  oriented applications● One backend, multiple frontends, same  fu...
Solutions● Implement only the business logic in Perl● Expose it through an API  ○   REST  ○   SOAP  ○   RPC  ○   WSDL● Ser...
SolutionsChosen one is the REST architecture:● The simplest to implement (from server and  client perspective)● Uniform in...
ImplementationRESTful routes:GET /api/user/1UPDATE /api/user/5DELETE /api/user/3
ImplementationDefine Mojolicious routes:$r->get(/api/user/:id)->to(user#show)->name(user_details);$r->put(/api/user/:id)->...
ImplementationMethods needed to implement for eachresource:● list - returns a collection of resources (list of  users)● sh...
ImplementationThe above operations:● each corresponds to a DB CRUD operation● each can get implemented in a controller  me...
Implementation - Overview
Implementation
sub update{  my $self = shift; my $result_rs = $self->app->model   ->resultset( $self->{resource} )   ->search_rs(       {...
Steps for having REST1. Create the DB tables2. Generate the DBIx::Class models with   DBIx::Class::Schema::Loader   a. add...
Conclusions● Very fast to develop  ○ There are only 6 steps    ■ 4 of them are, or can be, automated● Completely DRY  ○ de...
EndSample app (with the generic controller) can befound on github:https://github.com/tudorconstantin/expense-trackerThe ge...
Perl Tools for Productivity
Upcoming SlideShare
Loading in...5
×

Perl Tools for Productivity

2,269

Published on

Another presentation I had at YAPC::EU 2012.
It's about how to fastly develop RESTful web services with Mojolicious and DBIx::Class

Published in: Technology
0 Comments
3 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
2,269
On Slideshare
0
From Embeds
0
Number of Embeds
3
Actions
Shares
0
Downloads
16
Comments
0
Likes
3
Embeds 0
No embeds

No notes for slide

Perl Tools for Productivity

  1. 1. Tools for ProductivityRESTful Web Services with Mojolicious and DBIx:: Class
  2. 2. About MeTudor Constantin● Perl Developer @ Evozon● http://programming.tudorconstantin.com/● http://stackoverflow. com/users/459233/tudor-constantin● https://github.com/tudorconstantin● http://www.linkedin. com/in/tudorconstantin
  3. 3. Content● Objectives● Solutions● Implementation● Conclusions
  4. 4. Objectives● Find a way to create generic, distributed DB oriented applications● One backend, multiple frontends, same functionality: ○ web ○ mobile ○ desktop● Rapid app development for the backend ○ Avoid boilerplate code ○ Adhere to the DRY principle ○ Generate as much code as possible ○ Have a predictible API
  5. 5. Solutions● Implement only the business logic in Perl● Expose it through an API ○ REST ○ SOAP ○ RPC ○ WSDL● Services Oriented Architecture
  6. 6. SolutionsChosen one is the REST architecture:● The simplest to implement (from server and client perspective)● Uniform interface ○ simplifies and decouples the architecture● Stateless - each request can be treated on a different machine ○ Horizontal scalability
  7. 7. ImplementationRESTful routes:GET /api/user/1UPDATE /api/user/5DELETE /api/user/3
  8. 8. ImplementationDefine Mojolicious routes:$r->get(/api/user/:id)->to(user#show)->name(user_details);$r->put(/api/user/:id)->to(user#update)->name(user_update);Mojolicious routes are in a 1 to 1 correlationwith the REST ones
  9. 9. ImplementationMethods needed to implement for eachresource:● list - returns a collection of resources (list of users)● show - return an individual resource (1 user)● create - create a new resource● delete● update
  10. 10. ImplementationThe above operations:● each corresponds to a DB CRUD operation● each can get implemented in a controller method● each can be implemented in a generic way: ○ get parameters, do stuff, return json
  11. 11. Implementation - Overview
  12. 12. Implementation
  13. 13. sub update{ my $self = shift; my $result_rs = $self->app->model ->resultset( $self->{resource} ) ->search_rs( { id => $self->param(id) }, ); return $self->render_not_found if ( scalar( ( $result_rs->all ) ) == 0 );$result_rs->update_all( $self->{_payload} );$result_rs->result_class(DBIx::Class::ResultClass::HashRefInflator); my @result = $result_rs->all(); return $self->render_json( @result );}
  14. 14. Steps for having REST1. Create the DB tables2. Generate the DBIx::Class models with DBIx::Class::Schema::Loader a. add the relationships between models3. Create the controllers which inherit from the Base controller4. (optional) Override the desired methods (ie - customize the business logic)5. Create the routes to Mojolicious
  15. 15. Conclusions● Very fast to develop ○ There are only 6 steps ■ 4 of them are, or can be, automated● Completely DRY ○ define the table structure only once - when it is created ○ you write a column name only when you need to do something special with it (validate for example)● Very easy to test ○ end to end testing can be done with simple curl operations
  16. 16. EndSample app (with the generic controller) can befound on github:https://github.com/tudorconstantin/expense-trackerThe generic controller:https://github.com/tudorconstantin/expense-tracker/blob/master/lib/ExpenseTracker/Controllers/Base.pm
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×