Top Banner
Web development with Lua Programming Language Introducing Sailor , an MVC web framework in Lua Etiene Dalcol @etiene_d
60

Etiene Dalcol - Web development with Lua Programming Language - code.talks 2015

Jan 25, 2017

Download

Internet

AboutYouGmbH
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Etiene Dalcol - Web development with Lua Programming Language - code.talks 2015

Web development with Lua Programming Language

Introducing Sailor, an MVC web framework in Lua

Etiene Dalcol @etiene_d

Page 2: Etiene Dalcol - Web development with Lua Programming Language - code.talks 2015

@etiene_dcode.talks 2015

@etiene_d

Page 3: Etiene Dalcol - Web development with Lua Programming Language - code.talks 2015

@etiene_dcode.talks 2015

Sailor!sailorproject.org

Page 4: Etiene Dalcol - Web development with Lua Programming Language - code.talks 2015

@etiene_dcode.talks 2015

Lua Ladieslualadies.org

Page 5: Etiene Dalcol - Web development with Lua Programming Language - code.talks 2015

@etiene_dcode.talks 2015

Page 6: Etiene Dalcol - Web development with Lua Programming Language - code.talks 2015

@etiene_dcode.talks 2015

Advantages

Powerful. Simple. Fast.

Page 7: Etiene Dalcol - Web development with Lua Programming Language - code.talks 2015

@etiene_dcode.talks 2015

Page 8: Etiene Dalcol - Web development with Lua Programming Language - code.talks 2015

@etiene_dcode.talks 2015

Better Reasons

• It looks cool (I heard you could make games with it)

Page 9: Etiene Dalcol - Web development with Lua Programming Language - code.talks 2015

@etiene_dcode.talks 2015

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)

Page 10: Etiene Dalcol - Web development with Lua Programming Language - code.talks 2015

@etiene_dcode.talks 2015

• 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

Page 11: Etiene Dalcol - Web development with Lua Programming Language - code.talks 2015

@etiene_dcode.talks 2015

-- 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

Page 12: Etiene Dalcol - Web development with Lua Programming Language - code.talks 2015

@etiene_dcode.talks 2015

-- 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

Page 13: Etiene Dalcol - Web development with Lua Programming Language - code.talks 2015

@etiene_dcode.talks 2015

-- 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

Page 14: Etiene Dalcol - Web development with Lua Programming Language - code.talks 2015

@etiene_dcode.talks 2015

-- 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

Page 15: Etiene Dalcol - Web development with Lua Programming Language - code.talks 2015

@etiene_dcode.talks 2015

-- 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

Page 16: Etiene Dalcol - Web development with Lua Programming Language - code.talks 2015

@etiene_dcode.talks 2015

-- 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

Page 17: Etiene Dalcol - Web development with Lua Programming Language - code.talks 2015

@etiene_dcode.talks 2015

-- 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

Page 18: Etiene Dalcol - Web development with Lua Programming Language - code.talks 2015

@etiene_dcode.talks 2015

?

?

? ?

??

Page 19: Etiene Dalcol - Web development with Lua Programming Language - code.talks 2015

@etiene_dcode.talks 2015

Lua on the web

• Early stage

• cgilua ~ 1995

• Kepler Project ~ 2003

Page 20: Etiene Dalcol - Web development with Lua Programming Language - code.talks 2015

@etiene_dcode.talks 2015

“ 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

Page 21: Etiene Dalcol - Web development with Lua Programming Language - code.talks 2015

@etiene_dcode.talks 2015

Page 22: Etiene Dalcol - Web development with Lua Programming Language - code.talks 2015

@etiene_dcode.talks 2015

Page 23: Etiene Dalcol - Web development with Lua Programming Language - code.talks 2015

@etiene_dcode.talks 2015

Page 24: Etiene Dalcol - Web development with Lua Programming Language - code.talks 2015

@etiene_dcode.talks 2015

Page 25: Etiene Dalcol - Web development with Lua Programming Language - code.talks 2015

@etiene_dcode.talks 2015

Page 26: Etiene Dalcol - Web development with Lua Programming Language - code.talks 2015

@etiene_dcode.talks 2015

Why?

Page 27: Etiene Dalcol - Web development with Lua Programming Language - code.talks 2015

@etiene_dcode.talks 2015

Page 28: Etiene Dalcol - Web development with Lua Programming Language - code.talks 2015

@etiene_dcode.talks 2015

Servers

• Apache: mod_lua

• Nginx: OpenResty

Page 29: Etiene Dalcol - Web development with Lua Programming Language - code.talks 2015

@etiene_dcode.talks 2015

Servers

• Apache: mod_lua

• Nginx: OpenResty

Page 30: Etiene Dalcol - Web development with Lua Programming Language - code.talks 2015

@etiene_dcode.talks 2015

Servers

• Apache: mod_lua

• Nginx: OpenResty

• Xavante

• Others: Lighttpd, Lwan, Pegasus, Mongoose

Page 31: Etiene Dalcol - Web development with Lua Programming Language - code.talks 2015

@etiene_dcode.talks 2015

Frameworks

Orbit (2007) Least known No significant updates since 2010 MVC

Page 32: Etiene Dalcol - Web development with Lua Programming Language - code.talks 2015

@etiene_dcode.talks 2015

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

Page 33: Etiene Dalcol - Web development with Lua Programming Language - code.talks 2015

@etiene_dcode.talks 2015

Frameworks

Lapis (2012) Intense development Moonscript and Lua Very well documented Templater OpenResty only Not MVC

Page 34: Etiene Dalcol - Web development with Lua Programming Language - code.talks 2015

@etiene_dcode.talks 2015

Frameworks

Lapis (2012) Intense development Moonscript and Lua Very well documented Templater OpenResty only Not MVC

Others Complicated, abandoned, poorly documented, license issues or I never heard about it...

Page 35: Etiene Dalcol - Web development with Lua Programming Language - code.talks 2015

@etiene_dcode.talks 2015

Sailor!

Page 36: Etiene Dalcol - Web development with Lua Programming Language - code.talks 2015

@etiene_dcode.talks 2015

Sailor!

Page 37: Etiene Dalcol - Web development with Lua Programming Language - code.talks 2015

@etiene_dcode.talks 2015

Sailor!

0.1 (Venus)

Page 38: Etiene Dalcol - Web development with Lua Programming Language - code.talks 2015

@etiene_dcode.talks 2015

Sailor!

0.1 (Venus)

0.2 (Mars)

Page 39: Etiene Dalcol - Web development with Lua Programming Language - code.talks 2015

@etiene_dcode.talks 2015

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.4 (Chibi) • 0.5 (Pluto) will be released soon!

Page 40: Etiene Dalcol - Web development with Lua Programming Language - code.talks 2015

@etiene_dcode.talks 2015

Page 41: Etiene Dalcol - Web development with Lua Programming Language - code.talks 2015

@etiene_dcode.talks 2015

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

Page 42: Etiene Dalcol - Web development with Lua Programming Language - code.talks 2015

@etiene_dcode.talks 2015

• 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?

Page 43: Etiene Dalcol - Web development with Lua Programming Language - code.talks 2015

@etiene_dcode.talks 2015

Not so great things

• It’s still in early development • Things are changing fast • It lacks features • Documentation

Page 44: Etiene Dalcol - Web development with Lua Programming Language - code.talks 2015

@etiene_dcode.talks 2015

How to get Sailor!

$ luarocks install sailor $ sailor create ‘My App’ /var/www $ cd /var/www/my_app $ lua start-server.lua

Page 45: Etiene Dalcol - Web development with Lua Programming Language - code.talks 2015

@etiene_dcode.talks 2015

Page 46: Etiene Dalcol - Web development with Lua Programming Language - code.talks 2015

@etiene_dcode.talks 2015

$ luarocks install luasql-mysql

How to get Sailor!

$ luarocks install sailor $ sailor create ‘My App’ /var/www $ cd /var/www/my_app $ lua start-server.lua

Page 47: Etiene Dalcol - Web development with Lua Programming Language - code.talks 2015

@etiene_dcode.talks 2015

/conf /controllers /models /pub /runtime /tests /themes /views

App structure

Page 48: Etiene Dalcol - Web development with Lua Programming Language - code.talks 2015

@etiene_dcode.talks 2015

/conf /controllers /models /pub /runtime /tests /themes /views

App structure

Lua filesStuff!

JS libraries, images…Temp files Lua Pages

Page 49: Etiene Dalcol - Web development with Lua Programming Language - code.talks 2015

@etiene_dcode.talks 2015

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

Page 50: Etiene Dalcol - Web development with Lua Programming Language - code.talks 2015

@etiene_dcode.talks 2015

<!-- /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!

Page 51: Etiene Dalcol - Web development with Lua Programming Language - code.talks 2015

@etiene_dcode.talks 2015

Page 52: Etiene Dalcol - Web development with Lua Programming Language - code.talks 2015

@etiene_dcode.talks 2015

<?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 js.window.alert(another_msg) -- Sailor v0.4 -- window:alert(another_msg) -- ^ Sailor v0.5 ?>

Example!

Page 53: Etiene Dalcol - Web development with Lua Programming Language - code.talks 2015

@etiene_dcode.talks 2015

Page 54: Etiene Dalcol - Web development with Lua Programming Language - code.talks 2015

@etiene_dcode.talks 2015

local user = {} local v = require “valua” -- validation module user.attributes = { { id = “safe” }, { name = v:new().not_empty().len(6,50) } } user.db = { key = ‘id’, table = ‘users’ } user.relations = { posts = { -- u.posts relation = “HAS_MANY”, model = “post”, attribute = “author_id” } } return user

Example!

Page 55: Etiene Dalcol - Web development with Lua Programming Language - code.talks 2015

@etiene_dcode.talks 2015

local user = {} local v = require “valua” -- validation module user.attributes = { { id = “safe” }, { name = v:new().not_empty().len(6,50) } } user.db = { key = ‘id’, table = ‘users’ } user.relations = { posts = { -- u.posts relation = “HAS_MANY”, model = “post”, attribute = “author_id” } } return user

Example!

Page 56: Etiene Dalcol - Web development with Lua Programming Language - code.talks 2015

@etiene_dcode.talks 2015

-- /controllers/site.lua local site = {}

function site.index(page) local User = sailor.model(‘user’) local u = User:new() u.name = ‘Arnold’ local msg if u:save() then msg = ‘Success’ else msg = table.unpack(u.errors) end local users = User:find_all() page:render(‘index’, { msg = msg, users = users } )

end

return site

Example!

Page 60: Etiene Dalcol - Web development with Lua Programming Language - code.talks 2015

sailorproject.org

github.com/Etiene/sailor

[email protected]

@etiene_d

Thank you!