February 20, 2016
Lua is a very fast and powerful scripting language that can be easily embeddable. It has been shining in industries like game development, for example. Lua is also an excellent tool as a general purpose language and can be used to develop robust applications. Its use in web developments, however, despite its great potential and incredible benchmarks, needs to be more widespread. This talk will mention the current state of Lua in web development, show some benchmarks, compare existing tools and teach developers how to get started with Sailor, an MVC web framework written in Lua.
15. @etiene_dBulgaria Web Summit 2016
"Since Lua itself is so
simple, it tends to
encourage you to solve
problems simply."
Ragnar Svensson - Lead Developer at King
(Lua Workshop Oct 15 2015)
20. @etiene_dBulgaria Web Summit 2016
Better Reasons
• It looks cool
(I heard you could make games with it)
• It’s made in my home country
(In my university to be more precise)
21. @etiene_dBulgaria Web Summit 2016
• It looks cool
(I heard you could make games with it)
• It’s made in my home country
(In my university to be more precise)
• It’s easy to learn
Better Reasons
22. @etiene_dBulgaria Web Summit 2016
-- Cipher module
--[[ Based on algorithms/caesar_cipher.lua
by Roland Yonaba ]]
local cipher = {}
local function ascii_base(s)
return s:lower() == s and ('a'):byte() or ('A'):byte()
end
function cipher.caesar( str, key )
return str:gsub('%a', function(s)
local base = ascii_base(s)
return string.char(((s:byte() - base + key) % 26) + base)
end)
end
return cipher
One slide crash course: cipher module
23. @etiene_dBulgaria Web Summit 2016
-- Cipher module
--[[ Based on algorithms/caesar_cipher.lua
by Roland Yonaba ]]
local cipher = {}
local function ascii_base(s)
return s:lower() == s and ('a'):byte() or ('A'):byte()
end
function cipher.caesar( str, key )
return str:gsub('%a', function(s)
local base = ascii_base(s)
return string.char(((s:byte() - base + key) % 26) + base)
end)
end
return cipher
One slide crash course: cipher module
24. @etiene_dBulgaria Web Summit 2016
-- Cipher module
--[[ Based on algorithms/caesar_cipher.lua
by Roland Yonaba ]]
local cipher = {}
local function ascii_base(s)
return s:lower() == s and ('a'):byte() or ('A'):byte()
end
function cipher.caesar( str, key )
return str:gsub('%a', function(s)
local base = ascii_base(s)
return string.char(((s:byte() - base + key) % 26) + base)
end)
end
return cipher
One slide crash course: cipher module
private
public
25. @etiene_dBulgaria Web Summit 2016
-- Cipher module
--[[ Based on algorithms/caesar_cipher.lua
by Roland Yonaba ]]
local cipher = {}
local function ascii_base(s)
return s:lower() == s and ('a'):byte() or ('A'):byte()
end
function cipher.caesar( str, key )
return str:gsub('%a', function(s)
local base = ascii_base(s)
return string.char(((s:byte() - base + key) % 26) + base)
end)
end
return cipher
One slide crash course: cipher module
26. @etiene_dBulgaria Web Summit 2016
-- Cipher module
--[[ Based on algorithms/caesar_cipher.lua
by Roland Yonaba ]]
local cipher = {}
local function ascii_base(s)
return s:lower() == s and ('a'):byte() or ('A'):byte()
end
function cipher.caesar( str, key )
return str:gsub('%a', function(s)
local base = ascii_base(s)
return string.char(((s:byte() - base + key) % 26) + base)
end)
end
return cipher
One slide crash course: cipher module
27. @etiene_dBulgaria Web Summit 2016
-- Cipher module
--[[ Based on algorithms/caesar_cipher.lua
by Roland Yonaba ]]
local cipher = {}
local function ascii_base(s)
return s:lower() == s and ('a'):byte() or ('A'):byte()
end
function cipher.caesar( str, key )
return str:gsub('%a', function(s)
local base = ascii_base(s)
return string.char(((s:byte() - base + key) % 26) + base)
end)
end
return cipher
One slide crash course: cipher module
28. @etiene_dBulgaria Web Summit 2016
-- Cipher module
--[[ Based on algorithms/caesar_cipher.lua
by Roland Yonaba ]]
local cipher = {}
local function ascii_base(s)
return s:lower() == s and ('a'):byte() or ('A'):byte()
end
function cipher.caesar( str, key )
return str:gsub('%a', function(s)
local base = ascii_base(s)
return string.char(((s:byte() - base + key) % 26) + base)
end)
end
return cipher
One slide crash course: cipher module
31. @etiene_dBulgaria Web Summit 2016
“ I have myself developed Web sites with pure C++, Java, C#, PHP, and Python.
The easiest way to go was definitely Python. If the libraries existed, Lua would be
not quite as easy to use as Python, but probably quite a bit more efficient; I think it
would become my first choice... if the libraries existed.” Michael Gogins
“ Recently there was some discussion about mod_lua on the Apache developers
mailing list. I mentioned there that I feel Lua could replace PHP as the number one
web scripting language if mod_lua were stable (i.e. not still in beta) and it were
implemented well (not making some of PHP's mistakes such as putting everything
in the global scope with no consistent naming or parameter schemes). I've wanted
to use Lua for all the things I currently use PHP for ever since I discovered it.” Rena
45. @etiene_dBulgaria Web Summit 2016
Frameworks
Orbit (2007)
Least known
No significant updates since 2010
MVC
Luvit (2011)
Most popular
Intense development
node.js port 2-4x faster
Needs a better documentation
46. @etiene_dBulgaria Web Summit 2016
Frameworks
Lapis (2012)
Intense development
Moonscript and Lua
Very well documented
Templater
OpenResty only
Not MVC
47. @etiene_dBulgaria Web Summit 2016
Frameworks
Lapis (2012)
Intense development
Moonscript and Lua
Very well documented
Templater
OpenResty only
Not MVC
Others
Very early development, complicated, abandoned,
poorly documented, license issues or I never heard
about it...
lua.space/webdev/the-best-lua-web-frameworks
52. @etiene_dBulgaria Web Summit 2016
What exactly is
Sailor?
• It’s an MVC web framework
• Completely written in Lua
• Compatible with Apache (mod_lua), Nginx (OpenResty),
Xavante, Mongoose, Lighttpd and Lwan
• Compatible with Linux, Windows and Mac
• Compatible with different databases
• MIT License
• Pre alpha v0.5 (Pluto)
• Planning next release to be a 1.0!
54. @etiene_dBulgaria Web Summit 2016
What (else) is cool about
Sailor?
• Routing and friendly URLs
• Session, cookies, include, redirect…
• Lua Pages parsing
• Mail sending
• Simple Object Relational-Mapping
• Validation (valua)
• Basic login and authentication modules
• Form generation
• Themes (Bootstrap integration out of the box)
• App generator (Linux and Mac only)
• Model and CRUD generator
• Automated tests
55. @etiene_dBulgaria Web Summit 2016
• Routing and friendly URLs
• Session, cookies, include, redirect…
• Lua Pages parsing
• Mail sending
• Simple Object Relational-Mapping
• Validation (valua)
• Basic login and authentication modules
• Form generation
• Themes (Bootstrap integration out of the box)
• App generator (Linux and Mac only)
• Model and CRUD generator
• Automated tests
• Lua at client
What (else) is cool about
Sailor?
56. @etiene_dBulgaria Web Summit 2016
Not so great things
• It’s still in early development
• Things are changing fast
• It lacks features
• Documentation
57. @etiene_dBulgaria Web Summit 2016
How to get Sailor!
$ luarocks install sailor
$ sailor create ‘My App’ /var/www
$ cd /var/www/my_app
$ lua start-server.lua
61. @etiene_dBulgaria Web Summit 2016
Example!
-- /controllers/site.lua
local site = {}
function site.index(page)
local msg = “Hello World”
page:render(‘index’, { msg = msg } )
end
function site.notindex(page)
page.theme = nil
page:write(“I’m different!”)
end
return site
62. @etiene_dBulgaria Web Summit 2016
<!-- /views/site/index.lp —>
<p>
A message from the server:
<?lua page:print(msg) ?>
<br/>
The message again:
<%= msg %> <!-- syntactic sugar: same thing as above —>
</p>
Example!
64. @etiene_dBulgaria Web Summit 2016
<?lua@server -- Code here runs on the server ?>
<?lua -- Same as above ?>
<?lua@client -- Runs at the client ?>
<?lua@both -- Runs at the server and the client ?>
<?lua@both
another_msg = “Another message”
?>
<?lua page:print(another_msg) ?>
<?lua@client
window:alert(another_msg)
?>
Example!