moonbridge

annotate helloworld.lua @ 326:4379766473ec

Fixed proper errno handling for blocking I/O in localconnect function for connecting to sockets
author jbe
date Tue Jun 23 12:25:58 2020 +0200 (4 months ago)
parents db8abd21762c
children
rev   line source
jbe@33 1 -- minimal example application for Moonbridge
jbe@33 2 -- invoke with ./moonbridge helloworld.lua
jbe@33 3 --
jbe@33 4 -- see example_application.lua for a more elaborated example
jbe@33 5
jbe@33 6 local http = require "moonbridge_http"
jbe@33 7
jbe@33 8 listen{
jbe@125 9 { proto = "tcp", host = "127.0.0.1", port = 8080 }, -- IPv4
jbe@125 10 { proto = "tcp", host = "::1", port = 8080 }, -- IPv6
jbe@33 11 connect = http.generate_handler(
jbe@33 12 function(request)
jbe@33 13 local function error_response(status)
jbe@33 14 request:send_status(status)
jbe@33 15 request:send_header("Content-Type", "text/html")
jbe@221 16 request:send_data("<html>\n")
jbe@221 17 request:send_data("<head><title>", status, "</title></head>\n")
jbe@221 18 request:send_data("<body><h1>", status, "</h1></body>\n")
jbe@221 19 request:send_data("</html>\n")
jbe@33 20 end
jbe@33 21 if request.method == "GET" or request.method == "HEAD" then
jbe@33 22 if request.path == "" then
jbe@33 23 request:send_status("200 OK")
jbe@33 24 request:send_header("Content-Type", "text/html; charset=UTF-8")
jbe@221 25 request:send_data("<html>\n")
jbe@221 26 request:send_data("<head><title>Hello World Application</title></head>\n")
jbe@221 27 request:send_data("<body><h1>Hello World!</h1></body>\n")
jbe@221 28 request:send_data("</html>\n")
jbe@33 29 else
jbe@33 30 error_response("404 Not Found")
jbe@33 31 end
jbe@33 32 else
jbe@33 33 error_response("405 Method not allowed")
jbe@33 34 end
jbe@33 35 return true
jbe@33 36 end
jbe@33 37 )
jbe@33 38 }
jbe@33 39

Impressum / About Us