SlideShare a Scribd company logo
Tools for Productivity
RESTful Web Services with Mojolicious and DBIx::
                     Class
About Me
Tudor 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
Content
●   Objectives
●   Solutions
●   Implementation
●   Conclusions
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
Solutions
● Implement only the business logic in Perl
● Expose it through an API
  ○   REST
  ○   SOAP
  ○   RPC
  ○   WSDL
● Services Oriented Architecture
Solutions
Chosen 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
Implementation
RESTful routes:

GET /api/user/1

UPDATE /api/user/5

DELETE /api/user/3
Implementation
Define 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 correlation
with the REST ones
Implementation
Methods needed to implement for each
resource:
● list - returns a collection of resources (list of
  users)
● show - return an individual resource (1
  user)
● create - create a new resource
● delete
● update
Implementation
The 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
Implementation - Overview
Implementation
Perl Tools for Productivity
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 );
}
Steps for having REST
1. Create the DB tables
2. Generate the DBIx::Class models with
   DBIx::Class::Schema::Loader
   a. add the relationships between models
3. Create the controllers which inherit from the
   Base controller
4. (optional) Override the desired methods (ie -
   customize the business logic)
5. Create the routes to Mojolicious
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
End
Sample app (with the generic controller) can be
found on github:
https://github.com/tudorconstantin/expense-
tracker

The generic controller:
https://github.com/tudorconstantin/expense-
tracker/blob/master/lib/ExpenseTracker/Cont
rollers/Base.pm

More Related Content

PPTX
Functional Programming In JS
Damian Łabas
 
PDF
Web App Prototypes with Google App Engine
Vlad Filippov
 
PPTX
Azure functions: from a function to a whole application in 60 minutes
Alessandro Melchiori
 
PDF
JS Fest 2019: Comparing Node.js processes and threads for clustering HTTP, TC...
Vitalii Kukhar
 
PPTX
JS Fest 2019/Autumn. Виталий Кухар. Сравнение кластеризации HTTP, TCP и UDP н...
JSFestUA
 
PDF
Intro Couchdb
selvamanisampath
 
ODP
Migrations
Yaron Tal
 
PPT
Mssql to mysql - Anton Ivanov
DrupalCamp Kyiv
 
Functional Programming In JS
Damian Łabas
 
Web App Prototypes with Google App Engine
Vlad Filippov
 
Azure functions: from a function to a whole application in 60 minutes
Alessandro Melchiori
 
JS Fest 2019: Comparing Node.js processes and threads for clustering HTTP, TC...
Vitalii Kukhar
 
JS Fest 2019/Autumn. Виталий Кухар. Сравнение кластеризации HTTP, TCP и UDP н...
JSFestUA
 
Intro Couchdb
selvamanisampath
 
Migrations
Yaron Tal
 
Mssql to mysql - Anton Ivanov
DrupalCamp Kyiv
 

What's hot (20)

PDF
Flux - new/old approach to building frontend
Bartek Witczak
 
PDF
C# 4.0 dynamic
Wiryadi Adidharma
 
PPT
Headless drupal + react js Oleksandr Linyvyi
DrupalCamp Kyiv
 
PPTX
DOM & Events
CHAITANYA KUMAR REDDY
 
PDF
Large Scale Vandalism Detection in Knowledge Bases: PyData Berlin 2017
Alexey Grigorev
 
PDF
Lean and mean MongoDB
Oleg Podsechin
 
PDF
WebUI - rapid UI development for EGS-CC
Dariusz Walczak
 
PDF
OSMC 2018 | Visualization of your distributed infrastructure by Nicolai Buchwitz
NETWAYS
 
PDF
Scaling xtext
Lieven Lemiengre
 
PPTX
Apple.combine
mober77
 
PDF
JavaScript Roadmap - DOM Manipulation
Aswin Barath
 
PDF
An Introduction to MongoDB
Chamodi Adikaram
 
PDF
11 schema design & crud
Ahmed Elbassel
 
PDF
OSMC 2018 | Stream connector: Easily sending events and/or metrics from the C...
NETWAYS
 
PPTX
JavaScript | Introduction
Velimir Bulatovic
 
PDF
Locomotor: transparent migration of client-side database code
Michael Mior
 
PDF
Gdg dev fest hybrid apps your own mini-cordova
Ayman Mahfouz
 
PDF
Ng init | EPI Sousse
Hamdi Hmidi
 
PPTX
Go bei der 4Com GmbH & Co. KG
Jonas Riedel
 
PDF
Docker @haufe lexware tech lunch
HaufeLexwareRomania
 
Flux - new/old approach to building frontend
Bartek Witczak
 
C# 4.0 dynamic
Wiryadi Adidharma
 
Headless drupal + react js Oleksandr Linyvyi
DrupalCamp Kyiv
 
DOM & Events
CHAITANYA KUMAR REDDY
 
Large Scale Vandalism Detection in Knowledge Bases: PyData Berlin 2017
Alexey Grigorev
 
Lean and mean MongoDB
Oleg Podsechin
 
WebUI - rapid UI development for EGS-CC
Dariusz Walczak
 
OSMC 2018 | Visualization of your distributed infrastructure by Nicolai Buchwitz
NETWAYS
 
Scaling xtext
Lieven Lemiengre
 
Apple.combine
mober77
 
JavaScript Roadmap - DOM Manipulation
Aswin Barath
 
An Introduction to MongoDB
Chamodi Adikaram
 
11 schema design & crud
Ahmed Elbassel
 
OSMC 2018 | Stream connector: Easily sending events and/or metrics from the C...
NETWAYS
 
JavaScript | Introduction
Velimir Bulatovic
 
Locomotor: transparent migration of client-side database code
Michael Mior
 
Gdg dev fest hybrid apps your own mini-cordova
Ayman Mahfouz
 
Ng init | EPI Sousse
Hamdi Hmidi
 
Go bei der 4Com GmbH & Co. KG
Jonas Riedel
 
Docker @haufe lexware tech lunch
HaufeLexwareRomania
 
Ad

Similar to Perl Tools for Productivity (20)

PDF
RESTful web services
Tudor Constantin
 
PDF
RedisConf18 - Writing modular & encapsulated Redis code
Redis Labs
 
PDF
GDayX - Advanced Angular.JS
Nicolas Embleton
 
PDF
Nicolas Embleton, Advanced Angular JS
JavaScript Meetup HCMC
 
PDF
FITC presents: Mobile & offline data synchronization in Angular JS
FITC
 
PDF
Modern Perl Web Development with Dancer
Dave Cross
 
ODP
2014 11 20 Drupal 7 -> 8 test migratie
hcderaad
 
PPTX
Introduction To MongoDB
ElieHannouch
 
PDF
Creating a custom API for a headless Drupal
Exove
 
ODP
Android training day 4
Vivek Bhusal
 
PDF
Thinking in Components
Anton Ivanov
 
PDF
Introduction to mongo db
Lawrence Mwai
 
PDF
Parse cloud code
維佋 唐
 
PPTX
Introduction to MongoDB
Chun-Kai Wang
 
PDF
RESTful with Drupal - in-s and out-s
Kalin Chernev
 
PDF
Android Jetpack - Google IO Extended Singapore 2018
Hassan Abid
 
PDF
Extending CMS Made Simple
cmsmssjg
 
PPTX
Dev Jumpstart: Build Your First App with MongoDB
MongoDB
 
PDF
Docker tlv
Ariel Moskovich
 
PDF
Declarative Infrastructure Tools
Yulia Shcherbachova
 
RESTful web services
Tudor Constantin
 
RedisConf18 - Writing modular & encapsulated Redis code
Redis Labs
 
GDayX - Advanced Angular.JS
Nicolas Embleton
 
Nicolas Embleton, Advanced Angular JS
JavaScript Meetup HCMC
 
FITC presents: Mobile & offline data synchronization in Angular JS
FITC
 
Modern Perl Web Development with Dancer
Dave Cross
 
2014 11 20 Drupal 7 -> 8 test migratie
hcderaad
 
Introduction To MongoDB
ElieHannouch
 
Creating a custom API for a headless Drupal
Exove
 
Android training day 4
Vivek Bhusal
 
Thinking in Components
Anton Ivanov
 
Introduction to mongo db
Lawrence Mwai
 
Parse cloud code
維佋 唐
 
Introduction to MongoDB
Chun-Kai Wang
 
RESTful with Drupal - in-s and out-s
Kalin Chernev
 
Android Jetpack - Google IO Extended Singapore 2018
Hassan Abid
 
Extending CMS Made Simple
cmsmssjg
 
Dev Jumpstart: Build Your First App with MongoDB
MongoDB
 
Docker tlv
Ariel Moskovich
 
Declarative Infrastructure Tools
Yulia Shcherbachova
 
Ad

Recently uploaded (20)

PPTX
cloud computing vai.pptx for the project
vaibhavdobariyal79
 
PDF
Advances in Ultra High Voltage (UHV) Transmission and Distribution Systems.pdf
Nabajyoti Banik
 
PDF
Trying to figure out MCP by actually building an app from scratch with open s...
Julien SIMON
 
PDF
AI-Cloud-Business-Management-Platforms-The-Key-to-Efficiency-Growth.pdf
Artjoker Software Development Company
 
PDF
REPORT: Heating appliances market in Poland 2024
SPIUG
 
PDF
OFFOFFBOX™ – A New Era for African Film | Startup Presentation
ambaicciwalkerbrian
 
PDF
Cloud-Migration-Best-Practices-A-Practical-Guide-to-AWS-Azure-and-Google-Clou...
Artjoker Software Development Company
 
PDF
Orbitly Pitch Deck|A Mission-Driven Platform for Side Project Collaboration (...
zz41354899
 
PDF
Presentation about Hardware and Software in Computer
snehamodhawadiya
 
PDF
Google I/O Extended 2025 Baku - all ppts
HusseinMalikMammadli
 
PDF
CIFDAQ's Market Wrap : Bears Back in Control?
CIFDAQ
 
PDF
Brief History of Internet - Early Days of Internet
sutharharshit158
 
PDF
NewMind AI Weekly Chronicles - July'25 - Week IV
NewMind AI
 
PDF
Responsible AI and AI Ethics - By Sylvester Ebhonu
Sylvester Ebhonu
 
PDF
Structs to JSON: How Go Powers REST APIs
Emily Achieng
 
PDF
How Open Source Changed My Career by abdelrahman ismail
a0m0rajab1
 
PPTX
IT Runs Better with ThousandEyes AI-driven Assurance
ThousandEyes
 
PDF
A Strategic Analysis of the MVNO Wave in Emerging Markets.pdf
IPLOOK Networks
 
PPTX
OA presentation.pptx OA presentation.pptx
pateldhruv002338
 
PDF
Research-Fundamentals-and-Topic-Development.pdf
ayesha butalia
 
cloud computing vai.pptx for the project
vaibhavdobariyal79
 
Advances in Ultra High Voltage (UHV) Transmission and Distribution Systems.pdf
Nabajyoti Banik
 
Trying to figure out MCP by actually building an app from scratch with open s...
Julien SIMON
 
AI-Cloud-Business-Management-Platforms-The-Key-to-Efficiency-Growth.pdf
Artjoker Software Development Company
 
REPORT: Heating appliances market in Poland 2024
SPIUG
 
OFFOFFBOX™ – A New Era for African Film | Startup Presentation
ambaicciwalkerbrian
 
Cloud-Migration-Best-Practices-A-Practical-Guide-to-AWS-Azure-and-Google-Clou...
Artjoker Software Development Company
 
Orbitly Pitch Deck|A Mission-Driven Platform for Side Project Collaboration (...
zz41354899
 
Presentation about Hardware and Software in Computer
snehamodhawadiya
 
Google I/O Extended 2025 Baku - all ppts
HusseinMalikMammadli
 
CIFDAQ's Market Wrap : Bears Back in Control?
CIFDAQ
 
Brief History of Internet - Early Days of Internet
sutharharshit158
 
NewMind AI Weekly Chronicles - July'25 - Week IV
NewMind AI
 
Responsible AI and AI Ethics - By Sylvester Ebhonu
Sylvester Ebhonu
 
Structs to JSON: How Go Powers REST APIs
Emily Achieng
 
How Open Source Changed My Career by abdelrahman ismail
a0m0rajab1
 
IT Runs Better with ThousandEyes AI-driven Assurance
ThousandEyes
 
A Strategic Analysis of the MVNO Wave in Emerging Markets.pdf
IPLOOK Networks
 
OA presentation.pptx OA presentation.pptx
pateldhruv002338
 
Research-Fundamentals-and-Topic-Development.pdf
ayesha butalia
 

Perl Tools for Productivity

  • 1. Tools for Productivity RESTful Web Services with Mojolicious and DBIx:: Class
  • 2. About Me Tudor 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. Content ● Objectives ● Solutions ● Implementation ● Conclusions
  • 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. Solutions ● Implement only the business logic in Perl ● Expose it through an API ○ REST ○ SOAP ○ RPC ○ WSDL ● Services Oriented Architecture
  • 6. Solutions Chosen 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
  • 9. Implementation Methods needed to implement for each resource: ● list - returns a collection of resources (list of users) ● show - return an individual resource (1 user) ● create - create a new resource ● delete ● update
  • 10. Implementation The 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
  • 14. 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 ); }
  • 15. Steps for having REST 1. Create the DB tables 2. Generate the DBIx::Class models with DBIx::Class::Schema::Loader a. add the relationships between models 3. Create the controllers which inherit from the Base controller 4. (optional) Override the desired methods (ie - customize the business logic) 5. Create the routes to Mojolicious
  • 16. 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
  • 17. End Sample app (with the generic controller) can be found on github: https://github.com/tudorconstantin/expense- tracker The generic controller: https://github.com/tudorconstantin/expense- tracker/blob/master/lib/ExpenseTracker/Cont rollers/Base.pm