Skip to content

Commit

Permalink
Merge branch 'nos'
Browse files Browse the repository at this point in the history
  • Loading branch information
deanpress committed Jan 27, 2020
2 parents c751fa2 + 35b9bcc commit 078e064
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 16 deletions.
16 changes: 10 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
{
"private": true,
"name": "@nosplatform/storage",
"description": "Provides SQLite + TypeORM storage for Ark Core",
"version": "0.0.2",
Expand Down Expand Up @@ -28,10 +27,10 @@
"updates": "../../node_modules/npm-check-updates/bin/npm-check-updates -a"
},
"dependencies": {
"@arkecosystem/core-container": "^2.6.0-next.2",
"@arkecosystem/core-http-utils": "^2.6.0-next.2",
"@arkecosystem/core-interfaces": "^2.6.0-next.2",
"@arkecosystem/crypto": "^2.6.0-next.2",
"@arkecosystem/core-container": "^2.6.0-next.9",
"@arkecosystem/core-http-utils": "^2.6.0-next.9",
"@arkecosystem/core-interfaces": "^2.6.0-next.9",
"@arkecosystem/crypto": "^2.6.0-next.9",
"bytebuffer": "^5.0.1",
"queue": "^6.0.1",
"reflect-metadata": "0.1.13",
Expand All @@ -54,7 +53,12 @@
"cross-env": "^6.0.0",
"del-cli": "^3.0.0"
},
"publishConfig": {
"access": "public"
},
"jest": {
"preset": "../../jest-preset.json"
"transform": {
"^.+\\.tsx?$": "ts-jest"
}
}
}
22 changes: 22 additions & 0 deletions src/entities/Round.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { BaseEntity, Column, Entity, PrimaryColumn } from "typeorm";

@Entity()
export class Round extends BaseEntity {
@PrimaryColumn("int")
public id: number;

@Column("varchar")
public topDelegates: string;

@Column("varchar")
public forged: string;

@Column("varchar")
public removed: string;

@Column("varchar")
public staked: string;

@Column("varchar")
public released: string;
}
48 changes: 46 additions & 2 deletions src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,32 @@ export const startServer = async config => {
const server = await createServer({
host: config.host,
port: config.port,
routes: {
cors: config.cors,
},
});

// Round Data
server.route({
method: "GET",
path: "/api/v1/round/{id}",
async handler(request, h) {
const id: number = Number(request.params.id);
const round = await Round.findOne({ id });
const response: any = round;
if (round) {
response.topDelegates = round.topDelegates.split(",");
return response;
} else {
return notFound();
}
},
});

// Statistics
server.route({
method: "GET",
path: "/stat/{name}",
path: "/api/v1/stat/{name}",
async handler(request, h) {
const stats = await Statistic.findOne({ name: request.params.name });
if (stats) {
Expand All @@ -22,5 +42,29 @@ export const startServer = async config => {
},
});

// Statistics
server.route({
method: "GET",
path: "/api/v1/stats",
async handler(request, h) {
const stats = await Statistic.find();
if (stats) {
let statsJson = {};
for (const stat of stats) {
if (String(stat.name).split('.').length > 1) {
const statArr = String(stat.name).split('.');
statsJson[statArr[0]] = {};
statsJson[statArr[0]][statArr[1]] = stat.value;
} else {
statsJson[stat.name] = stat.value;
}
}
return statsJson;
} else {
return notFound();
}
},
});

return mountServer("nOS Storage Server", server);
};
};
21 changes: 13 additions & 8 deletions src/storage.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { app } from "@arkecosystem/core-container";
import { Container, Logger } from "@arkecosystem/core-interfaces";
import { Managers } from "@arkecosystem/crypto";
import * as path from "path";
Expand All @@ -17,23 +16,26 @@ qp.concurrency = 1;
import { Statistic } from "./entities";

// Core plugins
const logger = app.resolvePlugin<Logger.ILogger>("logger");
const network = Managers.configManager.get("network");
const dbPath = path.resolve(__dirname, `../../storage/databases/${network.name}.sqlite`);


// Queue job plug-in, all functions that write to db should be wrapped in this.
qp.autostart = true;
export const q = async fn => {
qp.push(fn);
};

let server;

export const plugin: Container.IPluginDescriptor = {
pkg: require("../package.json"),
defaults,
alias: "storage",
async register(container: Container.IContainer, options) {
logger.info(`Registering Storage Plug-in.`);
logger.info(`Storage Plug-in Database Path: ${dbPath}`);
const network = Managers.configManager.get("network");
const dbPath = path.resolve(__dirname, `../../storage/databases/${network.name}.sqlite`);

container.resolvePlugin<Logger.ILogger>("logger").info(`Registering Storage Plug-in.`);
container.resolvePlugin<Logger.ILogger>("logger").info(`Storage Plug-in Database Path: ${dbPath}`);

await createConnection({
type: "sqlite",
Expand All @@ -43,10 +45,13 @@ export const plugin: Container.IPluginDescriptor = {
synchronize: true,
});

startServer({ host: "0.0.0.0", port: 2053 });
server = await startServer({ host: options.host, port: options.port, cors: options.cors });
},
async deregister(container: Container.IContainer, options) {
logger.info(`Deregistering Storage Plug-in.`);
container.resolvePlugin<Logger.ILogger>("logger").info(`Deregistering Storage Plug-in.`);
await server.stop();
container.resolvePlugin<Logger.ILogger>("logger").info(`Closed Storage API Server.`);
await getConnection().close();
container.resolvePlugin<Logger.ILogger>("logger").info(`Closed Storage Connection.`);
},
};

0 comments on commit 078e064

Please sign in to comment.