Basic network programming examples in LUA

Source: Internet
Author: User
Tags html page lua require web hosting

This article mainly introduces the basic network programming examples in Lua, including simple server building and related Web Component introduction, and so on, need friends can refer to the following

LUA is a highly flexible language that is often used in multiple platforms, including Web applications. The 2004-year Kepler community provides the LUA Web Component open source.

While there are other web frameworks that have been developed using LUA, we will focus primarily on the components provided by the Kepler community.

Applications and frameworks

Orbit is a LUA MVC Web framework that is based on WSAPI.

Wsapi is a web hosting server that is abstracted from the LUA Web application, and is based on a number of project APIs.

Xavante is a LUA Web server that provides a WSAPI interface.

Sputnik is a wiki/cms developed WSAPI Kepler project for entertainment and comedy.

Cgilua provides luapages and Luascripts Web page production based on WSAPI, but no longer supported. Use orbit, Sputnik or WSAPI instead.

In this tutorial, we will try to make you more aware of LUA, its installation and use methods, please refer to the Kepler website

Orbit

Orbit is a LUA MVC web framework. It completely discards the application of Cgilua, where each application orbit can accommodate a single file "script" Cgilua mode, but if you want to split it into multiple files.

All orbit applications follow the WSAPI protocol, so they are currently working with xavante,cgi and fastcgi. It includes a transmitter that can easily launch Xavante instances for development.

The easiest way to install orbit is to use Luarocks. Luarocks Install the Orbit installation command. For this point, first you need to install Luarocks.

If you have not installed all the dependencies, this is the step you should follow to set up orbit in the Unix/linux environment.

Install Apache

Connect to the server. Install APACHE2, it supports modules and can use the required Apache2 modules:

?

1 2 3 4 $ sudo apt-get install apache2 libapache2-mod-fcgid libfcgi-dev build-essential $ sudo a2enmod rewrite $ sudo a2enmod fcgi D $ sudo/etc/init.d/apache2 Force-reload

Install Luarocks

?

1 $ sudo apt-get install Luarocks

Install Wsapi, fcgi, ORBIT, and Xavante

?

1 2 3 $ sudo luarocks install orbit $ sudo luarocks install wsapi-xavante $ sudo luarocks install wsapi-fcgi

Establish APACHE2

?

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

Add these contents to the following section of the configuration file. If this section has a "allowoverride none", then the required "None" is changed to "all" so that the htaccess file can overwrite the configuration.

The code is as follows:

  

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

  

Reboot the server to ensure that the changes take effect.

In order for the application to be accessible, you need + execcgi to add the root of the orbit application to the htaccess file-in this case, set to/var/www.

The code is as follows:

Options +execcgi

DirectoryIndex index.ws

A simple example-Orbit

The code is as follows:

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

  

  

  

The Page

  

]]

End

Now, you should be able to start your Web browser and go to http://localhost:8080/, and you should see

The code is as follows:

The Page

Orbit offers another option, which is that the LUA code can generate HTML.

The code is as follows:

#!/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 ())

Create a form

An example of a simple form is shown below.

The code is as follows:

#!/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 "Name", TD (Input{type= ' text ', name= ' a ')},

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

You can find a long orbit tutorial on the official website.

Wsapi

As mentioned earlier, WSAPI acts as a number of features that are embedded in many projects. You can use WASAPI and support the following platforms,

Windows

unix-based Systems

Supported servers and interfaces are included by WSAPI:

Cgi

FastCGI

Xavante

WSAPI provides a lot of use of the LUA library, which makes it easier to program on the Web. Some of the supported features are included in Lua,

Request processing

Output Buffering

Authentication

File uploads

Request Isolation

Multiplexing

A simple example of WSAPI is shown below.

The code is as follows:

#!/usr/bin/env wsapi.cgi

Module (..., package.seeall)

function Run (wsapi_env)

Local headers = {["content-type"] = "text/html"}

Local function Hello_text ()

Coroutine.yield ("")

Coroutine.yield ("

Hello wsapi!

")

Coroutine.yield ("

Path_info: ". Wsapi_env. Path_info.. "

")

Coroutine.yield ("

Script_name: ". Wsapi_env. Script_name.. "

")

Coroutine.yield ("")

End

Return, headers, coroutine.wrap (Hello_text)

End

You can make a simple HTML page and go back to the code above to see. You can see the use of the coprocessor so that it can invoke the function through the statement return statement. The last HTML status code (200), Header and HTML page returns.

Xavante

Xavante is a Lua1.1 HTTP Web server mapping handler using a URI-based modular architecture. Xavante is currently available,

File Handler

Redirect Handler

Wsapi Handler

File handlers are used for general files. REDIRECT Processing Enabes uri remapping and WSAPI processing with WSAPI request.

A simple example is shown below.

The code is as follows:

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

},

}

To use the Xavante virtual host, call Xavante. HTTP will change to resemble.

The code is as follows:

Xavante. http{

Server = {host = "*", port = 8080},

Defaulthost = {},

Virtualhosts = {

["www.sitename.com"] = Simplerules

}

}

Lua Web Components

Copas, a server based on the TCP/IP protocol is available for use with the co-scheduler.

Cosmo, the "Security Templates" engine, which prevents any code in the template from being used by the application.

Coxpcall Lua encapsulates native Pcall and Xpcall compatible with the coprocessor.

Luafilesystem, a portable way to access the underlying directory structure and file attributes.

Rings, a library that provides a new LUA state from within Lua.

As of note

There is so much LUA based on the web framework, and provided to us, and as required components, it can be used as an option. There are other web frameworks available,

Moonstalk is able to effectively develop and host the built in Lua language dynamically generated web-based projects; From basic Web pages to complex applications

Lapis, the custom version name of the Nginx that is running the framework of the Moonscript (or LUA) Web application is openresty.

Lua Server Pages, a LUA scripting engine plug-in any other approach to the development of embedded networks, offers a dramatic shortcut to the traditional C server page.

These web frameworks can take advantage of Web applications and help in doing powerful operations.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.