Skip to content

Instantly share code, notes, and snippets.

@Kristopher38
Created July 29, 2020 12:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Kristopher38/5a36d7f91ac6cac218dab85a16e059f1 to your computer and use it in GitHub Desktop.
Save Kristopher38/5a36d7f91ac6cac218dab85a16e059f1 to your computer and use it in GitHub Desktop.
local internet = require("internet")
local fs = require("filesystem")
local process = require("process")
local inspect = require("inspect")
local webserverAddress = "http://93.181.131.201:8000"
local function to_bool(str)
if str == "true" or str == "True" then
return true
elseif str == "false" or str == "False" then
return false
else
return nil
end
end
local function split(str, separator)
list = {}
i = 1
current_pos = 0
while current_pos do
local l, r = str:find(separator, current_pos+1)
list[i] = str:sub(current_pos+1, l and l-1 or l)
current_pos = r
i = i + 1
end
if #list[i - 1] == 0 then -- fix for empty string at the last position
list[i - 1] = nil
end
return list
end
local function parse_result(request)
local result = ""
for chunk in request do
result = result..chunk
end
local responseCode, message, headers = getmetatable(request).__index.response()
if responseCode == 200 then
for k, line in pairs(split(result, "\n")) do
local items = split(line, " ")
local action = items[1]
local file = items[2]
local isdir = items[3]
print(action, file, isdir)
actions[action](file, to_bool(isdir))
if file == "watcheroc.lua" then
print("reloading watcheroc")
-- os.execute(process.info().path)
-- reload
end
end
end
end
actions = {}
actions.update = function(filename, isdir)
if not isdir then
if fs.exists(filename) then
os.execute("rm -f " .. filename)
end
os.execute("wget -fq " .. webserverAddress .. "/file/" .. filename .. " " .. filename)
end
end
actions.create = function(filename, isdir)
if not isdir then
if fs.exists(filename) then
os.execute("rm -f " .. filename)
end
os.execute("wget -fq " .. webserverAddress .. "/file/" .. filename .. " " .. filename)
else
os.execute("mkdir " .. filename)
end
end
actions.delete = function(filename, isdir)
if fs.exists(filename) then
os.execute("rm -rfd " .. filename)
end
end
while true do
local request = internet.request(webserverAddress .. "/heartbeat")
parse_result(request)
os.sleep(3)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment