bug fixes, notes, removed non-functioning console

This commit is contained in:
Paul Liverman III 2018-09-16 12:03:56 -07:00
parent d1ec3d782f
commit fa116e3650
4 changed files with 27 additions and 19 deletions

View File

@ -1,3 +1 @@
FROM guard13007/docker-lapis:latest FROM guard13007/docker-lapis:latest
RUN luarocks install lapis-console

View File

@ -1,49 +1,55 @@
lapis = require "lapis" lapis = require "lapis"
console = require "lapis.console"
import Sessions, Deltas from require "models" import Sessions, Deltas from require "models"
import api, abort from require "helpers" import api, abort from require "helpers"
-- returns a Session (creating if needed) and list of Deltas
get = (id="") -> get = (id="") ->
if session = Sessions\find(:id) or Sessions\create(:id) if session = Sessions\find(:id) or Sessions\create(:id)
return session, Deltas\select "WHERE session_id = ?", session.id return session, Deltas\select "WHERE session_id = ?", session.id
else else
abort "Invalid session ID." abort "Invalid session ID."
types = (deltas) -> -- returns a hash table of accumulated time from a list of Deltas
types = (deltas={}) ->
tab = {} tab = {}
for delta in *deltas for delta in *deltas
if tab[delta.type]
tab[delta.type] += delta.time
else
tab[delta.type] = delta.time tab[delta.type] = delta.time
return tab if #tab > 0 return tab if #tab > 0
-- converts Session and Deltas list into a returnable result
result = (session, deltas) -> result = (session, deltas) ->
{ {
id: session.id, time: session.updated_at - session.started_at, id: session.id, time: session.updated_at - session.started_at,
types: types deltas types: types deltas
} }
-- core function of an update (POST or PUT) request
update = => update = =>
session, deltas = get(@params.id) session, deltas = get(@params.id)
session, delta = session\update(@params) session, delta = session\update(@params)
table.insert deltas, delta if delta table.insert deltas, delta if delta
return result session, deltas return result session, deltas
-- core function of a DELETE request
delete = => delete = =>
session = get(@params.id) session = get(@params.id)
ok, err = session\delete! ok, err = session\delete!
abort err unless ok abort err unless ok
return deleted: true return deleted: true
-- all request methods are technically usable (not not neccessarily useful) on both routes
fns = { fns = {
GET: => result get(@params.id) GET: api(=> result get(@params.id))
POST: => update(@) POST: api(=> update(@))
PUT: => update(@) PUT: api(=> update(@))
DELETE: => delete(@) DELETE: api(=> delete(@))
} }
class extends lapis.Application class extends lapis.Application
[console: "/console/#{config.secret}"]: => -- can access a session by its id in the URL, or generate one with /0/new
return console.make(env: "all")(@) [session: "/0/:id[a-fA-F%d]"]: fns
[new_session: "/0/new"]: fns
[session: "/0/:id[a-fA-F%d]"]: api fns
[new_session: "/0/new"]: api fns

View File

@ -2,17 +2,17 @@ import json_params, capture_errors, yield_error, respond_to from require "lapis.
import insert from table import insert from table
import max from math import max from math
api = (tab) -> api = (fn) =>
json_params capture_errors { json_params capture_errors {
=> =>
result = respond_to(tab) result = fn(@)
return json: result, return json: result
on_error: => on_error: =>
status = 400 status = 400 -- most likely a bad request
errors = {} errors = {}
for err in *@errors for err in *@errors
if "table" == type err if "table" == type err
status = max status, err[1] status = max status, err[1] -- the worst error will have a higher status number
insert errors, err[2] insert errors, err[2]
else else
insert errors, err insert errors, err

View File

@ -7,10 +7,12 @@ import abort from require "helpers"
class Sessions extends Model class Sessions extends Model
@create: (input) => @create: (input) =>
-- make sure a unique id is generated
id = input.id or to_hex bytes 16 id = input.id or to_hex bytes 16
while Sessions\find id: id while Sessions\find id: id
id = to_hex bytes 16 id = to_hex bytes 16
-- take user input time or when this request started processing
time = tonumber(input.started_at) or ngx.req.start_time! time = tonumber(input.started_at) or ngx.req.start_time!
values = { values = {
started_at: time started_at: time
@ -20,6 +22,8 @@ class Sessions extends Model
return super values return super values
@update: (input) => @update: (input) =>
-- user can manually change started_at and updated_at if they want
-- this will potentially invalidate data in the Deltas
values = { values = {
started_at: tonumber(input.started_at) or nil started_at: tonumber(input.started_at) or nil
updated_at: tonumber(input.updated_at) or ngx.req.start_time! updated_at: tonumber(input.updated_at) or ngx.req.start_time!