Skip to content

Commit

Permalink
feat: massive update
Browse files Browse the repository at this point in the history
Signed-off-by: Ricardo Arturo Cabral Mejía <me@ricardocabral.io>
  • Loading branch information
cameri committed Feb 2, 2023
1 parent 5556184 commit f9c53ee
Show file tree
Hide file tree
Showing 80 changed files with 2,126 additions and 677 deletions.
7 changes: 0 additions & 7 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,6 @@ LABEL org.opencontainers.image.description="nostream"
LABEL org.opencontainers.image.authors="Ricardo Arturo Cabral Mejía"
LABEL org.opencontainers.image.licenses=MIT


ENV DB_HOST=localhost
ENV DB_PORT=5432
ENV DB_NAME=nostr-ts-relay
ENV DB_USER=nostr-ts-relay
ENV DB_PASSWORD=nostr-ts-relay

WORKDIR /app
RUN apk add --no-cache --update git

Expand Down
44 changes: 21 additions & 23 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
services:
relay:
nostream:
build: .
container_name: nostr-ts-relay
container_name: nostream
environment:
SECRET: changeme
RELAY_PORT: 8008
NOSTR_CONFIG_DIR: /home/node/
# Master
DB_HOST: db
NOSTR_CONFIG_DIR: /home/node/.nostr
DB_HOST: nostream-db
DB_PORT: 5432
DB_USER: nostr_ts_relay
DB_PASSWORD: nostr_ts_relay
Expand All @@ -26,7 +26,7 @@ services:
RR_DB_MAX_POOL_SIZE: 64
RR_DB_ACQUIRE_CONNECTION_TIMEOUT: 60000
# Redis
REDIS_HOST: cache
REDIS_HOST: nostream-cache
REDIS_PORT: 6379
REDIS_USER: default
REDIS_PASSWORD: nostr_ts_relay
Expand All @@ -38,39 +38,38 @@ services:
# DEBUG: "primary:*"
# DEBUG: "worker:*"
# DEBUG: "knex:query"
env_file:
- test.env
user: node:node
volumes:
- ${PWD}/.nostr:/home/node/
- ${PWD}/.nostr:/home/node/.nostr
ports:
- 8008:8008
depends_on:
cache:
nostream-cache:
condition: service_healthy
db:
nostream-db:
condition: service_healthy
migrations:
nostream-migrate:
condition: service_completed_successfully
restart: on-failure
networks:
default:
ipv4_address: 10.10.10.2
db:
nostream-db:
image: postgres
container_name: db
container_name: nostream-db
environment:
POSTGRES_DB: nostr_ts_relay
POSTGRES_USER: nostr_ts_relay
POSTGRES_PASSWORD: nostr_ts_relay
volumes:
- pgdata:/var/lib/postgresql/data-old
- ${PWD}/.nostr/data:/var/lib/postgresql/data
- ${PWD}/.nostr/db-logs:/var/log/postgresql
- ${PWD}/postgresql.conf:/postgresql.conf
ports:
- 15432:5432
networks:
default:
ipv4_address: 10.10.10.3
command: postgres -c 'config_file=/postgresql.conf'
restart: always
healthcheck:
Expand All @@ -79,26 +78,25 @@ services:
timeout: 5s
retries: 5
start_period: 360s
cache:
nostream-cache:
image: redis:7.0.5-alpine3.16
container_name: cache
container_name: nostream-cache
volumes:
- cache:/data
command: redis-server --loglevel warning --requirepass nostr_ts_relay
networks:
default:
ipv4_address: 10.10.10.4
restart: always
healthcheck:
test: [ "CMD", "redis-cli", "ping", "|", "grep", "PONG" ]
interval: 1s
timeout: 5s
retries: 5
migrations:
nostream-migrate:
image: node:18-alpine3.16
container_name: migrations
container_name: nostream-migrate
environment:
DB_HOST: db
DB_HOST: nostream-db
DB_PORT: 5432
DB_USER: nostr_ts_relay
DB_PASSWORD: nostr_ts_relay
Expand All @@ -111,19 +109,19 @@ services:
- ./migrations:/code/migrations
- ./knexfile.js:/code/knexfile.js
depends_on:
db:
nostream-db:
condition: service_healthy
networks:
default:
ipv4_address: 10.10.10.254

networks:
default:
name: nostr-ts-relay
name: nostream
ipam:
driver: default
config:
- subnet: 10.10.10.0/24

volumes:
cache:
pgdata:
2 changes: 1 addition & 1 deletion migrations/20230107_230900_create_invoices_table.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ exports.up = function (knex) {
table.bigint('amount_requested').unsigned().notNullable()
table.bigint('amount_paid').unsigned()
table.enum('unit', ['msats', 'sats', 'btc'])
table.enum('status', ['pending', 'completed'])
table.enum('status', ['pending', 'completed', 'expired'])
table.text('description')
table.datetime('confirmed_at', { useTz: false, precision: 3 })
table.datetime('expires_at', { useTz: false, precision: 3 })
Expand Down
13 changes: 13 additions & 0 deletions migrations/20230107_230900_create_users_table.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
exports.up = function (knex) {
return knex.schema.createTable('users', (table) => {
table.binary('pubkey').primary()
table.boolean('is_admitted').default(0)
table.bigint('balance').default(0)
table.datetime('tos_accepted_at', { useTz: false, precision: 3 })
table.timestamps(true, true, false)
})
}

exports.down = function (knex) {
return knex.schema.dropTable('users')
}
70 changes: 70 additions & 0 deletions migrations/20230118_190000_confirm_invoice_func.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
// Adapted from: https://github.com/stackernews/stacker.news
// Original Author: Keyan Kousha https://github.com/huumn
/**
MIT License
Copyright (c) 2023 Keyan Kousha / Stacker News
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
exports.up = async function (knex) {
return knex.schema
.raw(`create function now_utc() returns timestamp as $$
select now() at time zone 'utc';
$$ language sql;`)
.raw(`create function ASSERT_SERIALIZED() returns void as $$
BEGIN
IF (select current_setting('transaction_isolation') <> 'serializable') THEN
RAISE EXCEPTION 'SN_NOT_SERIALIZABLE';
END IF;
END;
$$ language plpgsql;
`)
.raw(
`CREATE OR REPLACE FUNCTION confirm_invoice(invoice_id UUID, amount_received BIGINT, confirmation_date TIMESTAMP WITHOUT TIME ZONE)
RETURNS INTEGER
LANGUAGE plpgsql
AS $$
DECLARE
payee BYTEA;
confirmed_date TIMESTAMP WITHOUT TIME ZONE;
BEGIN
PERFORM ASSERT_SERIALIZED();
SELECT "pubkey", "confirmed_at" INTO payee, confirmed_date FROM "invoices" WHERE id = invoice_id;
IF confirmed_date IS NULL THEN
UPDATE invoices
SET
"confirmed_at" = confirmation_date,
"amount_paid" = amount_received,
"updated_at" = now_utc()
WHERE id = invoice_id;
UPDATE users SET balance = balance + amount_received WHERE "pubkey" = payee;
END IF;
RETURN 0;
END;
$$;`)
}

exports.down = function (knex) {
return knex.schema
.raw('DROP FUNCTION IF EXISTS confirm_invoice(UUID, BYTEA, TIMESTAMP);')
.raw('DROP FUNCTION IF EXISTS ASSERT_SERIALIZED();')
.raw('DROP FUNCTION IF EXISTS now_utc();')
}
14 changes: 14 additions & 0 deletions migrations/20230119_170900_add_kind_tags_created_at_index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
exports.up = async function (knex) {
return knex.schema
.raw('CREATE EXTENSION btree_gin;')
.raw(
`CREATE INDEX kind_tags_created_at_idx
ON events USING GIN ( event_kind, event_tags, event_created_at );`,
)
}

exports.down = function (knex) {
return knex.schema
.raw('DROP INDEX IF EXISTS kind_tags_created_at_idx;')
.raw('DROP EXTENSION btree_gin;')
}
27 changes: 27 additions & 0 deletions migrations/20230120_161800_charge_user_func.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
exports.up = async function (knex) {
return knex.schema
.raw(
`CREATE OR REPLACE FUNCTION charge_user(charged_user BYTEA, amount BIGINT)
RETURNS INTEGER
LANGUAGE plpgsql
AS $$
DECLARE
current_balance BIGINT;
BEGIN
PERFORM ASSERT_SERIALIZED();
SELECT "balance" INTO current_balance FROM "users" WHERE "pubkey" = charged_user;
IF current_balance - amount >= 0 THEN
UPDATE "users" SET balance = balance - amount WHERE "pubkey" = charged_user;
RETURN 1;
ELSE
RETURN 0;
END IF;
END;
$$;`)
}

exports.down = function (knex) {
return knex.schema
.raw('DROP FUNCTION IF EXISTS charge_user(BYTEA, BIGINT);')
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
exports.up = function (knex) {
return knex.raw('ALTER TABLE events ADD remote_address inet NULL;')
}

exports.down = function (knex) {
return knex.schema.alterTable('events', function (table) {
table.dropColumn('remote_address')
})
}

0 comments on commit f9c53ee

Please sign in to comment.