web development with lua @ bulgaria web summit 2016

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

Upload: etiene-dalcol

Post on 16-Jan-2017

694 views

Category:

Software


1 download

TRANSCRIPT

Page 1: Web development with Lua @ Bulgaria Web Summit 2016

Web development with Lua Programming Language

Introducing Sailor, an MVC web framework in Lua

Etiene Dalcol @etiene_d

Page 2: Web development with Lua @ Bulgaria Web Summit 2016

@etiene_dBulgaria Web Summit 2016

@etiene_d

Page 3: Web development with Lua @ Bulgaria Web Summit 2016

@etiene_dBulgaria Web Summit 2016

Sailor!sailorproject.org

Page 4: Web development with Lua @ Bulgaria Web Summit 2016

@etiene_dBulgaria Web Summit 2016

Google Summer of Code

LabLua

Page 5: Web development with Lua @ Bulgaria Web Summit 2016

@etiene_dBulgaria Web Summit 2016

Lua Ladieslualadies.org

Page 6: Web development with Lua @ Bulgaria Web Summit 2016

@etiene_dBulgaria Web Summit 2016

Lua Space

lua.space

@space_lua

Page 7: Web development with Lua @ Bulgaria Web Summit 2016

@etiene_dBulgaria Web Summit 2016

Lua overview

The state of web dev

Sailor

Page 8: Web development with Lua @ Bulgaria Web Summit 2016

@etiene_dBulgaria Web Summit 2016

Page 9: Web development with Lua @ Bulgaria Web Summit 2016

@etiene_dBulgaria Web Summit 2016

• Script Language • Duck-typed • Multi-paradigm

• procedural, OO, functional,data-description

• Garbage collection • Coroutines • First-class functions • Lexical scoping • Proper tail calls • MIT License

What is Lua?

Page 10: Web development with Lua @ Bulgaria Web Summit 2016

@etiene_dBulgaria Web Summit 2016

Advantages

Powerful.

Page 11: Web development with Lua @ Bulgaria Web Summit 2016

@etiene_dBulgaria Web Summit 2016

Why Lua?Size

(docs included)

First-class functions+

Lexical scoping +

Metatables

Native C API

276 Kb

Object Orientation

Ada, Fortran, Java, Smalltalk, C#, Perl,

Ruby etc.+

Page 12: Web development with Lua @ Bulgaria Web Summit 2016

@etiene_dBulgaria Web Summit 2016

Advantages

Simple.Powerful.

Page 13: Web development with Lua @ Bulgaria Web Summit 2016

@etiene_dBulgaria Web Summit 2016

_G _VERSION assert collectgarbage dofile error getmetatable ipairs load loadfile next pairs pcall print rawequal rawget rawlen rawset require select setmetatable tonumber tostring type xpcall bit32.arshift bit32.band bit32.bnot bit32.bor bit32.btest bit32.bxor bit32.extract bit32.lrotate bit32.lshift bit32.replace bit32.rrotate bit32.rshift

coroutine.create coroutine.resume coroutine.running coroutine.status coroutine.wrap coroutine.yield debug.debug debug.getuservalue debug.gethook debug.getinfo debug.getlocal debug.getmetatable debug.getregistry debug.getupvalue debug.setuservalue debug.sethook debug.setlocal debug.setmetatable debug.setupvalue debug.traceback debug.upvalueid debug.upvaluejoin io.close io.flush io.input io.lines io.open io.output io.popen io.read io.stderr io.stdin io.stdout io.tmpfile io.type io.write

file:close file:flush file:lines file:read file:seek file:setvbuf file:write math.abs math.acos math.asin math.atan math.atan2 math.ceil math.cos math.cosh math.deg math.exp math.floor math.fmod math.frexp math.huge math.ldexp math.log math.max math.min math.modf math.pi math.pow math.rad math.random math.randomseed math.sin math.sinh math.sqrt math.tan math.tanh

os.clock os.date os.difftime os.execute os.exit os.getenv os.remove os.rename os.setlocale os.time os.tmpname package package.config package.cpath package.loaded package.loadlib package.path package.preload package.searchers package.searchpath string.byte string.char string.dump string.find string.format string.gmatch string.gsub string.len string.lower string.match string.rep string.reverse string.sub string.upper table.concat table.insert table.pack table.remove table.sort table.unpack

Page 14: Web development with Lua @ Bulgaria Web Summit 2016

@etiene_dBulgaria Web Summit 2016

Page 15: Web development with Lua @ Bulgaria Web Summit 2016

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

Page 16: Web development with Lua @ Bulgaria Web Summit 2016

@etiene_dBulgaria Web Summit 2016

Advantages

Fast.Simple.Powerful.

Page 17: Web development with Lua @ Bulgaria Web Summit 2016

@etiene_dBulgaria Web Summit 2016

http://www.humbedooh.com/presentations/ACNA%20-%20mod_lua.odp Introducing mod_lua by Daniel Gruno

Page 18: Web development with Lua @ Bulgaria Web Summit 2016

@etiene_dBulgaria Web Summit 2016

Better Reasons

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

My

Page 19: Web development with Lua @ Bulgaria Web Summit 2016

@etiene_dBulgaria Web Summit 2016

Better Reasons

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

Page 20: Web development with Lua @ Bulgaria Web Summit 2016

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

Page 21: Web development with Lua @ Bulgaria Web Summit 2016

@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

Page 22: Web development with Lua @ Bulgaria Web Summit 2016

@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

Page 23: Web development with Lua @ Bulgaria Web Summit 2016

@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

Page 24: Web development with Lua @ Bulgaria Web Summit 2016

@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

Page 25: Web development with Lua @ Bulgaria Web Summit 2016

@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

Page 26: Web development with Lua @ Bulgaria Web Summit 2016

@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

Page 27: Web development with Lua @ Bulgaria Web Summit 2016

@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

Page 28: Web development with Lua @ Bulgaria Web Summit 2016

@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

Page 29: Web development with Lua @ Bulgaria Web Summit 2016

@etiene_dBulgaria Web Summit 2016

?

?

Learning Lua

?

? ?

Page 30: Web development with Lua @ Bulgaria Web Summit 2016

@etiene_dBulgaria Web Summit 2016

Lua on the web

• Early stage

• cgilua ~ 1995

• Kepler Project ~ 2003

Page 31: Web development with Lua @ Bulgaria Web Summit 2016

@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

Page 32: Web development with Lua @ Bulgaria Web Summit 2016

@etiene_dBulgaria Web Summit 2016

Page 33: Web development with Lua @ Bulgaria Web Summit 2016

@etiene_dBulgaria Web Summit 2016

9423 words

http://eev.ee/blog/2012/04/09/php-a-fractal-of-bad-design/

Page 34: Web development with Lua @ Bulgaria Web Summit 2016
Page 35: Web development with Lua @ Bulgaria Web Summit 2016
Page 36: Web development with Lua @ Bulgaria Web Summit 2016

@etiene_dBulgaria Web Summit 2016

http://w3techs.com/technologies/history_overview/programming_language/ms/y

Page 37: Web development with Lua @ Bulgaria Web Summit 2016

@etiene_dBulgaria Web Summit 2016

Why?

Page 38: Web development with Lua @ Bulgaria Web Summit 2016

@etiene_dBulgaria Web Summit 2016

Why?

http://blog.codinghorror.com/php-sucks-but-it-doesnt-matter/

Page 39: Web development with Lua @ Bulgaria Web Summit 2016

@etiene_dBulgaria Web Summit 2016

Page 40: Web development with Lua @ Bulgaria Web Summit 2016

@etiene_dBulgaria Web Summit 2016

Page 41: Web development with Lua @ Bulgaria Web Summit 2016

@etiene_dBulgaria Web Summit 2016

Servers

• Apache: mod_lua

• Nginx: OpenResty

Page 42: Web development with Lua @ Bulgaria Web Summit 2016

@etiene_dBulgaria Web Summit 2016

Servers

• Apache: mod_lua

• Nginx: OpenResty

Page 43: Web development with Lua @ Bulgaria Web Summit 2016

@etiene_dBulgaria Web Summit 2016

Servers

• Apache: mod_lua

• Nginx: OpenResty

• Xavante

• Others: Lighttpd, Lwan, Pegasus, Mongoose

Page 44: Web development with Lua @ Bulgaria Web Summit 2016

@etiene_dBulgaria Web Summit 2016

Frameworks

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

Page 45: Web development with Lua @ Bulgaria Web Summit 2016

@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

Page 46: Web development with Lua @ Bulgaria Web Summit 2016

@etiene_dBulgaria Web Summit 2016

Frameworks

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

Page 47: Web development with Lua @ Bulgaria Web Summit 2016

@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

Page 48: Web development with Lua @ Bulgaria Web Summit 2016

@etiene_dBulgaria Web Summit 2016

Sailor!sailorproject.org

Page 49: Web development with Lua @ Bulgaria Web Summit 2016

@etiene_dBulgaria Web Summit 2016

Sailor!

Page 50: Web development with Lua @ Bulgaria Web Summit 2016

@etiene_dBulgaria Web Summit 2016

Sailor!

0.1 (Venus)

Page 51: Web development with Lua @ Bulgaria Web Summit 2016

@etiene_dBulgaria Web Summit 2016

Sailor!

0.1 (Venus)

0.2 (Mars)

Page 52: Web development with Lua @ Bulgaria Web Summit 2016

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

Page 53: Web development with Lua @ Bulgaria Web Summit 2016

@etiene_dBulgaria Web Summit 2016

Page 54: Web development with Lua @ Bulgaria Web Summit 2016

@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

Page 55: Web development with Lua @ Bulgaria Web Summit 2016

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

Page 56: Web development with Lua @ Bulgaria Web Summit 2016

@etiene_dBulgaria Web Summit 2016

Not so great things

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

Page 57: Web development with Lua @ Bulgaria Web Summit 2016

@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

Page 58: Web development with Lua @ Bulgaria Web Summit 2016

@etiene_dBulgaria Web Summit 2016

Page 59: Web development with Lua @ Bulgaria Web Summit 2016

@etiene_dBulgaria Web Summit 2016

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

App structure

Page 60: Web development with Lua @ Bulgaria Web Summit 2016

@etiene_dBulgaria Web Summit 2016

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

App structure

Lua filesStuff!

JS libraries, images…Temp files Lua Pages

Page 61: Web development with Lua @ Bulgaria Web Summit 2016

@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

Page 62: Web development with Lua @ Bulgaria Web Summit 2016

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

Page 63: Web development with Lua @ Bulgaria Web Summit 2016

@etiene_dBulgaria Web Summit 2016

Page 64: Web development with Lua @ Bulgaria Web Summit 2016

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

Page 65: Web development with Lua @ Bulgaria Web Summit 2016

@etiene_dBulgaria Web Summit 2016

Page 66: Web development with Lua @ Bulgaria Web Summit 2016

@etiene_dBulgaria Web Summit 2016

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 67: Web development with Lua @ Bulgaria Web Summit 2016

@etiene_dBulgaria Web Summit 2016

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 68: Web development with Lua @ Bulgaria Web Summit 2016

@etiene_dBulgaria Web Summit 2016

bit.ly/luawebdev

Page 69: Web development with Lua @ Bulgaria Web Summit 2016

@etiene_dBulgaria Web Summit 2016

Rails Girls Summer of Code

railsgirlssummerofcode.org

Page 71: Web development with Lua @ Bulgaria Web Summit 2016

sailorproject.org

github.com/sailorproject

[email protected]

@etiene_d

gitter.im/sailorproject/sailor

@sailor_lua

Page 72: Web development with Lua @ Bulgaria Web Summit 2016
Page 73: Web development with Lua @ Bulgaria Web Summit 2016

Thank you!sailorproject.org

github.com/sailorproject

[email protected]

@etiene_d