SlideShare a Scribd company logo
1 of 23
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 handlingis 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
UsingDecoupledLibraries
DevelopingOpenRestyFramework
10<!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
11context=(...) 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
12___[#___+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
13
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
14
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
15
Demo
HOW THIS ALL WORKS TOGETHER?
Source Code Available at github.com/bungle/iresty
UsingDecoupledLibraries
DevelopingOpenRestyFramework
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 OUT MY GITHUB ACCOUNT FOR MORE
UsingDecoupledLibraries
DevelopingOpenRestyFramework
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
18
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
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
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
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
22
Questions?
aapo.talvensaari@gmail.com bungle@github
UsingDecoupledLibraries
DevelopingOpenRestyFramework
23

More Related Content

What's hot

New EEA Plone Add-ons
New EEA Plone Add-onsNew EEA Plone Add-ons
New EEA Plone Add-onsAlin Voinea
 
Server Side Swift
Server Side SwiftServer Side Swift
Server Side SwiftJens Ravens
 
[Community Open Camp] 給 PHP 開發者的 VS Code 指南
[Community Open Camp] 給 PHP 開發者的 VS Code 指南[Community Open Camp] 給 PHP 開發者的 VS Code 指南
[Community Open Camp] 給 PHP 開發者的 VS Code 指南Shengyou Fan
 
eSynergy Andy Hawkins - Enabling DevOps through next generation configuration...
eSynergy Andy Hawkins - Enabling DevOps through next generation configuration...eSynergy Andy Hawkins - Enabling DevOps through next generation configuration...
eSynergy Andy Hawkins - Enabling DevOps through next generation configuration...PatrickCrompton
 
Painless Data Storage with MongoDB & Go
Painless Data Storage with MongoDB & Go Painless Data Storage with MongoDB & Go
Painless Data Storage with MongoDB & Go Steven Francia
 
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 Lispmasayukitakagi
 
Plack perl superglue for web frameworks and servers
Plack perl superglue for web frameworks and serversPlack perl superglue for web frameworks and servers
Plack perl superglue for web frameworks and serversTatsuhiko Miyagawa
 
Rails Girls: Programming, Web Applications and Ruby on Rails
Rails Girls: Programming, Web Applications and Ruby on RailsRails Girls: Programming, Web Applications and Ruby on Rails
Rails Girls: Programming, Web Applications and Ruby on RailsDonSchado
 
Ruby MVC from scratch with Rack
Ruby MVC from scratch with RackRuby MVC from scratch with Rack
Ruby MVC from scratch with RackDonSchado
 
JSON REST API for WordPress
JSON REST API for WordPressJSON REST API for WordPress
JSON REST API for WordPressTaylor Lovett
 
Strangers In The Night: Ruby, Rack y Sinatra - Herramientas potentes para con...
Strangers In The Night: Ruby, Rack y Sinatra - Herramientas potentes para con...Strangers In The Night: Ruby, Rack y Sinatra - Herramientas potentes para con...
Strangers In The Night: Ruby, Rack y Sinatra - Herramientas potentes para con...Alberto Perdomo
 

What's hot (20)

Os Pruett
Os PruettOs Pruett
Os Pruett
 
Os Furlong
Os FurlongOs Furlong
Os Furlong
 
ElasticSearch
ElasticSearchElasticSearch
ElasticSearch
 
New EEA Plone Add-ons
New EEA Plone Add-onsNew EEA Plone Add-ons
New EEA Plone Add-ons
 
Server Side Swift
Server Side SwiftServer Side Swift
Server Side Swift
 
[Community Open Camp] 給 PHP 開發者的 VS Code 指南
[Community Open Camp] 給 PHP 開發者的 VS Code 指南[Community Open Camp] 給 PHP 開發者的 VS Code 指南
[Community Open Camp] 給 PHP 開發者的 VS Code 指南
 
eSynergy Andy Hawkins - Enabling DevOps through next generation configuration...
eSynergy Andy Hawkins - Enabling DevOps through next generation configuration...eSynergy Andy Hawkins - Enabling DevOps through next generation configuration...
eSynergy Andy Hawkins - Enabling DevOps through next generation configuration...
 
Web::Scraper
Web::ScraperWeb::Scraper
Web::Scraper
 
Painless Data Storage with MongoDB & Go
Painless Data Storage with MongoDB & Go Painless Data Storage with MongoDB & Go
Painless Data Storage with MongoDB & Go
 
Analyse Yourself
Analyse YourselfAnalyse Yourself
Analyse Yourself
 
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
 
Plack at YAPC::NA 2010
Plack at YAPC::NA 2010Plack at YAPC::NA 2010
Plack at YAPC::NA 2010
 
Ugo Cei Presentation
Ugo Cei PresentationUgo Cei Presentation
Ugo Cei Presentation
 
Plack perl superglue for web frameworks and servers
Plack perl superglue for web frameworks and serversPlack perl superglue for web frameworks and servers
Plack perl superglue for web frameworks and servers
 
Front-end tools
Front-end toolsFront-end tools
Front-end tools
 
Rails Girls: Programming, Web Applications and Ruby on Rails
Rails Girls: Programming, Web Applications and Ruby on RailsRails Girls: Programming, Web Applications and Ruby on Rails
Rails Girls: Programming, Web Applications and Ruby on Rails
 
Intro to PSGI and Plack
Intro to PSGI and PlackIntro to PSGI and Plack
Intro to PSGI and Plack
 
Ruby MVC from scratch with Rack
Ruby MVC from scratch with RackRuby MVC from scratch with Rack
Ruby MVC from scratch with Rack
 
JSON REST API for WordPress
JSON REST API for WordPressJSON REST API for WordPress
JSON REST API for WordPress
 
Strangers In The Night: Ruby, Rack y Sinatra - Herramientas potentes para con...
Strangers In The Night: Ruby, Rack y Sinatra - Herramientas potentes para con...Strangers In The Night: Ruby, Rack y Sinatra - Herramientas potentes para con...
Strangers In The Night: Ruby, Rack y Sinatra - Herramientas potentes para con...
 

Similar to Developing OpenResty Framework

Woo: Writing a fast web server @ ELS2015
Woo: Writing a fast web server @ ELS2015Woo: Writing a fast web server @ ELS2015
Woo: Writing a fast web server @ ELS2015fukamachi
 
NoSQL Matters 2013 - Introduction to Map Reduce with Couchbase 2.0
NoSQL Matters 2013 - Introduction to Map Reduce with Couchbase 2.0NoSQL Matters 2013 - Introduction to Map Reduce with Couchbase 2.0
NoSQL Matters 2013 - Introduction to Map Reduce with Couchbase 2.0Tugdual Grall
 
Building Server Applications Using ObjectiveC And GNUstep
Building Server Applications Using ObjectiveC And GNUstepBuilding Server Applications Using ObjectiveC And GNUstep
Building Server Applications Using ObjectiveC And GNUstepguest9efd1a1
 
Building Server Applications Using Objective C And Gn Ustep
Building Server Applications Using Objective C And Gn UstepBuilding Server Applications Using Objective C And Gn Ustep
Building Server Applications Using Objective C And Gn Ustepwangii
 
Introduction to Lua Luajit Openresty Luvit
Introduction to Lua Luajit Openresty LuvitIntroduction to Lua Luajit Openresty Luvit
Introduction to Lua Luajit Openresty LuvitLionel Duboeuf
 
HTTP Plugin for MySQL!
HTTP Plugin for MySQL!HTTP Plugin for MySQL!
HTTP Plugin for MySQL!Ulf Wendel
 
ElasticSearch for .NET Developers
ElasticSearch for .NET DevelopersElasticSearch for .NET Developers
ElasticSearch for .NET DevelopersBen van Mol
 
Apache Lucene: Searching the Web and Everything Else (Jazoon07)
Apache Lucene: Searching the Web and Everything Else (Jazoon07)Apache Lucene: Searching the Web and Everything Else (Jazoon07)
Apache Lucene: Searching the Web and Everything Else (Jazoon07)dnaber
 
Develop realtime web with Scala and Xitrum
Develop realtime web with Scala and XitrumDevelop realtime web with Scala and Xitrum
Develop realtime web with Scala and XitrumNgoc Dao
 
Language-agnostic data analysis workflows and reproducible research
Language-agnostic data analysis workflows and reproducible researchLanguage-agnostic data analysis workflows and reproducible research
Language-agnostic data analysis workflows and reproducible researchAndrew Lowe
 
Experiences building a distributed shared log on RADOS - Noah Watkins
Experiences building a distributed shared log on RADOS - Noah WatkinsExperiences building a distributed shared log on RADOS - Noah Watkins
Experiences building a distributed shared log on RADOS - Noah WatkinsCeph Community
 
Writing an Interactive Interface for SQL on Flink
Writing an Interactive Interface for SQL on FlinkWriting an Interactive Interface for SQL on Flink
Writing an Interactive Interface for SQL on FlinkEventador
 
LF_OVS_17_LXC Linux Containers over Open vSwitch
LF_OVS_17_LXC Linux Containers over Open vSwitchLF_OVS_17_LXC Linux Containers over Open vSwitch
LF_OVS_17_LXC Linux Containers over Open vSwitchLF_OpenvSwitch
 
Socket programming with php
Socket programming with phpSocket programming with php
Socket programming with phpElizabeth Smith
 

Similar to Developing OpenResty Framework (20)

Woo: Writing a fast web server @ ELS2015
Woo: Writing a fast web server @ ELS2015Woo: Writing a fast web server @ ELS2015
Woo: Writing a fast web server @ ELS2015
 
Not only SQL
Not only SQL Not only SQL
Not only SQL
 
NoSQL Matters 2013 - Introduction to Map Reduce with Couchbase 2.0
NoSQL Matters 2013 - Introduction to Map Reduce with Couchbase 2.0NoSQL Matters 2013 - Introduction to Map Reduce with Couchbase 2.0
NoSQL Matters 2013 - Introduction to Map Reduce with Couchbase 2.0
 
Building Server Applications Using ObjectiveC And GNUstep
Building Server Applications Using ObjectiveC And GNUstepBuilding Server Applications Using ObjectiveC And GNUstep
Building Server Applications Using ObjectiveC And GNUstep
 
Building Server Applications Using Objective C And Gn Ustep
Building Server Applications Using Objective C And Gn UstepBuilding Server Applications Using Objective C And Gn Ustep
Building Server Applications Using Objective C And Gn Ustep
 
Introduction to Lua Luajit Openresty Luvit
Introduction to Lua Luajit Openresty LuvitIntroduction to Lua Luajit Openresty Luvit
Introduction to Lua Luajit Openresty Luvit
 
Laravel 4 presentation
Laravel 4 presentationLaravel 4 presentation
Laravel 4 presentation
 
HTTP Plugin for MySQL!
HTTP Plugin for MySQL!HTTP Plugin for MySQL!
HTTP Plugin for MySQL!
 
ElasticSearch for .NET Developers
ElasticSearch for .NET DevelopersElasticSearch for .NET Developers
ElasticSearch for .NET Developers
 
Apache Lucene Searching The Web
Apache Lucene Searching The WebApache Lucene Searching The Web
Apache Lucene Searching The Web
 
Apache Lucene: Searching the Web and Everything Else (Jazoon07)
Apache Lucene: Searching the Web and Everything Else (Jazoon07)Apache Lucene: Searching the Web and Everything Else (Jazoon07)
Apache Lucene: Searching the Web and Everything Else (Jazoon07)
 
Develop realtime web with Scala and Xitrum
Develop realtime web with Scala and XitrumDevelop realtime web with Scala and Xitrum
Develop realtime web with Scala and Xitrum
 
Language-agnostic data analysis workflows and reproducible research
Language-agnostic data analysis workflows and reproducible researchLanguage-agnostic data analysis workflows and reproducible research
Language-agnostic data analysis workflows and reproducible research
 
SFScon 2020 - Peter Hopfgartner - Open Data de luxe
SFScon 2020 - Peter Hopfgartner - Open Data de luxeSFScon 2020 - Peter Hopfgartner - Open Data de luxe
SFScon 2020 - Peter Hopfgartner - Open Data de luxe
 
Logstash
LogstashLogstash
Logstash
 
Experiences building a distributed shared log on RADOS - Noah Watkins
Experiences building a distributed shared log on RADOS - Noah WatkinsExperiences building a distributed shared log on RADOS - Noah Watkins
Experiences building a distributed shared log on RADOS - Noah Watkins
 
Microservices in Clojure
Microservices in ClojureMicroservices in Clojure
Microservices in Clojure
 
Writing an Interactive Interface for SQL on Flink
Writing an Interactive Interface for SQL on FlinkWriting an Interactive Interface for SQL on Flink
Writing an Interactive Interface for SQL on Flink
 
LF_OVS_17_LXC Linux Containers over Open vSwitch
LF_OVS_17_LXC Linux Containers over Open vSwitchLF_OVS_17_LXC Linux Containers over Open vSwitch
LF_OVS_17_LXC Linux Containers over Open vSwitch
 
Socket programming with php
Socket programming with phpSocket programming with php
Socket programming with php
 

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 handlingis 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
  • 10. UsingDecoupledLibraries DevelopingOpenRestyFramework 10<!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>
  • 11. UsingDecoupledLibraries DevelopingOpenRestyFramework 11context=(...) 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}} ]=]
  • 12. UsingDecoupledLibraries DevelopingOpenRestyFramework 12___[#___+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(___)
  • 13. 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 13 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 }
  • 14. 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 14 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()
  • 15. 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 15
  • 16. Demo HOW THIS ALL WORKS TOGETHER? Source Code Available at github.com/bungle/iresty UsingDecoupledLibraries DevelopingOpenRestyFramework 16
  • 17. 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 OUT MY GITHUB ACCOUNT FOR MORE UsingDecoupledLibraries DevelopingOpenRestyFramework 17
  • 18. 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 18 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
  • 19. 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 19
  • 20. 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 20
  • 21. 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 21
  • 22. 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 22

Editor's Notes

  1. Basically I’m a Web Programmer. I started with ColdFusion and Microsoft’s original ASP in 90’s. Then it was mostly PHP from version 3 to 4.