initial version woop woop
This commit is contained in:
commit
4a95f69e5b
1
Dockerfile
Normal file
1
Dockerfile
Normal file
@ -0,0 +1 @@
|
||||
FROM guard13007/docker-lapis:latest
|
45
app.moon
Normal file
45
app.moon
Normal file
@ -0,0 +1,45 @@
|
||||
lapis = require "lapis"
|
||||
|
||||
import Sessions, Deltas from require "models"
|
||||
import api, abort from require "helpers"
|
||||
|
||||
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) ->
|
||||
tab = {}
|
||||
for delta in *deltas
|
||||
tab[delta.type] = delta.time
|
||||
return tab if #tab > 0
|
||||
|
||||
result = (session, deltas) ->
|
||||
{
|
||||
id: session.id, time: session.updated_at - session.started_at,
|
||||
types: types deltas
|
||||
}
|
||||
|
||||
update = =>
|
||||
session, deltas = get(@params.id)
|
||||
session, delta = session\update(@params)
|
||||
table.insert deltas, delta if delta
|
||||
return result session, deltas
|
||||
|
||||
delete = =>
|
||||
session = get(@params.id)
|
||||
ok, err = session\delete!
|
||||
abort err unless ok
|
||||
return deleted: true
|
||||
|
||||
fns = {
|
||||
GET: => result get(@params.id)
|
||||
POST: => update(@)
|
||||
PUT: => update(@)
|
||||
DELETE: => delete(@)
|
||||
}
|
||||
|
||||
class extends lapis.Application
|
||||
[session: "/0/:id[a-fA-F%d]"]: api fns
|
||||
[new_session: "/0/new"]: api fns
|
12
config.moon
Normal file
12
config.moon
Normal file
@ -0,0 +1,12 @@
|
||||
config = require "lapis.config"
|
||||
|
||||
postgres_password = os.getenv "POSTGRES_PASSWORD"
|
||||
|
||||
config "development", ->
|
||||
postgres ->
|
||||
host "dt-db"
|
||||
user "postgres"
|
||||
database "postgres"
|
||||
password postgres_password
|
||||
num_workers 2
|
||||
code_cache "on"
|
8
create
Normal file
8
create
Normal file
@ -0,0 +1,8 @@
|
||||
#!/usr/bin/env bash
|
||||
docker build -t dt0 .
|
||||
docker network create dt-db
|
||||
docker run -d --restart always --name dt-db --network dt-db \
|
||||
-v "$PWD/db":/var/lib/postgresql/data postgres:10.5-alpine
|
||||
docker run -d --restart always --name dt0 --network dt-db \
|
||||
dt0:latest
|
||||
docker network connect web dt0
|
34
helpers.moon
Normal file
34
helpers.moon
Normal file
@ -0,0 +1,34 @@
|
||||
import json_params, capture_errors, yield_error, respond_to from require "lapis.application"
|
||||
import insert from table
|
||||
import max from math
|
||||
|
||||
api = (tab) ->
|
||||
json_params capture_errors {
|
||||
respond_to(tab),
|
||||
on_error: =>
|
||||
status = 400
|
||||
errors = {}
|
||||
for err in *@errors
|
||||
if "table" == type err
|
||||
status = max status, err[1]
|
||||
insert errors, err[2]
|
||||
else
|
||||
insert errors, err
|
||||
return(:status, json: { success: false, :errors })
|
||||
}
|
||||
|
||||
abort = (status, message) ->
|
||||
if message
|
||||
yield_error {status, message}
|
||||
else
|
||||
yield_error status
|
||||
|
||||
assert_model = (result, err) ->
|
||||
abort 500, err if err
|
||||
return result
|
||||
|
||||
{
|
||||
:api
|
||||
:abort
|
||||
:assert_model
|
||||
}
|
15
migrations.moon
Normal file
15
migrations.moon
Normal file
@ -0,0 +1,15 @@
|
||||
import create_table, types from require "lapis.db.schema"
|
||||
|
||||
{
|
||||
[1535648305]: =>
|
||||
create_table "sessions", {
|
||||
{"id", types.varchar primary_key: true, unique: true}
|
||||
{"started_at", types.double}
|
||||
{"updated_at", types.double}
|
||||
}
|
||||
create_table "deltas", {
|
||||
{"session_id", types.varchar primary_key: true}
|
||||
{"type", types.varchar}
|
||||
{"time", types.double}
|
||||
}
|
||||
}
|
4
models/Deltas.moon
Normal file
4
models/Deltas.moon
Normal file
@ -0,0 +1,4 @@
|
||||
import Model from require "lapis.db.model"
|
||||
|
||||
class Deltas extends Model
|
||||
@primary_key: "session_id"
|
33
models/Sessions.moon
Normal file
33
models/Sessions.moon
Normal file
@ -0,0 +1,33 @@
|
||||
import Model from require "lapis.db.model"
|
||||
|
||||
import Deltas from require "models"
|
||||
import bytes from require "resty.random"
|
||||
import to_hex from require "resty.string"
|
||||
import abort from require "helpers"
|
||||
|
||||
class Sessions extends Model
|
||||
@create: (input) =>
|
||||
id = input.id or to_hex bytes 16
|
||||
while Sessions\find id: id
|
||||
id = to_hex bytes 16
|
||||
|
||||
time = tonumber(input.started_at) or ngx.req.start_time!
|
||||
values = {
|
||||
started_at: time
|
||||
updated_at: time
|
||||
id: id
|
||||
}
|
||||
return super values
|
||||
|
||||
@update: (input) =>
|
||||
values = {
|
||||
started_at: tonumber(input.started_at) or nil
|
||||
updated_at: tonumber(input.updated_at) or ngx.req.start_time!
|
||||
}
|
||||
local delta
|
||||
if input.type
|
||||
delta, err = Deltas\create session_id: @id, type: input.type, time: values.updated_at - @updated_at
|
||||
unless delta
|
||||
-- abort "Invalid type (must be <=255 bytes)."
|
||||
abort "Failed to create delta: #{err}"
|
||||
return super(values), delta
|
Loading…
Reference in New Issue
Block a user