当前位置: 移动技术网 > IT编程>脚本编程>Lua > Lua下基本的网络编程示例

Lua下基本的网络编程示例

2017年12月08日  | 移动技术网IT编程  | 我要评论

 lua是高度灵活的语言,它往往是在多个平台,包括web应用程序中使用。成立2004年的kepler社区提供lua的web组件开放源码。

虽然,也有使用lua已经开发了其他的web框架,我们将主要集中在kepler社区提供的组件。
应用程序和框架

  •     orbit 是一个lua的mvc web框架,它是基于wsapi。
  •     wsapi是从lua的web应用程序抽象的web主机服务器,是基于许多项目的api。
  •     xavante是一个lua的web服务器,提供了一个wsapi接口。
  •     sputnik是一个wiki/cms开发过wsapi的kepler项目用于娱乐和搞笑。
  •     cgilua提供luapages和luascripts网页制作的基础上的wsapi,但不再支持。使用orbit, sputnik 或wsapi 代替。

在本教程中,我们会尽量让你可以做更多地了解lua,它的安装和使用方法,请参考kepler网站
orbit

orbit是lua一个mvc web框架。它彻底抛弃cgilua 的应用,每个应用程序orbit可以容纳一个单一文件“脚本”的cgilua模式,但是如果想让它分割成多个文件也可以。

所有orbit应用程序遵循wsapi协议,所以他们目前正在与xavante,cgi和fastcgi的一起结合工作。它包括一个发射器,可以很容易推出xavante实例进行开发。

安装orbit的最简单方法是使用luarocks。 luarocks安装orbit的安装命令。对于这一点,首先需要先安装luarocks。

如果还没有安装所有的依赖,这是应遵循在的unix / linux环境设置orbit的步骤。
安装apache

连接到服务器。安装apache2,它支持的模块和能使用所需的apache2模块:

$ sudo apt-get install apache2 libapache2-mod-fcgid libfcgi-dev build-essential
$ sudo a2enmod rewrite
$ sudo a2enmod fcgid
$ sudo /etc/init.d/apache2 force-reload

安装 luarocks

$ sudo apt-get install luarocks

安装wsapi, fcgi, orbit, and xavante

$ sudo luarocks install orbit
$ sudo luarocks install wsapi-xavante
$ sudo luarocks install wsapi-fcgi

建立apache2

$ sudo raj /etc/apache2/sites-available/default

添加下面的 <directory /var/www/>部分配置文件的这些内容。如果这个部分有一个“allowoverride none”,那么需要的“none”改为“all”,这样htaccess文件可以覆盖配置。

<ifmodule mod_fcgid.c>
    addhandler fcgid-script .lua
    addhandler fcgid-script .ws
    addhandler fcgid-script .op
    fcgiwrapper "/usr/local/bin/wsapi.fcgi" .ws
    fcgiwrapper "/usr/local/bin/wsapi.fcgi" .lua
    fcgiwrapper "/usr/local/bin/op.fcgi" .op
    #fcgiserver "/usr/local/bin/wsapi.fcgi" -idle-timeout 60 -processes 1
    #idletimeout 60
    #processlifetime 60
</ifmodule>

重新启动服务器,以确保变化生效。

为了使可以访问应用程序,需要+ execcgi添加到htaccess文件中的orbit 应用程序的根- 在这种情况下,设置为 /var/www。

options +execcgi
directoryindex index.ws

简单的例子- orbit

#!/usr/bin/env index.lua
-- index.lua
require"orbit"

-- declaration
 module("myorbit", package.seeall, orbit.new)

-- handler
function index(web)
  return my_home_page()
end

-- dispatch
myorbit:dispatch_get(index, "/", "/index")

-- sample page
function my_home_page()
   return [[
    <head></head>
    <html>
    <h2>first page</h2>
    </html>
    ]]
end

现在,应该可以启动web浏览器并转到http://localhost:8080/ ,应该看到

orbit 提供了另一种选择,那就是lua代码可以生成html。

#!/usr/bin/env index.lua
-- index.lua
require"orbit"

function generate()
    return html {
        head{title "html example"},
        body{
            h2{"here we go again!"}
        }
    }
end

orbit.htmllify(generate)

print(generate())

创建表单

一种简单形式的例子如下所示。

#!/usr/bin/env index.lua
require"orbit"

function wrap (inner)
    return html{ head(), body(inner) }
end

function test ()
    return wrap(form (h'table' {
        tr{td"first name",td( input{type='text', name='first'})},
        tr{td"second name",td(input{type='text', name='second'})},
        tr{ td(input{type='submit', value='submit!'}),
            td(input{type='submit',value='cancel'})
        },
    }))
end

orbit.htmllify(wrap,test)

print(test())

你可以在官方网站上找到一个很长的orbit教程
wsapi

如前面提到的,wsapi充当基础由许多项目嵌入其中的多个特征。可以使用wasapi并支持以下平台,

  •     windows
  •     unix-based systems

支持的服务器和接口由wsapi包括:

  •     cgi
  •     fastcgi
  •     xavante

wsapi提供了大量的使用lua库,这使得我们更容易在网络编程。一些支持的功能在lua包括,

  •     request processing
  •     output buffering
  •     authentication
  •     file uploads
  •     request isolation
  •     multiplexing

wsapi一个简单的例子如下所示。

#!/usr/bin/env wsapi.cgi

module(..., package.seeall)
function run(wsapi_env)
   local headers = { ["content-type"] = "text/html" }
  
   local function hello_text()
      coroutine.yield("<html><body>")
      coroutine.yield("<p>hello wsapi!</p>")
      coroutine.yield("<p>path_info: " .. wsapi_env.path_info .. "</p>")
      coroutine.yield("<p>script_name: " .. wsapi_env.script_name .. "</p>")
      coroutine.yield("</body></html>")
   end

   return 200, headers, coroutine.wrap(hello_text)
end

你可以在一个简单的html页面组成,回到上面的代码中看到。可以看到协程的使用,使得它可以通过语句返回语句来调用函数。最后html状态码(200),头和html页面返回。
xavante

xavante是使用基于uri的模块化架构一个lua1.1的http web服务器映射处理程序。 xavante目前提供,

  •     file handler
  •     redirect handler
  •     wsapi handler

文件处理程序是用于一般文件。重定向处理enabes的uri重新映射和wsapi处理与wsapi申请。

一个简单的例子如下所示。

require "xavante.filehandler"
require "xavante.cgiluahandler"
require "xavante.redirecthandler"

-- define here where xavante http documents scripts are located
local webdir = xavante_web

local simplerules = {

    { -- uri remapping example
      match = "^[^%./]*/$",
      with = xavante.redirecthandler,
      params = {"index.lp"}
    },

    { -- cgiluahandler example
      match = {"%.lp$", "%.lp/.*$", "%.lua$", "%.lua/.*$" },
      with = xavante.cgiluahandler.makehandler (webdir)
    },
   
    { -- filehandler example
      match = ".",
      with = xavante.filehandler,
      params = {basedir = webdir}
    },
}

xavante.http{
    server = {host = "*", port = 8080},
   
    defaulthost = {
     rules = simplerules
    },
}

若要使用xavante虚拟主机,调用xavante.http将改为类似。

xavante.http{
    server = {host = "*", port = 8080},
   
    defaulthost = {},
    
    virtualhosts = {
        ["www.sitename.com"] = simplerules
    }
}

lua web组件

  •     copas,基于协程调度器可以使用通过tcp/ip协议的服务器。
  •     cosmo, “安全模板”引擎,它可以防止在模板中任意代码的应用程序。
  •     coxpcall lua封装原生pcall和xpcall与协程相容的。
  •     luafilesystem, 可移植的方式来访问底层的目录结构和文件属性。
  •     rings, 一个库,它提供了一种从内lua创建新的lua状态。

截至注意

有这么多lua基于web框架,并提供给我们,并根据需要组成部分,它可以作为选件。还有包括其他可用的web框架,

  •     moonstalk 能够有效地开发和托管建成lua语言动态生成基于网络的项目;从基本的网页到复杂的应用程序
  •     lapis, 构建用moonscript(或lua)web应用程序的框架运行的nginx的定制版名为openresty。
  •     lua server pages, 一个lua脚本引擎插件任何其他方法,以嵌入式网络的发展,提供了一个戏剧性的捷径传统的c服务器页面。

这些web框架可以利用web应用程序,并帮助在做强大的操作。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网