🧱 update CI workflow and remove alpinejs frontend files
This commit is contained in:
@@ -94,7 +94,7 @@ end
|
||||
-- Seed initial data
|
||||
local function seed_db()
|
||||
db.with_connection(function(conn)
|
||||
local res, err = conn:query("SELECT COUNT(*) as total FROM devices")
|
||||
local res = conn:query("SELECT COUNT(*) as total FROM devices")
|
||||
local count = 0
|
||||
if res and res[1] then
|
||||
count = tonumber(res[1].total) or 0
|
||||
@@ -137,7 +137,8 @@ local function seed_db()
|
||||
|
||||
for _, device in ipairs(devices) do
|
||||
conn:query(
|
||||
"INSERT INTO devices (name, manufacturer, release_year, cpu, ram_mb, storage_mb, display_size, battery_hours) VALUES ($1, $2, $3, $4, $5, $6, $7, $8)",
|
||||
"INSERT INTO devices (name, manufacturer, release_year, cpu, ram_mb, storage_mb, "
|
||||
.. "display_size, battery_hours) VALUES ($1, $2, $3, $4, $5, $6, $7, $8)",
|
||||
device.name,
|
||||
device.manufacturer,
|
||||
device.release_year,
|
||||
@@ -205,17 +206,17 @@ end
|
||||
|
||||
local function b64(data)
|
||||
-- Minimal Base64
|
||||
local b='ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'
|
||||
return ((data:gsub('.', function(x)
|
||||
local r,b='',x:byte()
|
||||
for i=8,1,-1 do r=r..(b%2^i-b%2^(i-1)>0 and '1' or '0') end
|
||||
return r;
|
||||
end)..'0000'):gsub('%d%d%d?%d?%d?%d?', function(x)
|
||||
local b = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'
|
||||
return ((data:gsub('.', function(x)
|
||||
local r, byte = '', x:byte()
|
||||
for i = 8, 1, -1 do r = r .. (byte % 2^i - byte % 2^(i - 1) > 0 and '1' or '0') end
|
||||
return r
|
||||
end) .. '0000'):gsub('%d%d%d?%d?%d?%d?', function(x)
|
||||
if (#x < 6) then return '' end
|
||||
local c=0
|
||||
for i=1,6 do c=c+(x:sub(i,i)=='1' and 2^(6-i) or 0) end
|
||||
return b:sub(c+1,c+1)
|
||||
end)..({ '', '==', '=' })[#data%3+1])
|
||||
return b:sub(c + 1, c + 1)
|
||||
end) .. ({ '', '==', '=' })[#data % 3 + 1])
|
||||
end
|
||||
|
||||
local function encode_ws_frame(payload)
|
||||
@@ -227,11 +228,11 @@ local function encode_ws_frame(payload)
|
||||
header = header .. string.char(126) .. string.char(math.floor(len / 256)) .. string.char(len % 256)
|
||||
else
|
||||
-- 64-bit length not implemented for simplicity
|
||||
header = header .. string.char(127) .. string.rep(string.char(0), 4) ..
|
||||
string.char(math.floor(len / 16777216) % 256) ..
|
||||
string.char(math.floor(len / 65536) % 256) ..
|
||||
string.char(math.floor(len / 256) % 256) ..
|
||||
string.char(len % 256)
|
||||
header = header .. string.char(127) .. string.rep(string.char(0), 4)
|
||||
.. string.char(math.floor(len / 16777216) % 256)
|
||||
.. string.char(math.floor(len / 65536) % 256)
|
||||
.. string.char(math.floor(len / 256) % 256)
|
||||
.. string.char(len % 256)
|
||||
end
|
||||
return header .. payload
|
||||
end
|
||||
@@ -275,7 +276,8 @@ end
|
||||
function Device.create(data, request_id)
|
||||
local res = db.with_connection(function(conn)
|
||||
return conn:query(
|
||||
"INSERT INTO devices (name, manufacturer, release_year, cpu, ram_mb, storage_mb, display_size, battery_hours) VALUES ($1, $2, $3, $4, $5, $6, $7, $8) RETURNING id",
|
||||
"INSERT INTO devices (name, manufacturer, release_year, cpu, ram_mb, storage_mb, "
|
||||
.. "display_size, battery_hours) VALUES ($1, $2, $3, $4, $5, $6, $7, $8) RETURNING id",
|
||||
data.name,
|
||||
data.manufacturer,
|
||||
(not is_json_null(data.release_year)) and tonumber(data.release_year) or nil,
|
||||
@@ -302,14 +304,30 @@ function Device.update(id, data, request_id)
|
||||
db.with_connection(function(conn)
|
||||
local updates = {}
|
||||
local pg = conn
|
||||
if not is_json_null(data.name) then table.insert(updates, "name = " .. pg:escape_literal(data.name)) end
|
||||
if not is_json_null(data.manufacturer) then table.insert(updates, "manufacturer = " .. pg:escape_literal(data.manufacturer)) end
|
||||
if not is_json_null(data.release_year) then table.insert(updates, "release_year = " .. tonumber(data.release_year)) end
|
||||
if not is_json_null(data.cpu) then table.insert(updates, "cpu = " .. pg:escape_literal(data.cpu)) end
|
||||
if not is_json_null(data.ram_mb) then table.insert(updates, "ram_mb = " .. tonumber(data.ram_mb)) end
|
||||
if not is_json_null(data.storage_mb) then table.insert(updates, "storage_mb = " .. tonumber(data.storage_mb)) end
|
||||
if not is_json_null(data.display_size) then table.insert(updates, "display_size = " .. pg:escape_literal(data.display_size)) end
|
||||
if not is_json_null(data.battery_hours) then table.insert(updates, "battery_hours = " .. tonumber(data.battery_hours)) end
|
||||
if not is_json_null(data.name) then
|
||||
table.insert(updates, "name = " .. pg:escape_literal(data.name))
|
||||
end
|
||||
if not is_json_null(data.manufacturer) then
|
||||
table.insert(updates, "manufacturer = " .. pg:escape_literal(data.manufacturer))
|
||||
end
|
||||
if not is_json_null(data.release_year) then
|
||||
table.insert(updates, "release_year = " .. tonumber(data.release_year))
|
||||
end
|
||||
if not is_json_null(data.cpu) then
|
||||
table.insert(updates, "cpu = " .. pg:escape_literal(data.cpu))
|
||||
end
|
||||
if not is_json_null(data.ram_mb) then
|
||||
table.insert(updates, "ram_mb = " .. tonumber(data.ram_mb))
|
||||
end
|
||||
if not is_json_null(data.storage_mb) then
|
||||
table.insert(updates, "storage_mb = " .. tonumber(data.storage_mb))
|
||||
end
|
||||
if not is_json_null(data.display_size) then
|
||||
table.insert(updates, "display_size = " .. pg:escape_literal(data.display_size))
|
||||
end
|
||||
if not is_json_null(data.battery_hours) then
|
||||
table.insert(updates, "battery_hours = " .. tonumber(data.battery_hours))
|
||||
end
|
||||
|
||||
if #updates > 0 then
|
||||
table.insert(updates, "updated_at = CURRENT_TIMESTAMP")
|
||||
@@ -370,7 +388,7 @@ local function parse_headers(client)
|
||||
-- Use a small timeout for individual header lines to handle slow clients
|
||||
-- or cases where headers are partially sent.
|
||||
client:settimeout(0.1)
|
||||
local line, err = client:receive("*l")
|
||||
local line = client:receive("*l")
|
||||
if not line or line == "" then break end
|
||||
local key, value = line:match("^([^:]+):%s*(.*)$")
|
||||
if key then
|
||||
@@ -588,7 +606,7 @@ function app.start()
|
||||
local red = get_redis_connection()
|
||||
if red then
|
||||
-- Note: redis-lua's subscribe() sets the connection into a subscription state.
|
||||
local ok, err = pcall(function() return red:subscribe("devices:events") end)
|
||||
local ok, err = pcall(function() return red:subscribe("devices:events") end)
|
||||
if ok then
|
||||
-- Set the underlying socket to a very small timeout for non-blocking feel
|
||||
pcall(function()
|
||||
@@ -626,9 +644,8 @@ function app.start()
|
||||
for i, c in ipairs(clients) do
|
||||
local line, err = c.socket:receive("*l")
|
||||
if line then
|
||||
local method, full_path = parse_request(line)
|
||||
local headers = parse_headers(c.socket)
|
||||
|
||||
|
||||
if headers["upgrade"] == "websocket" then
|
||||
-- Handle WebSocket Handshake
|
||||
local key = headers["sec-websocket-key"]
|
||||
|
||||
Reference in New Issue
Block a user