Deep Analysis of LUA Web Development Learning notes

Source: Internet
Author: User

LUA Web developmentLearning notes are the content to be introduced in this article, mainly to understandLUAInWeb DevelopmentFor more information, see the implementation of the specific content.

Kepler Environment Construction

I do it myselfWeb DevelopmentNaturally, I like to move new thingsWebContact. When it comes to Web and Lua, everyone will naturally think of an open source project Kepler http://www.keplerproject.org /). It gives me the biggest feeling that it is short and concise, and it is a bit unbelievable, including the download and installation packages of Socket, IO, TCP/IP, HTTP, WebCGI and other multi-functional module software packages, it's about K! The installation process is also very simple. I will write down the process of installing Kepler in Windows2003/XP:

1, download and install LuaRocks http://www.luarocks.org/), the default latest version of the installation directory in C: LuaRocks.5.2, after installation, it is recommended to add this directory to the Windows system Path environment parameters.

2. In the CMD command window, type luarocks install kepler-xavante without adding the Path parameter. You need to specify the full luarocks Path.) note that your computer must be connected to the network properly, so that you can download the Kepler and Xavante program feature packages online. There will be about 3 ~ Download and install the SDK within 5 minutes.

3. After Kepler is installed, the C: LuaRocks directory creates two subdirectories bin and rocks. The following is the executable file of the Kepler core, the Lua software feature package and configuration information used by rocks are shown below. After the installation is complete, we recommend that you add the C: LuaRocks. in directory to the Path environment parameters of Windows.

4. Run the "setup-kepler" command without adding the Path parameter. Run the "C: LuaRocks. insetup-kepler" command to configure the default Web site. I use the default configuration after Kepler installation. If you want to change the configuration, you can modify C: LuaRocks

The content of the setup-kepler file in the ockskepler1.1-1.in directory.

5,WebAfter the site Initialization is complete, you will find that in the C: LuaRocks directory, there is an additional kepler subdirectory. This directory name and the above installationWebSite configuration). This is the content of the site that we will pay attention to in the future. Open the C: LuaRockskeplerhtdocs est. lp file to see if it feels like the content in the middle isLuaSyntax ASP/JSP? :)

6, WebIf the website content is ready, the Web server process may be started. Run xavante_start without adding the Path parameter. You can run the C: LuaRocks. inxavante_start batch processing command. The parameter configuration of the Web site is described in detail below.
The installation has been completed. If the default configuration of Kepler is used, open http: // localhost: 8080/in your browser to see the welcome page of Kepler?

Like genuine raiders of friends, the installation process can also refer to the http://www.keplerproject.org/en/Installation, but I feel it write not detailed enough, especially the last step, there are errors: directly running the xavante.exe file will cause errors that cannot be found in the reference database and cannot be started successfully.WebThis is because the environment path is not configured. If you are a little careful, open the C: LuaRocks. inxavante_start.bat file.

Xavante parameter configuration

In the Kepler software package, the module that actually acts as a Web server is called Xavante. Its configuration file is a Lua file located in C: LuaRockskepleretcxavanteconfig. lua. before using the setup-kepler command to install the site, you can modify the default configuration file C: LuaRocks of Kepler.

Ockskepler1.1-1confxavanteconfig. lua allows your modifications to take effect for all new websites created in the future. Next let's take a look at the configuration content of this configuration file. For instructions on Xavante In the Kepler official manual, see

 
 
  1. http://www.keplerproject.org/xavante/ 

Default content of this file:

-- The copyright information at the beginning of the file is omitted

 
 
  1. require "xavante.filehandler"  
  2. require "xavante.cgiluahandler"  
  3. require "xavante.redirecthandler"  
  4. require "orbit.ophandler"  
  5.  
  6. -- Define here where Xavante HTTP documents scripts are located  
  7. local webDir = XAVANTE_WEB 
  8. local simplerules = {  
  9. { -- URI remapping example  
  10. match = "^/$",  
  11. with = xavante.redirecthandler,  
  12. params = {"index.lp"}  
  13. },   
  14. { -- cgiluahandler example  
  15. match = {"%.lp$", "%.lp/.*$", "%.lua$", "%.lua/.*$" },  
  16. with = xavante.cgiluahandler.makeHandler (webDir)  
  17. },  
  18. { -- ophandler example  
  19. match = {"%.op$", "%.op/.*$" },  
  20. with = orbit.ophandler.makeHandler (webDir)  
  21. },  
  22. { - wsapihandler example  
  23. match = {"%.ws$", "%.ws/" },  
  24. with = wsapi.xavante.makeGenericHandler (webDir)  
  25. },  
  26. { -- filehandler example  
  27. match = ".",  
  28. with = xavante.filehandler,  
  29. params = {baseDir = webDir}  
  30. },  
  31. }   
  32.  Displays a message in the console with the used ports  
  33. xavante.start_message(function (ports)  
  34. local date = os.date("[%Y-%m-%d %H:%M:%S]")  
  35. print(string.format("%s Xavante started on port(s) %s",  
  36. date, table.concat(ports, ", ")))  
  37. end)  
  38. xavante.HTTP{  
  39. server = {host = "*", port = 8080},  
  40. defaultHost = {  
  41. rules = simplerules 
  42. },  

The file is divided into three parts:

 
 
  1. simplerules, xavante.start_message, xavante.HTTP: 

Simplerules: similar to ASP. net in IIS, the URL Rewrite function of the URL resend function, to put it bluntly, is to search in order, find the matching Request URL regular item, forward it to a script file in the Web site defined in this matching item for calculation, and finally return the HTTP Response content.

Xavante. start_message: used to record the Log record format configuration of the program after each Xavante process starts.

Xavante. HTTP: used to configure the main parameters of the Web server. Modify port = 8080 to modify the default port of the HTTP service. To bind a domain name to your Xavante server, add one:

 
 
  1. VirtualHosts = {
  2. ["Www.rex.com"] = simplerules --www.rex.com is the domain name of your website.
  3. },

Note: The H of virtualHosts must be capitalized here, otherwise the program will report an error! This is because of a written mistake on the official Kepler website. I tried this after being transferred for more than half an hour. I hope my friends will not take any detours. In addition, after the virtualHosts section is configured, the content of the original defaultHost Section cannot be removed. Otherwise, a program error may occur. After the domain name is bound, my complete xavante. HTTP configuration is as follows:

 
 
  1. xavante.HTTP{  
  2. server = {host = "*", port = 80},  
  3. defaultHost = {  
  4. rules = simplerules 
  5. },  
  6. virtualHosts = {  
  7. ["www.rex.com"] = simplerules  
  8. },  

Note: If you have other Web services such as Appach ISS and use port 80 at the same time, Xavante startup errors will also occur. Therefore, pause other Web Services before starting Xavante. I have always been puzzled: Why does the Xavante configuration have to force the Port configuration to each site? I am a newbie. Thank you for your advice!

I tried to use Kepler and Xavante for only about two days. I wrote so many articles first, hoping to have a chance to discuss and learn with friends interested in it!

Embed LUA into an ASP. Net page

Considering the stability of Xavante, I decided to use IIS as a Web server and use the Progress object in ASP. Net to call LUA's VM to call LUA code. Create An ASPX page and write the following Code:

 
 
  1. using System.Collections;  
  2. using System.Configuration;  
  3. using System.Data;  
  4. using System.Linq;  
  5. using System.Web;  
  6. using System.Web.Security;  
  7. using System.Web.UI;  
  8. using System.Web.UI.HtmlControls;  
  9. using System.Web.UI.WebControls;  
  10. using System.Web.UI.WebControls.WebParts;  
  11. using System.Xml.Linq;  
  12. using System.Diagnostics;  
  13. public partial class _Default : System.Web.UI.Page  
  14. {  
  15. protected void Page_Load(object sender, EventArgs e)  
  16. {  
  17. if (!object.Equals(Request["lua"], null))  
  18. {  
  19. string luaFile = Request["lua"];  
  20. if (!string.IsNullOrEmpty(luaFile))  
  21. {  
  22. Response.Cache.SetNoStore();  
  23. Response.Cache.SetNoServerCaching();  
  24. string output = _Default.EnvokeLua(this, luaFile);  
  25. Response.Write(output);  
  26. Response.End();  
  27. }  
  28. }  
  29. }  
  30.  
  31. static string EnvokeLua(Page pg,string luaFile)  
  32. {  
  33. string rtval = string.Empty;  
  34. HttpServerUtility hsu = pg.Server;  
  35. string exeFile = hsu.MapPath("~/lib/lua.exe");  
  36. string luaPath = hsu.MapPath(string.Format("~/lua/{0}.lua", luaFile));  
  37. using (Process proc = new Process())  
  38. {  
  39. proc.StartInfo.FileName = exeFile;  
  40. proc.StartInfo.Arguments = string.Format(@" {0}", luaPath);  
  41. proc.StartInfo.RedirectStandardOutput = true;  
  42. proc.StartInfo.UseShellExecute = false;  
  43. proc.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;  
  44. proc.Start();  
  45. proc.WaitForExit();  
  46. rtval = proc.StandardOutput.ReadToEnd();  
  47. }  
  48. return rtval;  
  49. }  

This is simple. You can use An ASPX page as a proxy to explain how to execute the LUA file: Access http: // localhost/default. aspx? Lua = abc. lua, you can explain how to execute the abc. LUA file located in the lua directory of the website, get the output, and get the output through HTTP Response. This pure Text output method is also suitable for AJAX and REST Web programs.

Summary: in-depth analysisLUA Web developmentThe content of the study notes has been introduced. I hope this article will help you! The content of the study notes has been introduced. I hope this article will help you!

Related Article

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.