Improve demo files

This commit is contained in:
Marcos Kirsch 2015-02-22 16:06:06 -06:00
parent 5fe2dfcf9d
commit 316af66d3c
3 changed files with 34 additions and 32 deletions

View File

@ -1,21 +1,20 @@
local function sendHeader(connection) return function (connection, args)
connection:send("HTTP/1.0 200 OK\r\nContent-Type: text/html\r\Cache-Control: private, no-store\r\n\r\n") connection:send("HTTP/1.0 200 OK\r\nContent-Type: text/html\r\Cache-Control: private, no-store\r\n\r\n")
end connection:send('<!DOCTYPE html><html lang="en"><head><meta charset="utf-8"><title>Server File Listing</title></head>')
connection:send('<body>')
local function sendFileList(connection) connection:send('<h1>Server File Listing</h1>')
connection:send("<ul>\n") connection:send("<ul>\n")
local l = file.list() local l = file.list()
for name, size in pairs(l) do for name, size in pairs(l) do
connection:send(" <li>" .. name .. " (" .. size .. ")</li>\n")
local isHttpFile = string.match(name, "(http/)") ~= nil
local url = string.match(name, ".*/(.*)")
if isHttpFile then
connection:send(' <li><a href="' .. url .. '">' .. url .. "</a> (" .. size .. " bytes)</li>\n")
end
end end
connection:send("</ul>\n") connection:send("</ul>\n")
end
return function (connection, args)
sendHeader(connection)
connection:send('<!DOCTYPE html><html lang="en"><head><meta charset="utf-8"><title>A Lua script sample</title></head>')
connection:send('<body>')
connection:send('<h1>File Listing</h1>')
sendFileList(connection)
connection:send('</body></html>') connection:send('</body></html>')
end end

View File

@ -8,20 +8,21 @@
<h1>Hello World!</h1> <h1>Hello World!</h1>
<div> <div>
<section> <section>
<h3>nodemcu-httpserver</h3> <p>
<p>This page is served by nodemcu-httpserver running on an ESP8266. One of the smallest web servers... ever!</p> This page is served by <b>nodemcu-httpserver</b> running on an ESP8266 that uses the <a href="http://nodemcu.com/index_en.html">NodeMCU</a> firmware.
NodeMCU puts a <a href="http://www.lua.org">Lua</a> interpreter inside the ESP8266. This is surely one of the smallest web servers to date!
</p>
<h3>Where's the source code?</h3> <h3>Where's the source code?</h3>
<p><a href="https://github.com/marcoskirsch/nodemcu-httpserver">Right here</a></p> <p>You can find the Lua code for nodemcu-httpserver in <a href="https://github.com/marcoskirsch/nodemcu-httpserver">GitHub</a></p>
<h3>Show me the demos</h3> <h3>Serve me some pages!</h3>
<ul> <ul>
<li><a href="index.html">This page</a> (static)</li> <li><a href="index.html">Index</a>: This page (static)</li>
<li><a href="file_list.lua">List all server files</a> (Lua)</li> <li><a href="args.lua">Arguments</a>: Parses arguments passed in the URL and prints them. (Lua)</li>
<li><a href="node_info.lua">NodeMCU info</a> (Lua)</li> <li><a href="file_list.lua">List all server files</a>: Displays a list of all the server files. (Lua)</li>
<li><a href="memory.lua">Memory statistics</a> (Lua)</li> <li><a href="node_info.lua">NodeMCU info</a>: Shows some basic NodeMCU(Lua)</li>
<li><a href="foo.html">A file not found</a> (error)</li> <li><a href="foo.html">Foo</a>: A file that isn't actually in the server. Should error (error)</li>
<li><a href="args.lua?color=red&size=big&number=42">Argument parsing</a> (Lua)</li>
</ul> </ul>
</body> </body>
</html> </html>

View File

@ -13,15 +13,17 @@ return function (connection, args)
connection:send('<body>') connection:send('<body>')
connection:send('<h1>Node info</h1>') connection:send('<h1>Node info</h1>')
majorVer, minorVer, devVer, chipid, flashid, flashsize, flashmode, flashspeed = node.info(); majorVer, minorVer, devVer, chipid, flashid, flashsize, flashmode, flashspeed = node.info();
connection:send('<ul>Node info:') sendAttr(connection, "majorVer" , majorVer)
sendAttr(connection, "majorVer" , majorVer) sendAttr(connection, "devVer" , devVer)
sendAttr(connection, "devVer" , devVer) sendAttr(connection, "chipid" , chipid)
sendAttr(connection, "chipid" , chipid) sendAttr(connection, "flashid" , flashid)
sendAttr(connection, "flashid" , flashid) sendAttr(connection, "flashsize" , flashsize)
sendAttr(connection, "flashsize" , flashsize) sendAttr(connection, "flashmode" , flashmode)
sendAttr(connection, "flashmode" , flashmode) sendAttr(connection, "flashspeed" , flashspeed)
sendAttr(connection, "flashspeed" , flashspeed) sendAttr(connection, "node.heap()" , node.heap())
sendAttr(connection, "node.heap()" , node.heap()) sendAttr(connection, 'Memory in use (KB)' , collectgarbage("count"))
sendAttr(connection, 'IP address' , wifi.sta.getip())
sendAttr(connection, 'MAC address' , wifi.sta.getmac())
connection:send('</ul>') connection:send('</ul>')
connection:send('</body></html>') connection:send('</body></html>')
end end