moonbridge

diff example_application.lua @ 0:f6d3b3f70dab

Initial commit
author jbe
date Sun Jan 04 19:30:28 2015 +0100 (2015-01-04)
parents
children 583e2ad140dc
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/example_application.lua	Sun Jan 04 19:30:28 2015 +0100
     1.3 @@ -0,0 +1,150 @@
     1.4 +-- Moonbridge example application
     1.5 +-- invoke with ./moonbridge example_application.lua
     1.6 +
     1.7 +local http = require "http"
     1.8 +
     1.9 +local documents = {"example_webpage.html", "example_webpage.css"}
    1.10 +
    1.11 +listen{
    1.12 +  -- listen to a tcp version 4 socket
    1.13 +  { proto = "tcp4", port = 8080, localhost = true },
    1.14 +
    1.15 +  -- listen to a tcp version 6 socket
    1.16 +  { proto = "tcp6", port = 8080, localhost = true },
    1.17 +
    1.18 +  -- listen to a unix domain socket
    1.19 +  --{ proto = "local", path = 'socket' },
    1.20 +
    1.21 +  -- execute the listener regularly (without incoming connection)
    1.22 +  --{ proto = "interval", name = "myint", delay = 10, strict = false },
    1.23 +
    1.24 +  -- desired number of spare (idle) processes
    1.25 +  pre_fork = 1, -- number of forks
    1.26 +
    1.27 +  -- minimum number of processes
    1.28 +  min_fork = 2, -- number of forks
    1.29 +
    1.30 +  -- maximum number of processes (hard limit)
    1.31 +  max_fork = 16, -- number of forks
    1.32 +
    1.33 +  -- delay between creation of spare processes
    1.34 +  fork_delay = 1, -- seconds
    1.35 +
    1.36 +  -- delay before retry of failed process creation
    1.37 +  fork_error_delay = 2, -- seconds
    1.38 +
    1.39 +  -- delay between destruction of excessive spare processes
    1.40 +  exit_delay = 60, -- seconds
    1.41 +
    1.42 +  -- idle time after a fork gets terminated
    1.43 +  idle_timeout = 0, -- seconds (0 for no timeout)
    1.44 +
    1.45 +  -- maximum memory consumption before process gets terminated
    1.46 +  memory_limit = 1024*1024, -- bytes
    1.47 +  
    1.48 +  -- preparation of process (executed before fork)
    1.49 +  prepare = function()
    1.50 +    for i, document in ipairs(documents) do
    1.51 +      local file = assert(io.open(document))
    1.52 +      documents[document] = file:read("*a")
    1.53 +      file:close()
    1.54 +    end
    1.55 +  end,
    1.56 +  
    1.57 +  -- connection handler
    1.58 +  connect = http.generate_handler(
    1.59 +    {
    1.60 +      static_headers = {"Server: Moonbridge Example Server"},
    1.61 +      request_body_size_limit = 16*1024*1024*1024  -- allow big file uploads
    1.62 +    },
    1.63 +    function(request)
    1.64 +
    1.65 +      if request.method == "GET" or request.method == "HEAD" then
    1.66 +
    1.67 +        if request.path == "/" then
    1.68 +          request:send_status("303 See Other")
    1.69 +          request:send_header("Location", "http://" .. request.headers_value.host .. "/example_webpage.html")
    1.70 +
    1.71 +        else
    1.72 +          local document_name = string.match(request.path, "^/(.*)$")
    1.73 +          local document_extension = string.match(document_name, "%.([^.])$")
    1.74 +          local document = documents[string.match(request.path, "^/(.*)$")]
    1.75 +          if document then
    1.76 +            request:send_status("200 OK")
    1.77 +
    1.78 +            if document_extension == "html" then
    1.79 +              request:send_header("Content-Type", "text/html; charset=UTF-8")
    1.80 +            elseif document_extension == "css" then
    1.81 +              request:send_header("Content-Type", "text/css; charset=UTF-8")
    1.82 +            end
    1.83 +            request:send_data(document)
    1.84 +          else
    1.85 +            request:send_status("404 Not Found")
    1.86 +            request:send_header("Content-Type", "text/html; chatset=UTF-8")
    1.87 +            request:send_data("<html><head><title>404 Not Found</title></head><body><h1>404 Not Found</h1></body></html>")
    1.88 +          end
    1.89 +
    1.90 +        end
    1.91 +
    1.92 +      elseif request.method == "POST" then
    1.93 +
    1.94 +        if request.path == "/post_example" then
    1.95 +          local files = {}
    1.96 +          do
    1.97 +            local file
    1.98 +            request:stream_post_param("files", function(chunk, field_name, meta)
    1.99 +              if meta then
   1.100 +                file = {
   1.101 +                  file_name = meta.file_name,
   1.102 +                  content_type = meta.content_type,
   1.103 +                  length = 0
   1.104 +                }
   1.105 +              end
   1.106 +              if chunk then
   1.107 +                file.length = file.length + #chunk
   1.108 +              else
   1.109 +                files[#files+1] = file
   1.110 +              end
   1.111 +            end)
   1.112 +          end
   1.113 +          
   1.114 +          request:send_status("200 OK")
   1.115 +          request:send_header("Content-Type", "text/html; chatset=UTF-8")
   1.116 +          request:send_data("<html>\n<head>\n")
   1.117 +          request:send_data('<link href="example_webpage.css" rel="stylesheet" type="text/css">\n')
   1.118 +          request:send_data("<title>Moonbridge Network Server for Lua Applications &ndash; Example Application</title>\n")
   1.119 +          request:send_data("</head>\n<body>\n")
   1.120 +          request:send_data("<h1>Moonbridge Network Server for Lua &ndash; Example Application</h1>\n")
   1.121 +          request:send_data("<h2>POST request successful</h2>\n")
   1.122 +          request:send_data('<table>\n<thead><th>File name</th><th>Content type</th><th class="numeric">Bytes received</th></thead>\n<tbody>\n')
   1.123 +          for i, file in ipairs(files) do
   1.124 +            request:send_data("<tr>")
   1.125 +            request:send_data("<td>", http.encode_html(file.file_name or "(unknown)"), "</td>")
   1.126 +            request:send_data("<td>", http.encode_html(file.content_type or "(unknown)"), "</td>")
   1.127 +            request:send_data('<td class="numeric">', http.encode_html(tostring(file.length)), "</td>")
   1.128 +            request:send_data("</tr>\n")
   1.129 +          end
   1.130 +          request:send_data("</tbody>\n</table>\n")
   1.131 +          request:send_data("<p>Submitted comment: ", http.encode_html(request.post_params.comment), "</p>\n")
   1.132 +          request:send_data("</body>\n</html>\n")
   1.133 +
   1.134 +        else
   1.135 +          request:send_status("404 Not Found")
   1.136 +          request:send_data("<html><head><title>404 Not Found</title></head><body><h1>404 Not Found</h1></body></html>")
   1.137 +
   1.138 +      end
   1.139 +        
   1.140 +      else
   1.141 +        request:send_status("405 Method not allowed")
   1.142 +
   1.143 +      end
   1.144 +
   1.145 +      -- returning false causes termination of current process (and re-forking)
   1.146 +      return true
   1.147 +    end),
   1.148 +    
   1.149 +  -- executed on process termination
   1.150 +  finish = function()
   1.151 +  end
   1.152 +}
   1.153 +

Impressum / About Us