Created
July 29, 2020 12:31
-
-
Save Kristopher38/5a36d7f91ac6cac218dab85a16e059f1 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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