ZNZNCOOP

WEB-server

May 27th, 2015
375
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. -- Web-server by Zer0Galaxy
  2. on = require("opennet")
  3. fs = require("filesystem")
  4. ser = require("serialization").serialize
  5. webDir='/web/'
  6. maxPacketSize=8000
  7.  
  8. local myIP,err=on.getIP()
  9. if not myIP then
  10.   print(err)
  11.   return
  12. end
  13.  
  14. local sendIP, command
  15. commands={}
  16. function commands.ping()
  17.   return "pong"
  18. end
  19.  
  20. function commands.ver()
  21.   return "Web-server ver 1.0"
  22. end
  23.  
  24. function commands.get(path)
  25.   path=path or 'index'
  26.   file=io.open(webDir..path,'r')
  27.   if not file then file=io.open(webDir..'404','r') end
  28.   if file then
  29.     text=file:read("*a") file:close()
  30.   else
  31.     text="Файл "..path.." не найден"
  32.   end
  33.   return text
  34. end
  35.  
  36. function commands.list(path)
  37.   local result={}
  38.   path=webDir..(path or "")
  39.   for name in fs.list(path) do
  40.     result[#result+1]=name
  41.   end
  42.   return ser(result)
  43. end
  44.  
  45. while true do
  46.   local dat = {on.receive()}
  47.   sendIP, command = dat[1], dat[2]
  48.   if command then
  49.     if commands[command] then
  50.       on.send(sendIP, command, commands[command](table.unpack(dat,3)) )
  51.     else
  52.       on.send(sendIP, false, command, "Недопустимая команда" )
  53.     end
  54.   end
  55. end
RAW Paste Data