Processing Lua pages

Here’s a small sample that processes files with lua code, like php. You can use this in your own app/webserver to server lua pages.

What is a Lua Server Page

A Lua server page is an HTML document that has embedded Lua code. It can have a similar structure like an ASP Page or a PHP Page. The embedding of the executable code is within the tags and is identified with the language tags as <?lua ?> with the executable code within the tag as

For our processor, we shall first load the file which is achieved as

Decoding the data

In the function above, we are calling the decode function which processes the string data or runs the Lua code by running the Lua code and returns the resulting text.

Closing notes

This can be extended to include the HTML header data so that when it is returned from a web server, it has the headers, etc.

The standard HTML 1.1 headers look something like

And when the file/data is not found (404 error code), the header to return could look like

This code can be used in your own applications and also if you were to write a webserver that serves simple web pages. The bit for that would be simple as setting up a socket layer and listening on port 80 and returning data. Maybe a follow-up article could help run a Lua server.

Comments are closed.