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
RUN luarocks install lapis-console

View File

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

View File

@ -2,17 +2,17 @@ import json_params, capture_errors, yield_error, respond_to from require "lapis.
import insert from table
import max from math
api = (tab) ->
api = (fn) =>
json_params capture_errors {
=>
result = respond_to(tab)
return json: result,
result = fn(@)
return json: result
on_error: =>
status = 400
status = 400 -- most likely a bad request
errors = {}
for err in *@errors
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]
else
insert errors, err

View File

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