Skip to content

Commit

Permalink
refactor(core-api): adapt to new container
Browse files Browse the repository at this point in the history
  • Loading branch information
Brian Faust committed Sep 2, 2019
1 parent 0c03165 commit 67b10dd
Show file tree
Hide file tree
Showing 19 changed files with 390 additions and 640 deletions.
127 changes: 67 additions & 60 deletions packages/core-api/src/defaults.ts
Original file line number Diff line number Diff line change
@@ -1,65 +1,72 @@
export const defaults = {
enabled: !process.env.CORE_API_DISABLED,
host: process.env.CORE_API_HOST || "0.0.0.0",
port: process.env.CORE_API_PORT || 4003,
cache: {
enabled: true,
/**
* How many seconds the server will try to complete the request and cache the result.
*
* Defaults to 8 seconds, set it to false if you do not care about the timeout.
*
* Setting it to false can result in requests never being completed, which is usually
* caused by low-spec servers that are unable to handle the heavy load that results
* out of SQL queries on the blocks and transactions tables.
*
* If you experience issues with the cache timeout, which is indicated by a 503 status codes,
* you should consider upgrading your hardware or tweak your PostgreSQL settings.
*/
generateTimeout: process.env.CORE_API_CACHE_TIMEOUT || 8000,
},
// @see https://hapijs.com/api#-serveroptionstls
ssl: {
enabled: process.env.CORE_API_SSL,
host: process.env.CORE_API_SSL_HOST || "0.0.0.0",
port: process.env.CORE_API_SSL_PORT || 8443,
key: process.env.CORE_API_SSL_KEY,
cert: process.env.CORE_API_SSL_CERT,
},
// @see https://github.com/wraithgar/hapi-rate-limit
rateLimit: {
enabled: !process.env.CORE_API_RATE_LIMIT,
pathLimit: false,
userLimit: process.env.CORE_API_RATE_LIMIT_USER_LIMIT || 300,
userCache: {
expiresIn: process.env.CORE_API_RATE_LIMIT_USER_EXPIRES || 60000,
server: {
http: {
enabled: !process.env.CORE_API_DISABLED,
host: process.env.CORE_API_HOST || "0.0.0.0",
port: process.env.CORE_API_PORT || 4003,
},
// @see https://hapijs.com/api#-serveroptionstls
https: {
enabled: process.env.CORE_API_SSL,
host: process.env.CORE_API_SSL_HOST || "0.0.0.0",
port: process.env.CORE_API_SSL_PORT || 8443,
tls: {
key: process.env.CORE_API_SSL_KEY,
cert: process.env.CORE_API_SSL_CERT,
},
},
},
// @see https://github.com/fknop/hapi-pagination
pagination: {
limit: 100,
include: [
"/api/blocks",
"/api/blocks/{id}/transactions",
"/api/blocks/search",
"/api/delegates",
"/api/delegates/{id}/blocks",
"/api/delegates/{id}/voters",
"/api/delegates/search",
"/api/peers",
"/api/transactions",
"/api/transactions/search",
"/api/transactions/unconfirmed",
"/api/votes",
"/api/wallets",
"/api/wallets/top",
"/api/wallets/{id}/transactions",
"/api/wallets/{id}/transactions/received",
"/api/wallets/{id}/transactions/sent",
"/api/wallets/{id}/votes",
"/api/wallets/search",
],
plugins: {
cache: {
enabled: true,
/**
* How many seconds the server will try to complete the request and cache the result.
*
* Defaults to 8 seconds, set it to false if you do not care about the timeout.
*
* Setting it to false can result in requests never being completed, which is usually
* caused by low-spec servers that are unable to handle the heavy load that results
* out of SQL queries on the blocks and transactions tables.
*
* If you experience issues with the cache timeout, which is indicated by a 503 status codes,
* you should consider upgrading your hardware or tweak your PostgreSQL settings.
*/
generateTimeout: process.env.CORE_API_CACHE_TIMEOUT || 8000,
},
// @see https://github.com/wraithgar/hapi-rate-limit
rateLimit: {
enabled: !process.env.CORE_API_RATE_LIMIT,
pathLimit: false,
userLimit: process.env.CORE_API_RATE_LIMIT_USER_LIMIT || 300,
userCache: {
expiresIn: process.env.CORE_API_RATE_LIMIT_USER_EXPIRES || 60000,
},
},
// @see https://github.com/fknop/hapi-pagination
pagination: {
limit: 100,
include: [
"/api/blocks",
"/api/blocks/{id}/transactions",
"/api/blocks/search",
"/api/delegates",
"/api/delegates/{id}/blocks",
"/api/delegates/{id}/voters",
"/api/delegates/search",
"/api/peers",
"/api/transactions",
"/api/transactions/search",
"/api/transactions/unconfirmed",
"/api/votes",
"/api/wallets",
"/api/wallets/top",
"/api/wallets/{id}/transactions",
"/api/wallets/{id}/transactions/received",
"/api/wallets/{id}/transactions/sent",
"/api/wallets/{id}/votes",
"/api/wallets/search",
],
},
whitelist: ["*"],
},
whitelist: ["*"],
plugins: [],
};
44 changes: 25 additions & 19 deletions packages/core-api/src/handlers/blocks/methods.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,21 @@ import Boom from "@hapi/boom";
import { ServerCache } from "../../services";
import { paginate, respondWithResource, toPagination } from "../utils";

const databaseService = app.get<Contracts.Database.DatabaseService>(Container.Identifiers.DatabaseService);
const blocksRepository = databaseService.blocksBusinessRepository;
const transactionsRepository = databaseService.transactionsBusinessRepository;

const index = async request => {
const blocks = await blocksRepository.search({
...request.query,
...paginate(request),
});
const blocks = await app
.get<Contracts.Database.DatabaseService>(Container.Identifiers.DatabaseService)
.blocksBusinessRepository.search({
...request.query,
...paginate(request),
});

return toPagination(blocks, "block", request.query.transform);
};

const show = async request => {
const block = await blocksRepository.findByIdOrHeight(request.params.id);
const block = await app
.get<Contracts.Database.DatabaseService>(Container.Identifiers.DatabaseService)
.blocksBusinessRepository.findByIdOrHeight(request.params.id);

if (!block) {
return Boom.notFound("Block not found");
Expand All @@ -28,26 +28,32 @@ const show = async request => {
};

const transactions = async request => {
const block = await blocksRepository.findByIdOrHeight(request.params.id);
const block = await app
.get<Contracts.Database.DatabaseService>(Container.Identifiers.DatabaseService)
.blocksBusinessRepository.findByIdOrHeight(request.params.id);

if (!block) {
return Boom.notFound("Block not found");
}

const rows = await transactionsRepository.findAllByBlock(block.id, {
...request.query,
...paginate(request),
});
const rows = await app
.get<Contracts.Database.DatabaseService>(Container.Identifiers.DatabaseService)
.transactionsBusinessRepository.findAllByBlock(block.id, {
...request.query,
...paginate(request),
});

return toPagination(rows, "transaction", request.query.transform);
};

const search = async request => {
const blocks = await blocksRepository.search({
...request.payload,
...request.query,
...paginate(request),
});
const blocks = await app
.get<Contracts.Database.DatabaseService>(Container.Identifiers.DatabaseService)
.blocksBusinessRepository.search({
...request.payload,
...request.query,
...paginate(request),
});

return toPagination(blocks, "block", request.query.transform);
};
Expand Down
51 changes: 31 additions & 20 deletions packages/core-api/src/handlers/delegates/methods.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,21 @@ import Boom from "@hapi/boom";
import { ServerCache } from "../../services";
import { paginate, respondWithResource, toPagination } from "../utils";

const databaseService = app.get<Contracts.Database.DatabaseService>(Container.Identifiers.DatabaseService);
const blocksRepository = databaseService.blocksBusinessRepository;

const index = async request => {
const delegates = databaseService.delegates.search({
...request.query,
...paginate(request),
});
const delegates = app
.get<Contracts.Database.DatabaseService>(Container.Identifiers.DatabaseService)
.delegates.search({
...request.query,
...paginate(request),
});

return toPagination(delegates, "delegate");
};

const show = async request => {
const delegate = databaseService.delegates.findById(request.params.id);
const delegate = app
.get<Contracts.Database.DatabaseService>(Container.Identifiers.DatabaseService)
.delegates.findById(request.params.id);

if (!delegate) {
return Boom.notFound("Delegate not found");
Expand All @@ -27,38 +28,48 @@ const show = async request => {
};

const search = async request => {
const delegates = databaseService.delegates.search({
...request.payload,
...request.query,
...paginate(request),
});
const delegates = app
.get<Contracts.Database.DatabaseService>(Container.Identifiers.DatabaseService)
.delegates.search({
...request.payload,
...request.query,
...paginate(request),
});

return toPagination(delegates, "delegate");
};

const blocks = async request => {
const delegate = databaseService.delegates.findById(request.params.id);
const delegate = app
.get<Contracts.Database.DatabaseService>(Container.Identifiers.DatabaseService)
.delegates.findById(request.params.id);

if (!delegate) {
return Boom.notFound("Delegate not found");
}

const rows = await blocksRepository.findAllByGenerator(delegate.publicKey, paginate(request));
const rows = await app
.get<Contracts.Database.DatabaseService>(Container.Identifiers.DatabaseService)
.blocksBusinessRepository.findAllByGenerator(delegate.publicKey, paginate(request));

return toPagination(rows, "block", request.query.transform);
};

const voters = async request => {
const delegate = databaseService.delegates.findById(request.params.id);
const delegate = app
.get<Contracts.Database.DatabaseService>(Container.Identifiers.DatabaseService)
.delegates.findById(request.params.id);

if (!delegate) {
return Boom.notFound("Delegate not found");
}

const wallets = databaseService.wallets.findAllByVote(delegate.publicKey, {
...request.query,
...paginate(request),
});
const wallets = app
.get<Contracts.Database.DatabaseService>(Container.Identifiers.DatabaseService)
.wallets.findAllByVote(delegate.publicKey, {
...request.query,
...paginate(request),
});

return toPagination(wallets, "wallet");
};
Expand Down
7 changes: 3 additions & 4 deletions packages/core-api/src/handlers/rounds/methods.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,9 @@ import { ServerCache } from "../../services";
import { respondWithCollection } from "../utils";

const delegates = async request => {
const databaseService = app.get<Contracts.Database.DatabaseService>(Container.Identifiers.DatabaseService);
const roundsRepository = databaseService.connection.roundsRepository;

const delegates = await roundsRepository.findById(request.params.id);
const delegates = await app
.get<Contracts.Database.DatabaseService>(Container.Identifiers.DatabaseService)
.connection.roundsRepository.findById(request.params.id);

if (!delegates || !delegates.length) {
return Boom.notFound("Round not found");
Expand Down
7 changes: 4 additions & 3 deletions packages/core-api/src/handlers/shared/schemas/pagination.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { app } from "@arkecosystem/core-kernel";
// import { app } from "@arkecosystem/core-kernel";
import Joi from "@hapi/joi";

export const pagination = {
Expand All @@ -10,6 +10,7 @@ export const pagination = {
.min(0),
limit: Joi.number()
.integer()
.min(1)
.max(app.get<any>("api.options").pagination.limit),
.min(1),
// @fixme: the container is not available at the time this file is loaded
// .max(app.get<any>("api.options").get("plugins.pagination.limit")),
};
29 changes: 16 additions & 13 deletions packages/core-api/src/handlers/transactions/methods.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,21 @@ import Boom from "@hapi/boom";
import { ServerCache } from "../../services";
import { paginate, respondWithResource, toPagination } from "../utils";

const transactionsRepository = app.get<Contracts.Database.DatabaseService>(Container.Identifiers.DatabaseService)
.transactionsBusinessRepository;

const index = async request => {
const transactions = await transactionsRepository.search({
...request.query,
...paginate(request),
});
const transactions = await app
.get<Contracts.Database.DatabaseService>(Container.Identifiers.DatabaseService)
.transactionsBusinessRepository.search({
...request.query,
...paginate(request),
});

return toPagination(transactions, "transaction", (request.query.transform as unknown) as boolean);
};

const show = async request => {
const transaction = await transactionsRepository.findById(request.params.id);
const transaction = await app
.get<Contracts.Database.DatabaseService>(Container.Identifiers.DatabaseService)
.transactionsBusinessRepository.findById(request.params.id);

if (!transaction) {
return Boom.notFound("Transaction not found");
Expand All @@ -27,11 +28,13 @@ const show = async request => {
};

const search = async request => {
const transactions = await transactionsRepository.search({
...request.query,
...request.payload,
...paginate(request),
});
const transactions = await app
.get<Contracts.Database.DatabaseService>(Container.Identifiers.DatabaseService)
.transactionsBusinessRepository.search({
...request.query,
...request.payload,
...paginate(request),
});

return toPagination(transactions, "transaction", (request.query.transform as unknown) as boolean);
};
Expand Down
6 changes: 4 additions & 2 deletions packages/core-api/src/handlers/transactions/schema.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { app } from "@arkecosystem/core-kernel";
// import { app } from "@arkecosystem/core-kernel";
import Joi from "@hapi/joi";

import { blockId } from "../shared/schemas/block-id";
Expand Down Expand Up @@ -55,7 +55,9 @@ export const store: object = {
transactions: {
$ref: "transactions",
minItems: 1,
maxItems: app.get<any>("transactionPool.options").maxTransactionsPerRequest,
maxItems: 40,
// @fixme: the container is not available at the time this file is loaded
// maxItems: app.get<any>("transactionPool.options").maxTransactionsPerRequest,
},
},
};
Expand Down
Loading

0 comments on commit 67b10dd

Please sign in to comment.