incomplete v1

This commit is contained in:
Paul Liverman III 2018-09-03 10:38:10 -07:00
commit ee896cd1de
7 changed files with 110 additions and 0 deletions

3
Dockerfile Normal file
View File

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

37
app.moon Normal file
View File

@ -0,0 +1,37 @@
lapis = require "lapis"
bcrypt = require "bcrypt"
config = require("lapis.config").get!
import Users from require "models"
import api, abort, assert_model from require "helpers"
class extends lapis.Application
[authenticate: "/0/auth"]: api {
POST: =>
local user
if @params.name
user = Users\find name: @params.name
elseif @params.id
user = Users\find id: @params.id
abort "No such user." unless user
if user
unless bcrypt.verify(@params.password, user.digest)
abort "Incorrect password."
elseif @params.password
-- TODO create user with specified password
-- TODO constraints on password for security purposes
user = assert_model Users\create {
name: @params.name
digest: bcrypt.digest(@params.password, config.digest_rounds)
}
return name: user.name, id: user.id
}
[name: "/0/:id[%d]"]: api {
GET: =>
if user = Users\find id: @params.id
return name: user.name
else
abort "No such user."
}

14
config.moon Normal file
View File

@ -0,0 +1,14 @@
config = require "lapis.config"
postgres_password = os.getenv "POSTGRES_PASSWORD"
config "development", ->
postgres ->
host "passwd-db"
user "postgres"
database "postgres"
password postgres_password
num_workers 2
code_cache "on"
digest_rounds 12

8
create Executable file
View File

@ -0,0 +1,8 @@
#!/usr/bin/env bash
docker build -t passwd0 .
docker network create passwd-db
docker run -d --restart always --name passwd-db --network passwd-db \
-v "$PWD/db":/var/lib/postgresql/data postgres:10.5-alpine
docker run -d --restart always --name passwd0 --network passwd-db \
passwd0:latest
docker network connect passwd0 web

34
helpers.moon Normal file
View 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
}

10
migrations.moon Normal file
View File

@ -0,0 +1,10 @@
import create_table, types from require "lapis.db.schema"
{
[1535780489]: =>
create_table "users", {
{"id", types.serial primary_key: true}
{"name", types.varchar unique: true}
{"digest", types.text}
}
}

4
models/Users.moon Normal file
View File

@ -0,0 +1,4 @@
import Model from require "lapis.db.model"
class Users extends Model
-- TODO constraints on usernames under 256 bytes, alphanumerics only