Post your best Lua script examples here

User avatar
By yes8s
#3696 I learned a long time ago that good coding practices need to be observed AS you are writng code, almost line by line, and NOT after you've written 1000s of lines of code. :D Correct indentation and relevant comments helps me to follow my own code whether it's only a few hours after coding or years after coding a module.

Handling the form data string is working quite well now. I can pass any key and value to a local database. I am still working on letting the webpage itself define what resources lua needs to allocate in terms of database for form data and variables that need to be passed into the html.

I found this neat way of deciphering the first line of the http request to seprate the method (i.e. GET, POST etc), the path and also the major and minor html version type (if you so wish). A side effect of this is that by looking for this pattern you can automatically check the basic validitiy of the request also. (See here for more good tricks:

Here's what it now looks like:
Code: Select all      .......
         -- Look for a http request         
         _, _, method, path, major, minor  = string.find(request, "([A-Z]+) (.+) HTTP/(%d).(%d)")
         if (method == nil) then
            -- Bad request
            conn:send("Bad Request") print("Bad Request"..payload) conn:on("sent",function(conn) conn:close() end)
         -- Valid request
            -- Was it a POST method request?
            if (method == "POST") then

The string library, and in particular the pattern and capture features, of LUA are awesome but quite confusing. This seems to be a serious coding "weapon" for LUA so the more we can work understand this the more we can do with LUA. Smaller functions, execution and memory usage can all be improved with utilizing the correct string function.

I like what you are doing with embedding lua code into a html. I can see where that can come in handy in many applications. Keep at it and if I can add anything I will.
User avatar
By yes8s
alonewolfx2 wrote:Thank you for your sharing. Safari, chrome or webkit on Android have different payload size and method. I think that's why it can't work webserver with all browsers. I am trying to figure out. But I am new on lua and lua string handling different and difficult. We wish to see i2c side on your code.

If you work this out, please do let me know....