SlideShare a Scribd company logo
1 of 22
Download to read offline
Developing
OpenResty
Framework
USING DECOUPLED LIBRARIES
by Aapo Talvensaari @ OpenResty Con 2015
Where Did I Get Here?
About 6.500 Kilometers – Almost Nothing Compared to The Length of The Great Wall of China
UsingDecoupledLibraries
DevelopingOpenRestyFramework
2
Who Am I?
PROFESSIONALLY
WEB PROGRAMMER SINCE 90’S
ColdFusion ➠ ASP ➠ PHP ➠ Java ➠ ASP.NET ➠ OpenResty
SYSTEM ADMINISTRATOR
Linux and Windows Platforms, and Cloud
JOB
IT Manager at Aalto Capital Oy
Founder of Talvensaari Solutions Oy
UsingDecoupledLibraries
DevelopingOpenRestyFramework
3
What to Expect?
u  Short History
u  Basis for Building Web Applications
u  Routing
u  Templating
u  Validation and Filtering
u  Sessions
u  Style Sheets
u  Libraries
u  Kronometrix Analytics
In addition to English with Finnish
accent, I will show you some of my
libraries and how they work together.
Checkout also Leaf Corcoran’s
Excellent Lapis Framework:
http://leafo.net/lapis/
UsingDecoupledLibraries
DevelopingOpenRestyFramework
4
Why OpenResty?
u  I wanted to expand skills, and learn something new
u  I wanted to get a rid of gateway interfaces – less moving parts
u  A real web server with scripting language support
(Basically a new Apache + mod_php with a modern twist, e.g. Web Sockets support)
u  I was already using Nginx
u  Alternatives that I looked
(All the alternatives actually looked really nice, but I felt more comfortable with OpenResty)
u  Node.js (I didn’t enjoy JavaScript, and it wasn't using a real web server)
u  Python / Ruby (too object oriented, usually behind a gateway interface)
u  Go (not a scripting language, no particular web development focus)
u  Simplicity, both in development and deployment, and Performance
UsingDecoupledLibraries
DevelopingOpenRestyFramework
5
OpenResty Stack
Nginx
by Igor Sysoev et al.
Web Server
OpenResty
by Yichun Zhang et al.
Web Application Server
Lua
by Roberto Ierusalimschy et al.
Programming Language
UsingDecoupledLibraries
DevelopingOpenRestyFramework
6
Routing
LUA-RESTY-ROUTE
u  Nginx already does routing! Why oh why?
u  Makes web development enjoyable! Works as a glue.
u  Nginx configuration can be fairly static
u  Nice DSL for Specifying Route Handlers
u  Middleware
u  Before and After Filters
u  Web Sockets Routing
u  Pluggable Matchers
u  Simple – ngx.re.match + pre-defined :string and :number matchers
u  Regex – ngx.re.match
u  Match – string.match
u  Equals – plain ==, no pattern or string matching
7
local r = require "resty.route"!
local route = r.new()!
-- Use a Middleware!
-- (not needed in this example)!
route:use "reqargs" {}!
-- Add HTTP GET Route!
route:get("/hello", function()!
ngx.print "世界你好"!
end)!
-- Dispatch the Request!
route:dispatch()!
Templating
LUA-RESTY-TEMPLATE
u  One of the Top OpenResty
Libraries in GitHub
u  It's a Compiling Templating Engine
u  Just Another Way to Write Lua
u  Almost all you can do in Lua,
you can do in Template
u  Basically it is just plain string
concatenation after all
(white space and line-feed handling is fine tuned)
u  Supports Lua / LuaJIT / OpenResty
u  Handwritten Parser Using string.find
u  Features
u  Layouts
u  Blocks
u  Include
u  Comments
u  Verbatim / Raw Blocks
u  Automatic Escaping
u  Precompilation
u  Caching
u  Preloading (WIP)
UsingDecoupledLibraries
DevelopingOpenRestyFramework
8
UsingDecoupledLibraries
DevelopingOpenRestyFramework
9<!DOCTYPE html>!
<html lang="{{lang}}">!
<head>!
<meta charset="{{charset}}">!
<title>{{title}}</title>!
{*blocks.styles*}!
{#!
Commented out, because it causes a problems (see issue #35):!
<script src="js/analytics.js"></script>!
#}!
{-head_scripts-}!
<script src="js/app.js"></script>!
{-head_scripts-}!
</head>!
<body>!
{-raw-}!
Everything inside here is outputted as is, e.g. {{not evaluated}}!
{-raw-}!
{(include/header.html)}!
{[concat({"include/navigation", access.level, ".html"}, "-")]}!
<section id="main">!
{* view *}!
</section>!
{(include/footer.html)}!
{% for _, script in ipairs(scripts) do %}!
<script src="{{script}}"></script>!
{% end %}!
{*blocks.head_scripts*}!
</body>!
</html>!
UsingDecoupledLibraries
DevelopingOpenRestyFramework
10context=(...) or {}!
local function include(v, c)!
return template.compile(v)(c or context)!
end!
local ___,blocks,layout={},blocks or {}!
___[#___+1]=[=[!
<!DOCTYPE html>!
<html lang="]=]!
___[#___+1]=template.escape(lang)!
___[#___+1]=[=[!
">!
<head>!
<meta charset="]=]!
___[#___+1]=template.escape(charset)!
___[#___+1]=[=[!
">!
<title>]=]!
___[#___+1]=template.escape(title)!
___[#___+1]=[=[!
</title>!
]=]!
___[#___+1]=template.output(blocks.styles)!
blocks["head_scripts"]=include[=[!
<script src="js/app.js"></script>!
]=]!
___[#___+1]=[=[!
</head>!
<body>!
]=]!
___[#___+1]=[=[!
Everything inside here is outputted as is, eg. {{not evaluated}}!
]=]!
UsingDecoupledLibraries
DevelopingOpenRestyFramework
11___[#___+1]=include([=[include/header.html]=])!
___[#___+1]=[=[!
!
]=]!
___[#___+1]=include(concat({"include/navigation", access.level, ".html"}, "-"))!
___[#___+1]=[=[!
!
<section id="main">!
]=]!
___[#___+1]=template.output( view )!
___[#___+1]=[=[!
!
</section>!
]=]!
___[#___+1]=include([=[include/footer.html]=])!
___[#___+1]=[=[!
!
]=]!
for _, script in ipairs(scripts) do !
___[#___+1]=[=[!
<script src="]=]!
___[#___+1]=template.escape(script)!
___[#___+1]=[=[!
"></script>!
]=]!
end !
___[#___+1]=template.output(blocks.head_scripts)!
___[#___+1]=[=[!
!
</body>!
</html>]=]!
return layout and include(layout,setmetatable({view=template.concat(___),blocks=blocks},{__index=context})) or
template.concat(___)!
Validation and Filtering
LUA-RESTY-VALIDATION
u  Both Validation and Filtering
u  Validation and Filter Chaining
u  Reusable Validators
(Define Once, Use in Many Places)
u  Automatic Conditional Validators
u  Group Validators and Filters
u  Easy to Extend
u  Report Errors in Frontend, JSON
Friendly
u  Send Data to Backend
u  Stop Validators
(e.g. optional)
Example Here
UsingDecoupledLibraries
DevelopingOpenRestyFramework
12
local v = require "resty.validation”!
local validate = {!
nick = v.string.trim:minlen(2),!
email = v.string.trim.email,!
password = v.string.trim:minlen(8)!
}!
-- First we create single validators!
-- for each form field!
local register = v.new{!
nick = validate.nick,!
email = validate.email,!
email2 = validate.email,!
password = validate.password,!
password2 = validate.password!
}!
-- Next we create group validators for email!
-- and password:!
register:compare "email == email2"!
register:compare "password == password2”!
-- And finally we return from this forms module!
return {!
register = register!
}!
Sessions
LUA-RESTY-SESSION
u  Secure Defaults
u  Configurable from Nginx Config
or Lua Code
u  Plugins for
u  Client and Server Storages
(Cookie, Shared Memory, Memcache, Redis)
u  Ciphers
(AES, HMAC)
u  Encoders
(Base64 and Base16)
u  Serializers
(JSON)
u  Identifiers
(WIP, Random, JWT, UUID)
UsingDecoupledLibraries
DevelopingOpenRestyFramework
13
local session = require "resty.session"!
!
-- Construct and start a session!
-- (new, and open also available)!
local s = session.start()!
!
-- Store some data to session!
s.data.name = "OpenResty Fan"!
!
-- Save a session!
s:save()!
!
-- Destroy a session!
s:destroy()!
!
-- Regenerate a session!
-- (e.g. when security context changes)!
s:regenerate()!
Style Sheets
LUA-RESTY-SASS
u  Syntactically Awesome Style Sheets
u  LuaJIT FFI Bindings to libSass
u  Automatic Online Compilers for OpenResty
u  Caching to Normal CSS Files Once Compiled
u  Save-And-Refresh Development
u  A Good and a Bad Thing: Errors in Sass File prevents compiling to CSS
Errors are catched earlier, but works differently than with plain CSS
u  Deploy Sass Files Directly to The Production, No Build Needed
u  More about Sass at http://sass-lang.com/
UsingDecoupledLibraries
DevelopingOpenRestyFramework
14
Demo
HOW THIS ALL WORKS TOGETHER?
Source Code Available at github.com/bungle/iresty
UsingDecoupledLibraries
DevelopingOpenRestyFramework
15
Libraries
COMMON NEEDS IN WEB DEVELOPMENT
u  Excel ➠ LUA-RESTY-LIBXL
u  Cryptography ➠ LUA-RESTY-NETTLE
u  Universally Unique Identifiers ➠ LUA-RESTY-UUID
u  Audio Metadata ➠ LUA-RESTY-TAGLIB
u  Markdown ➠ LUA-RESTY-HOEDOWN
u  File Information ➠ LUA-RESTY-FILEINFO
u  Translations ➠ LUA-RESTY-GETTEXT
u  Unicode ➠ LUA-RESTY-UNISTRING
u  JSON Pretty Formatting ➠ LUA-RESTY-PRETTYCJSON
CHECK MY GITHUB ACCOUNT FOR MORE
UsingDecoupledLibraries
DevelopingOpenRestyFramework
16
Excel
LUA-RESTY-LIBXL
LuaJIT FFI to a LibXL Library – a Library that Can Read and Write Excel Files
u  LibXL
u  Proprietary Library (from Slovakia)
u  Supports XLS and XLSX Formats
u  Pricing Starts from $ 199.00
u  Source Code for The Library is
Available for $ 2499.00
u  High Performance
u  No Extra Dependencies
u  lua-resty-libxl
u  Nice Lua API (almost full featured*)
* some Sheet APIs are still not finished
UsingDecoupledLibraries
DevelopingOpenRestyFramework
17
local excel = require "resty.libxl.book"!
local book = excel.new()!
book:load "demo.xlsx"!
local sheet = book.sheets[1]!
local value = sheet:read(1, 1)!
sheet:write(1, 1, value + math.pi)!
book:save "output.xlsx"!
u  Features
u  Pictures
u  Formats
u  Fonts and Colors
u  Hyperlinks
u  Equations
Cryptography
LUA-RESTY-NETTLE
LuaJIT FFI Bindings to GNU Nettle Library – a Low Level Cryptography Library
u  Hash Functions
u  SHA1, SHA2, SHA3
u  MD2, MD4, MD5
u  RIPEMD160, GOSTHASH94
u  Keyed Hash Functions
u  HMAC
u  SHA1, SHA2
u  MD5
u  RIPEMD160
u  UMAC
u  Poly1305
u  Key Derivation Functions
u  PBKDF2
u  SHA1
u  SHA2
u  Randomness
u  Yarrow
u  Knuth’s Lagged Fibonacci
u  ASCII Encoding
u  Base16, Base64, URL-safe Base64
UsingDecoupledLibraries
DevelopingOpenRestyFramework
18
Cryptography
LUA-RESTY-NETTLE
u  Cipher Functions
u  AES
u  ARCFOUR
u  ARCTWO
u  BLOWFISH
u  Camellia
u  CAST128
u  ChaCha
u  DES
u  DES3
u  Salsa20
u  SERPENT
u  TWOFISH
u  Cipher Modes
u  ECB
u  CBC
u  CTR
u  AEAD
Authenticated Encryption with Associated Data
u  EAX for AES
u  GCM for AES / Camellia
u  CCM for AES
u  ChaCha-Poly1305
u  Public Key Algorithms
Bindings for these are still work in progress, L.
UsingDecoupledLibraries
DevelopingOpenRestyFramework
19
Kronometrix
A REAL-TIME ANALYTICS APPLIANCE DESIGNED FOR TIME SERIES DATA ANALYSIS
u  Build on OpenResty and Lua
u  Using Redis as a Backend Store
u  1,000 Recording Devices per
Appliance in Real-Time
u  Data Recorders
u  Aviation Meteorology
u  Computer Performance Metrics
u  Windows
u  Linux
u  BSD
u  Solaris
u  Climatology
u  Architecture of The Appliance
u  Authentication Layer
u  OpenResty Server
u  Redis Authentication Database
u  Messaging Layer
u  OpenResty Server
u  Kernel Layer
u  OpenResty Server
u  Redis Statistics Database
u  Aggregate Layer
u  OpenResty Server
u  Redis Aggregates Database
UsingDecoupledLibraries
DevelopngOpenRestyFramework
20
OpenResty in a Future
IT'S BRIGHT FOR SURE
What I Will Be Doing?
u  Adding Documentation
u  Adding Tests
u  Maintaining Existing Libraries
u  More Libraries, and Bindings
u  lua-resty-password / -auth
u  lua-resty-chromelogger
u  lua-resty-cloudflare
u  Start Contributing on ngx_lua
u  A Modern Open Source CMS
What I Would Like to See?
u  Uniting Chinese and Western
Communities
u  Package Management
u  File APIs (Using Nginx File APIs?)
u  Official Packages for Platforms
u  LuaJIT Enhancements
u  Less NYIs
u  String Buffer
u  Lua 5.2 / 5.3 Support / Uncertainty
u  Optimizing for LuaJIT is Hard
UsingDecoupledLibraries
DevelopingOpenRestyFramework
21
Questions?
aapo.talvensaari@gmail.com bungle@github
UsingDecoupledLibraries
DevelopingOpenRestyFramework
22

More Related Content

What's hot

Webdevcon Keynote hh-2012-09-18
Webdevcon Keynote hh-2012-09-18Webdevcon Keynote hh-2012-09-18
Webdevcon Keynote hh-2012-09-18
Pierre Joye
 
Building a desktop app with HTTP::Engine, SQLite and jQuery
Building a desktop app with HTTP::Engine, SQLite and jQueryBuilding a desktop app with HTTP::Engine, SQLite and jQuery
Building a desktop app with HTTP::Engine, SQLite and jQuery
Tatsuhiko Miyagawa
 

What's hot (20)

Plack at YAPC::NA 2010
Plack at YAPC::NA 2010Plack at YAPC::NA 2010
Plack at YAPC::NA 2010
 
Webdevcon Keynote hh-2012-09-18
Webdevcon Keynote hh-2012-09-18Webdevcon Keynote hh-2012-09-18
Webdevcon Keynote hh-2012-09-18
 
Dev ninja -> vagrant + virtualbox + chef-solo + git + ec2
Dev ninja  -> vagrant + virtualbox + chef-solo + git + ec2Dev ninja  -> vagrant + virtualbox + chef-solo + git + ec2
Dev ninja -> vagrant + virtualbox + chef-solo + git + ec2
 
Front-end tools
Front-end toolsFront-end tools
Front-end tools
 
Intro to PSGI and Plack
Intro to PSGI and PlackIntro to PSGI and Plack
Intro to PSGI and Plack
 
Web development with Lua @ Bulgaria Web Summit 2016
Web development with Lua @ Bulgaria Web Summit 2016Web development with Lua @ Bulgaria Web Summit 2016
Web development with Lua @ Bulgaria Web Summit 2016
 
Plack basics for Perl websites - YAPC::EU 2011
Plack basics for Perl websites - YAPC::EU 2011Plack basics for Perl websites - YAPC::EU 2011
Plack basics for Perl websites - YAPC::EU 2011
 
Mongodb - drupal dev days
Mongodb - drupal dev daysMongodb - drupal dev days
Mongodb - drupal dev days
 
[drupalday2017] - REST in pieces
[drupalday2017] - REST in pieces[drupalday2017] - REST in pieces
[drupalday2017] - REST in pieces
 
Web development with Lua and Sailor @ GeeCon 2015
Web development with Lua and Sailor @ GeeCon 2015Web development with Lua and Sailor @ GeeCon 2015
Web development with Lua and Sailor @ GeeCon 2015
 
Lisp Meet Up #31, Clake: a GNU make-like build utility in Common Lisp
Lisp Meet Up #31, Clake: a GNU make-like build utility in Common LispLisp Meet Up #31, Clake: a GNU make-like build utility in Common Lisp
Lisp Meet Up #31, Clake: a GNU make-like build utility in Common Lisp
 
HTML5 tutorial: canvas, offfline & sockets
HTML5 tutorial: canvas, offfline & socketsHTML5 tutorial: canvas, offfline & sockets
HTML5 tutorial: canvas, offfline & sockets
 
About Clack
About ClackAbout Clack
About Clack
 
Lisp in the Cloud
Lisp in the CloudLisp in the Cloud
Lisp in the Cloud
 
Apache Sling as an OSGi-powered REST middleware
Apache Sling as an OSGi-powered REST middlewareApache Sling as an OSGi-powered REST middleware
Apache Sling as an OSGi-powered REST middleware
 
Building a desktop app with HTTP::Engine, SQLite and jQuery
Building a desktop app with HTTP::Engine, SQLite and jQueryBuilding a desktop app with HTTP::Engine, SQLite and jQuery
Building a desktop app with HTTP::Engine, SQLite and jQuery
 
Woo: Writing a fast web server
Woo: Writing a fast web serverWoo: Writing a fast web server
Woo: Writing a fast web server
 
[Community Open Camp] 給 PHP 開發者的 VS Code 指南
[Community Open Camp] 給 PHP 開發者的 VS Code 指南[Community Open Camp] 給 PHP 開發者的 VS Code 指南
[Community Open Camp] 給 PHP 開發者的 VS Code 指南
 
Selenium sandwich-3: Being where you aren't.
Selenium sandwich-3: Being where you aren't.Selenium sandwich-3: Being where you aren't.
Selenium sandwich-3: Being where you aren't.
 
Getting Started With Aura
Getting Started With AuraGetting Started With Aura
Getting Started With Aura
 

Viewers also liked

Nginx+Lua在京东商品详情页的大规模应用
Nginx+Lua在京东商品详情页的大规模应用Nginx+Lua在京东商品详情页的大规模应用
Nginx+Lua在京东商品详情页的大规模应用
OpenRestyCon
 
OpenResty: превращаем NGINX в полноценный сервер приложений / Владимир Прота...
OpenResty: превращаем NGINX в полноценный сервер приложений  / Владимир Прота...OpenResty: превращаем NGINX в полноценный сервер приложений  / Владимир Прота...
OpenResty: превращаем NGINX в полноценный сервер приложений / Владимир Прота...
Ontico
 

Viewers also liked (16)

Zi nginx conf_2015
Zi nginx conf_2015Zi nginx conf_2015
Zi nginx conf_2015
 
Nginx-lua
Nginx-luaNginx-lua
Nginx-lua
 
Using ngx_lua in UPYUN 2
Using ngx_lua in UPYUN 2Using ngx_lua in UPYUN 2
Using ngx_lua in UPYUN 2
 
Nginx+Lua在京东商品详情页的大规模应用
Nginx+Lua在京东商品详情页的大规模应用Nginx+Lua在京东商品详情页的大规模应用
Nginx+Lua在京东商品详情页的大规模应用
 
基于OpenResty的百万级长连接推送
基于OpenResty的百万级长连接推送基于OpenResty的百万级长连接推送
基于OpenResty的百万级长连接推送
 
Lightning Hedis
Lightning HedisLightning Hedis
Lightning Hedis
 
OpenResty: превращаем NGINX в полноценный сервер приложений / Владимир Прота...
OpenResty: превращаем NGINX в полноценный сервер приложений  / Владимир Прота...OpenResty: превращаем NGINX в полноценный сервер приложений  / Владимир Прота...
OpenResty: превращаем NGINX в полноценный сервер приложений / Владимир Прота...
 
Nginx+lua在阿里巴巴的使用
Nginx+lua在阿里巴巴的使用Nginx+lua在阿里巴巴的使用
Nginx+lua在阿里巴巴的使用
 
Advanced nginx in mercari - How to handle over 1,200,000 HTTPS Reqs/Min
Advanced nginx in mercari - How to handle over 1,200,000 HTTPS Reqs/MinAdvanced nginx in mercari - How to handle over 1,200,000 HTTPS Reqs/Min
Advanced nginx in mercari - How to handle over 1,200,000 HTTPS Reqs/Min
 
RESTful API的设计与开发
RESTful API的设计与开发RESTful API的设计与开发
RESTful API的设计与开发
 
API Gateway report
API Gateway reportAPI Gateway report
API Gateway report
 
以AWS Lambda與Amazon API Gateway打造無伺服器後端
以AWS Lambda與Amazon API Gateway打造無伺服器後端以AWS Lambda與Amazon API Gateway打造無伺服器後端
以AWS Lambda與Amazon API Gateway打造無伺服器後端
 
MySQL技术分享:一步到位实现mysql优化
MySQL技术分享:一步到位实现mysql优化MySQL技术分享:一步到位实现mysql优化
MySQL技术分享:一步到位实现mysql优化
 
Lua/LuaJIT 字节码浅析
Lua/LuaJIT 字节码浅析Lua/LuaJIT 字节码浅析
Lua/LuaJIT 字节码浅析
 
第一次用 Vue.js 就愛上 [改]
第一次用 Vue.js 就愛上 [改]第一次用 Vue.js 就愛上 [改]
第一次用 Vue.js 就愛上 [改]
 
Dev-Ops与Docker的最佳实践 QCon2016 北京站演讲
Dev-Ops与Docker的最佳实践 QCon2016 北京站演讲Dev-Ops与Docker的最佳实践 QCon2016 北京站演讲
Dev-Ops与Docker的最佳实践 QCon2016 北京站演讲
 

Similar to Developing OpenResty Framework

Alfresco in few points - Search Tutorial
Alfresco in few points - Search TutorialAlfresco in few points - Search Tutorial
Alfresco in few points - Search Tutorial
PASCAL Jean Marie
 

Similar to Developing OpenResty Framework (20)

Automate Yo' Self
Automate Yo' SelfAutomate Yo' Self
Automate Yo' Self
 
Not only SQL
Not only SQL Not only SQL
Not only SQL
 
Solid and Sustainable Development in Scala
Solid and Sustainable Development in ScalaSolid and Sustainable Development in Scala
Solid and Sustainable Development in Scala
 
Solid And Sustainable Development in Scala
Solid And Sustainable Development in ScalaSolid And Sustainable Development in Scala
Solid And Sustainable Development in Scala
 
PLAT-8 Spring Web Scripts and Spring Surf
PLAT-8 Spring Web Scripts and Spring SurfPLAT-8 Spring Web Scripts and Spring Surf
PLAT-8 Spring Web Scripts and Spring Surf
 
PySpark with Juypter
PySpark with JuypterPySpark with Juypter
PySpark with Juypter
 
Kubernetes and AWS Lambda can play nicely together
Kubernetes and AWS Lambda can play nicely togetherKubernetes and AWS Lambda can play nicely together
Kubernetes and AWS Lambda can play nicely together
 
DiUS Computing Lca Rails Final
DiUS  Computing Lca Rails FinalDiUS  Computing Lca Rails Final
DiUS Computing Lca Rails Final
 
HFM, Workspace, and FDM – Voiding your warranty
HFM, Workspace, and FDM – Voiding your warrantyHFM, Workspace, and FDM – Voiding your warranty
HFM, Workspace, and FDM – Voiding your warranty
 
JSON REST API for WordPress
JSON REST API for WordPressJSON REST API for WordPress
JSON REST API for WordPress
 
Elasticsearch Basics
Elasticsearch BasicsElasticsearch Basics
Elasticsearch Basics
 
Killing the Angle Bracket
Killing the Angle BracketKilling the Angle Bracket
Killing the Angle Bracket
 
DSLs in JavaScript
DSLs in JavaScriptDSLs in JavaScript
DSLs in JavaScript
 
Rails Vs CakePHP
Rails Vs CakePHPRails Vs CakePHP
Rails Vs CakePHP
 
Apache Zeppelin and Helium @ApacheCon 2017 may, FL
Apache Zeppelin and Helium  @ApacheCon 2017 may, FLApache Zeppelin and Helium  @ApacheCon 2017 may, FL
Apache Zeppelin and Helium @ApacheCon 2017 may, FL
 
Ruby on Rails (RoR) as a back-end processor for Apex
Ruby on Rails (RoR) as a back-end processor for Apex Ruby on Rails (RoR) as a back-end processor for Apex
Ruby on Rails (RoR) as a back-end processor for Apex
 
Alfresco in few points - Search Tutorial
Alfresco in few points - Search TutorialAlfresco in few points - Search Tutorial
Alfresco in few points - Search Tutorial
 
Website designing company_in_delhi_phpwebdevelopment
Website designing company_in_delhi_phpwebdevelopmentWebsite designing company_in_delhi_phpwebdevelopment
Website designing company_in_delhi_phpwebdevelopment
 
Fast Web Applications with Go
Fast Web Applications with Go Fast Web Applications with Go
Fast Web Applications with Go
 
Tanel Poder - Scripts and Tools short
Tanel Poder - Scripts and Tools shortTanel Poder - Scripts and Tools short
Tanel Poder - Scripts and Tools short
 

Recently uploaded

Indian Escort in Abu DHabi 0508644382 Abu Dhabi Escorts
Indian Escort in Abu DHabi 0508644382 Abu Dhabi EscortsIndian Escort in Abu DHabi 0508644382 Abu Dhabi Escorts
Indian Escort in Abu DHabi 0508644382 Abu Dhabi Escorts
Monica Sydney
 
原版制作美国爱荷华大学毕业证(iowa毕业证书)学位证网上存档可查
原版制作美国爱荷华大学毕业证(iowa毕业证书)学位证网上存档可查原版制作美国爱荷华大学毕业证(iowa毕业证书)学位证网上存档可查
原版制作美国爱荷华大学毕业证(iowa毕业证书)学位证网上存档可查
ydyuyu
 
Russian Escort Abu Dhabi 0503464457 Abu DHabi Escorts
Russian Escort Abu Dhabi 0503464457 Abu DHabi EscortsRussian Escort Abu Dhabi 0503464457 Abu DHabi Escorts
Russian Escort Abu Dhabi 0503464457 Abu DHabi Escorts
Monica Sydney
 
75539-Cyber Security Challenges PPT.pptx
75539-Cyber Security Challenges PPT.pptx75539-Cyber Security Challenges PPT.pptx
75539-Cyber Security Challenges PPT.pptx
Asmae Rabhi
 
pdfcoffee.com_business-ethics-q3m7-pdf-free.pdf
pdfcoffee.com_business-ethics-q3m7-pdf-free.pdfpdfcoffee.com_business-ethics-q3m7-pdf-free.pdf
pdfcoffee.com_business-ethics-q3m7-pdf-free.pdf
JOHNBEBONYAP1
 
Top profile Call Girls In Dindigul [ 7014168258 ] Call Me For Genuine Models ...
Top profile Call Girls In Dindigul [ 7014168258 ] Call Me For Genuine Models ...Top profile Call Girls In Dindigul [ 7014168258 ] Call Me For Genuine Models ...
Top profile Call Girls In Dindigul [ 7014168258 ] Call Me For Genuine Models ...
gajnagarg
 
一比一原版(Flinders毕业证书)弗林德斯大学毕业证原件一模一样
一比一原版(Flinders毕业证书)弗林德斯大学毕业证原件一模一样一比一原版(Flinders毕业证书)弗林德斯大学毕业证原件一模一样
一比一原版(Flinders毕业证书)弗林德斯大学毕业证原件一模一样
ayvbos
 
一比一原版(Curtin毕业证书)科廷大学毕业证原件一模一样
一比一原版(Curtin毕业证书)科廷大学毕业证原件一模一样一比一原版(Curtin毕业证书)科廷大学毕业证原件一模一样
一比一原版(Curtin毕业证书)科廷大学毕业证原件一模一样
ayvbos
 

Recently uploaded (20)

Indian Escort in Abu DHabi 0508644382 Abu Dhabi Escorts
Indian Escort in Abu DHabi 0508644382 Abu Dhabi EscortsIndian Escort in Abu DHabi 0508644382 Abu Dhabi Escorts
Indian Escort in Abu DHabi 0508644382 Abu Dhabi Escorts
 
Nagercoil Escorts Service Girl ^ 9332606886, WhatsApp Anytime Nagercoil
Nagercoil Escorts Service Girl ^ 9332606886, WhatsApp Anytime NagercoilNagercoil Escorts Service Girl ^ 9332606886, WhatsApp Anytime Nagercoil
Nagercoil Escorts Service Girl ^ 9332606886, WhatsApp Anytime Nagercoil
 
20240507 QFM013 Machine Intelligence Reading List April 2024.pdf
20240507 QFM013 Machine Intelligence Reading List April 2024.pdf20240507 QFM013 Machine Intelligence Reading List April 2024.pdf
20240507 QFM013 Machine Intelligence Reading List April 2024.pdf
 
"Boost Your Digital Presence: Partner with a Leading SEO Agency"
"Boost Your Digital Presence: Partner with a Leading SEO Agency""Boost Your Digital Presence: Partner with a Leading SEO Agency"
"Boost Your Digital Presence: Partner with a Leading SEO Agency"
 
原版制作美国爱荷华大学毕业证(iowa毕业证书)学位证网上存档可查
原版制作美国爱荷华大学毕业证(iowa毕业证书)学位证网上存档可查原版制作美国爱荷华大学毕业证(iowa毕业证书)学位证网上存档可查
原版制作美国爱荷华大学毕业证(iowa毕业证书)学位证网上存档可查
 
best call girls in Hyderabad Finest Escorts Service 📞 9352988975 📞 Available ...
best call girls in Hyderabad Finest Escorts Service 📞 9352988975 📞 Available ...best call girls in Hyderabad Finest Escorts Service 📞 9352988975 📞 Available ...
best call girls in Hyderabad Finest Escorts Service 📞 9352988975 📞 Available ...
 
Russian Escort Abu Dhabi 0503464457 Abu DHabi Escorts
Russian Escort Abu Dhabi 0503464457 Abu DHabi EscortsRussian Escort Abu Dhabi 0503464457 Abu DHabi Escorts
Russian Escort Abu Dhabi 0503464457 Abu DHabi Escorts
 
20240510 QFM016 Irresponsible AI Reading List April 2024.pdf
20240510 QFM016 Irresponsible AI Reading List April 2024.pdf20240510 QFM016 Irresponsible AI Reading List April 2024.pdf
20240510 QFM016 Irresponsible AI Reading List April 2024.pdf
 
75539-Cyber Security Challenges PPT.pptx
75539-Cyber Security Challenges PPT.pptx75539-Cyber Security Challenges PPT.pptx
75539-Cyber Security Challenges PPT.pptx
 
pdfcoffee.com_business-ethics-q3m7-pdf-free.pdf
pdfcoffee.com_business-ethics-q3m7-pdf-free.pdfpdfcoffee.com_business-ethics-q3m7-pdf-free.pdf
pdfcoffee.com_business-ethics-q3m7-pdf-free.pdf
 
Vip Firozabad Phone 8250092165 Escorts Service At 6k To 30k Along With Ac Room
Vip Firozabad Phone 8250092165 Escorts Service At 6k To 30k Along With Ac RoomVip Firozabad Phone 8250092165 Escorts Service At 6k To 30k Along With Ac Room
Vip Firozabad Phone 8250092165 Escorts Service At 6k To 30k Along With Ac Room
 
20240509 QFM015 Engineering Leadership Reading List April 2024.pdf
20240509 QFM015 Engineering Leadership Reading List April 2024.pdf20240509 QFM015 Engineering Leadership Reading List April 2024.pdf
20240509 QFM015 Engineering Leadership Reading List April 2024.pdf
 
Microsoft Azure Arc Customer Deck Microsoft
Microsoft Azure Arc Customer Deck MicrosoftMicrosoft Azure Arc Customer Deck Microsoft
Microsoft Azure Arc Customer Deck Microsoft
 
Top profile Call Girls In Dindigul [ 7014168258 ] Call Me For Genuine Models ...
Top profile Call Girls In Dindigul [ 7014168258 ] Call Me For Genuine Models ...Top profile Call Girls In Dindigul [ 7014168258 ] Call Me For Genuine Models ...
Top profile Call Girls In Dindigul [ 7014168258 ] Call Me For Genuine Models ...
 
Best SEO Services Company in Dallas | Best SEO Agency Dallas
Best SEO Services Company in Dallas | Best SEO Agency DallasBest SEO Services Company in Dallas | Best SEO Agency Dallas
Best SEO Services Company in Dallas | Best SEO Agency Dallas
 
20240508 QFM014 Elixir Reading List April 2024.pdf
20240508 QFM014 Elixir Reading List April 2024.pdf20240508 QFM014 Elixir Reading List April 2024.pdf
20240508 QFM014 Elixir Reading List April 2024.pdf
 
一比一原版(Flinders毕业证书)弗林德斯大学毕业证原件一模一样
一比一原版(Flinders毕业证书)弗林德斯大学毕业证原件一模一样一比一原版(Flinders毕业证书)弗林德斯大学毕业证原件一模一样
一比一原版(Flinders毕业证书)弗林德斯大学毕业证原件一模一样
 
Meaning of On page SEO & its process in detail.
Meaning of On page SEO & its process in detail.Meaning of On page SEO & its process in detail.
Meaning of On page SEO & its process in detail.
 
一比一原版(Curtin毕业证书)科廷大学毕业证原件一模一样
一比一原版(Curtin毕业证书)科廷大学毕业证原件一模一样一比一原版(Curtin毕业证书)科廷大学毕业证原件一模一样
一比一原版(Curtin毕业证书)科廷大学毕业证原件一模一样
 
Power point inglese - educazione civica di Nuria Iuzzolino
Power point inglese - educazione civica di Nuria IuzzolinoPower point inglese - educazione civica di Nuria Iuzzolino
Power point inglese - educazione civica di Nuria Iuzzolino
 

Developing OpenResty Framework

  • 1. Developing OpenResty Framework USING DECOUPLED LIBRARIES by Aapo Talvensaari @ OpenResty Con 2015
  • 2. Where Did I Get Here? About 6.500 Kilometers – Almost Nothing Compared to The Length of The Great Wall of China UsingDecoupledLibraries DevelopingOpenRestyFramework 2
  • 3. Who Am I? PROFESSIONALLY WEB PROGRAMMER SINCE 90’S ColdFusion ➠ ASP ➠ PHP ➠ Java ➠ ASP.NET ➠ OpenResty SYSTEM ADMINISTRATOR Linux and Windows Platforms, and Cloud JOB IT Manager at Aalto Capital Oy Founder of Talvensaari Solutions Oy UsingDecoupledLibraries DevelopingOpenRestyFramework 3
  • 4. What to Expect? u  Short History u  Basis for Building Web Applications u  Routing u  Templating u  Validation and Filtering u  Sessions u  Style Sheets u  Libraries u  Kronometrix Analytics In addition to English with Finnish accent, I will show you some of my libraries and how they work together. Checkout also Leaf Corcoran’s Excellent Lapis Framework: http://leafo.net/lapis/ UsingDecoupledLibraries DevelopingOpenRestyFramework 4
  • 5. Why OpenResty? u  I wanted to expand skills, and learn something new u  I wanted to get a rid of gateway interfaces – less moving parts u  A real web server with scripting language support (Basically a new Apache + mod_php with a modern twist, e.g. Web Sockets support) u  I was already using Nginx u  Alternatives that I looked (All the alternatives actually looked really nice, but I felt more comfortable with OpenResty) u  Node.js (I didn’t enjoy JavaScript, and it wasn't using a real web server) u  Python / Ruby (too object oriented, usually behind a gateway interface) u  Go (not a scripting language, no particular web development focus) u  Simplicity, both in development and deployment, and Performance UsingDecoupledLibraries DevelopingOpenRestyFramework 5
  • 6. OpenResty Stack Nginx by Igor Sysoev et al. Web Server OpenResty by Yichun Zhang et al. Web Application Server Lua by Roberto Ierusalimschy et al. Programming Language UsingDecoupledLibraries DevelopingOpenRestyFramework 6
  • 7. Routing LUA-RESTY-ROUTE u  Nginx already does routing! Why oh why? u  Makes web development enjoyable! Works as a glue. u  Nginx configuration can be fairly static u  Nice DSL for Specifying Route Handlers u  Middleware u  Before and After Filters u  Web Sockets Routing u  Pluggable Matchers u  Simple – ngx.re.match + pre-defined :string and :number matchers u  Regex – ngx.re.match u  Match – string.match u  Equals – plain ==, no pattern or string matching 7 local r = require "resty.route"! local route = r.new()! -- Use a Middleware! -- (not needed in this example)! route:use "reqargs" {}! -- Add HTTP GET Route! route:get("/hello", function()! ngx.print "世界你好"! end)! -- Dispatch the Request! route:dispatch()!
  • 8. Templating LUA-RESTY-TEMPLATE u  One of the Top OpenResty Libraries in GitHub u  It's a Compiling Templating Engine u  Just Another Way to Write Lua u  Almost all you can do in Lua, you can do in Template u  Basically it is just plain string concatenation after all (white space and line-feed handling is fine tuned) u  Supports Lua / LuaJIT / OpenResty u  Handwritten Parser Using string.find u  Features u  Layouts u  Blocks u  Include u  Comments u  Verbatim / Raw Blocks u  Automatic Escaping u  Precompilation u  Caching u  Preloading (WIP) UsingDecoupledLibraries DevelopingOpenRestyFramework 8
  • 9. UsingDecoupledLibraries DevelopingOpenRestyFramework 9<!DOCTYPE html>! <html lang="{{lang}}">! <head>! <meta charset="{{charset}}">! <title>{{title}}</title>! {*blocks.styles*}! {#! Commented out, because it causes a problems (see issue #35):! <script src="js/analytics.js"></script>! #}! {-head_scripts-}! <script src="js/app.js"></script>! {-head_scripts-}! </head>! <body>! {-raw-}! Everything inside here is outputted as is, e.g. {{not evaluated}}! {-raw-}! {(include/header.html)}! {[concat({"include/navigation", access.level, ".html"}, "-")]}! <section id="main">! {* view *}! </section>! {(include/footer.html)}! {% for _, script in ipairs(scripts) do %}! <script src="{{script}}"></script>! {% end %}! {*blocks.head_scripts*}! </body>! </html>!
  • 10. UsingDecoupledLibraries DevelopingOpenRestyFramework 10context=(...) or {}! local function include(v, c)! return template.compile(v)(c or context)! end! local ___,blocks,layout={},blocks or {}! ___[#___+1]=[=[! <!DOCTYPE html>! <html lang="]=]! ___[#___+1]=template.escape(lang)! ___[#___+1]=[=[! ">! <head>! <meta charset="]=]! ___[#___+1]=template.escape(charset)! ___[#___+1]=[=[! ">! <title>]=]! ___[#___+1]=template.escape(title)! ___[#___+1]=[=[! </title>! ]=]! ___[#___+1]=template.output(blocks.styles)! blocks["head_scripts"]=include[=[! <script src="js/app.js"></script>! ]=]! ___[#___+1]=[=[! </head>! <body>! ]=]! ___[#___+1]=[=[! Everything inside here is outputted as is, eg. {{not evaluated}}! ]=]!
  • 11. UsingDecoupledLibraries DevelopingOpenRestyFramework 11___[#___+1]=include([=[include/header.html]=])! ___[#___+1]=[=[! ! ]=]! ___[#___+1]=include(concat({"include/navigation", access.level, ".html"}, "-"))! ___[#___+1]=[=[! ! <section id="main">! ]=]! ___[#___+1]=template.output( view )! ___[#___+1]=[=[! ! </section>! ]=]! ___[#___+1]=include([=[include/footer.html]=])! ___[#___+1]=[=[! ! ]=]! for _, script in ipairs(scripts) do ! ___[#___+1]=[=[! <script src="]=]! ___[#___+1]=template.escape(script)! ___[#___+1]=[=[! "></script>! ]=]! end ! ___[#___+1]=template.output(blocks.head_scripts)! ___[#___+1]=[=[! ! </body>! </html>]=]! return layout and include(layout,setmetatable({view=template.concat(___),blocks=blocks},{__index=context})) or template.concat(___)!
  • 12. Validation and Filtering LUA-RESTY-VALIDATION u  Both Validation and Filtering u  Validation and Filter Chaining u  Reusable Validators (Define Once, Use in Many Places) u  Automatic Conditional Validators u  Group Validators and Filters u  Easy to Extend u  Report Errors in Frontend, JSON Friendly u  Send Data to Backend u  Stop Validators (e.g. optional) Example Here UsingDecoupledLibraries DevelopingOpenRestyFramework 12 local v = require "resty.validation”! local validate = {! nick = v.string.trim:minlen(2),! email = v.string.trim.email,! password = v.string.trim:minlen(8)! }! -- First we create single validators! -- for each form field! local register = v.new{! nick = validate.nick,! email = validate.email,! email2 = validate.email,! password = validate.password,! password2 = validate.password! }! -- Next we create group validators for email! -- and password:! register:compare "email == email2"! register:compare "password == password2”! -- And finally we return from this forms module! return {! register = register! }!
  • 13. Sessions LUA-RESTY-SESSION u  Secure Defaults u  Configurable from Nginx Config or Lua Code u  Plugins for u  Client and Server Storages (Cookie, Shared Memory, Memcache, Redis) u  Ciphers (AES, HMAC) u  Encoders (Base64 and Base16) u  Serializers (JSON) u  Identifiers (WIP, Random, JWT, UUID) UsingDecoupledLibraries DevelopingOpenRestyFramework 13 local session = require "resty.session"! ! -- Construct and start a session! -- (new, and open also available)! local s = session.start()! ! -- Store some data to session! s.data.name = "OpenResty Fan"! ! -- Save a session! s:save()! ! -- Destroy a session! s:destroy()! ! -- Regenerate a session! -- (e.g. when security context changes)! s:regenerate()!
  • 14. Style Sheets LUA-RESTY-SASS u  Syntactically Awesome Style Sheets u  LuaJIT FFI Bindings to libSass u  Automatic Online Compilers for OpenResty u  Caching to Normal CSS Files Once Compiled u  Save-And-Refresh Development u  A Good and a Bad Thing: Errors in Sass File prevents compiling to CSS Errors are catched earlier, but works differently than with plain CSS u  Deploy Sass Files Directly to The Production, No Build Needed u  More about Sass at http://sass-lang.com/ UsingDecoupledLibraries DevelopingOpenRestyFramework 14
  • 15. Demo HOW THIS ALL WORKS TOGETHER? Source Code Available at github.com/bungle/iresty UsingDecoupledLibraries DevelopingOpenRestyFramework 15
  • 16. Libraries COMMON NEEDS IN WEB DEVELOPMENT u  Excel ➠ LUA-RESTY-LIBXL u  Cryptography ➠ LUA-RESTY-NETTLE u  Universally Unique Identifiers ➠ LUA-RESTY-UUID u  Audio Metadata ➠ LUA-RESTY-TAGLIB u  Markdown ➠ LUA-RESTY-HOEDOWN u  File Information ➠ LUA-RESTY-FILEINFO u  Translations ➠ LUA-RESTY-GETTEXT u  Unicode ➠ LUA-RESTY-UNISTRING u  JSON Pretty Formatting ➠ LUA-RESTY-PRETTYCJSON CHECK MY GITHUB ACCOUNT FOR MORE UsingDecoupledLibraries DevelopingOpenRestyFramework 16
  • 17. Excel LUA-RESTY-LIBXL LuaJIT FFI to a LibXL Library – a Library that Can Read and Write Excel Files u  LibXL u  Proprietary Library (from Slovakia) u  Supports XLS and XLSX Formats u  Pricing Starts from $ 199.00 u  Source Code for The Library is Available for $ 2499.00 u  High Performance u  No Extra Dependencies u  lua-resty-libxl u  Nice Lua API (almost full featured*) * some Sheet APIs are still not finished UsingDecoupledLibraries DevelopingOpenRestyFramework 17 local excel = require "resty.libxl.book"! local book = excel.new()! book:load "demo.xlsx"! local sheet = book.sheets[1]! local value = sheet:read(1, 1)! sheet:write(1, 1, value + math.pi)! book:save "output.xlsx"! u  Features u  Pictures u  Formats u  Fonts and Colors u  Hyperlinks u  Equations
  • 18. Cryptography LUA-RESTY-NETTLE LuaJIT FFI Bindings to GNU Nettle Library – a Low Level Cryptography Library u  Hash Functions u  SHA1, SHA2, SHA3 u  MD2, MD4, MD5 u  RIPEMD160, GOSTHASH94 u  Keyed Hash Functions u  HMAC u  SHA1, SHA2 u  MD5 u  RIPEMD160 u  UMAC u  Poly1305 u  Key Derivation Functions u  PBKDF2 u  SHA1 u  SHA2 u  Randomness u  Yarrow u  Knuth’s Lagged Fibonacci u  ASCII Encoding u  Base16, Base64, URL-safe Base64 UsingDecoupledLibraries DevelopingOpenRestyFramework 18
  • 19. Cryptography LUA-RESTY-NETTLE u  Cipher Functions u  AES u  ARCFOUR u  ARCTWO u  BLOWFISH u  Camellia u  CAST128 u  ChaCha u  DES u  DES3 u  Salsa20 u  SERPENT u  TWOFISH u  Cipher Modes u  ECB u  CBC u  CTR u  AEAD Authenticated Encryption with Associated Data u  EAX for AES u  GCM for AES / Camellia u  CCM for AES u  ChaCha-Poly1305 u  Public Key Algorithms Bindings for these are still work in progress, L. UsingDecoupledLibraries DevelopingOpenRestyFramework 19
  • 20. Kronometrix A REAL-TIME ANALYTICS APPLIANCE DESIGNED FOR TIME SERIES DATA ANALYSIS u  Build on OpenResty and Lua u  Using Redis as a Backend Store u  1,000 Recording Devices per Appliance in Real-Time u  Data Recorders u  Aviation Meteorology u  Computer Performance Metrics u  Windows u  Linux u  BSD u  Solaris u  Climatology u  Architecture of The Appliance u  Authentication Layer u  OpenResty Server u  Redis Authentication Database u  Messaging Layer u  OpenResty Server u  Kernel Layer u  OpenResty Server u  Redis Statistics Database u  Aggregate Layer u  OpenResty Server u  Redis Aggregates Database UsingDecoupledLibraries DevelopngOpenRestyFramework 20
  • 21. OpenResty in a Future IT'S BRIGHT FOR SURE What I Will Be Doing? u  Adding Documentation u  Adding Tests u  Maintaining Existing Libraries u  More Libraries, and Bindings u  lua-resty-password / -auth u  lua-resty-chromelogger u  lua-resty-cloudflare u  Start Contributing on ngx_lua u  A Modern Open Source CMS What I Would Like to See? u  Uniting Chinese and Western Communities u  Package Management u  File APIs (Using Nginx File APIs?) u  Official Packages for Platforms u  LuaJIT Enhancements u  Less NYIs u  String Buffer u  Lua 5.2 / 5.3 Support / Uncertainty u  Optimizing for LuaJIT is Hard UsingDecoupledLibraries DevelopingOpenRestyFramework 21