Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add ssl support to knex #47

Merged
merged 2 commits into from
Nov 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 18 additions & 1 deletion apps/backend/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,27 @@

Backend contains database migrations 📦 and cron jobs 🕒 to fetch and analyze statistics 📊

### Config

```
DATABASE_URL=
RPC_URL=
NETWORK=
COINGECKO_API_KEY=
COINMARKETCAP_API_KEY=
LIVECOINWATCH_API_KEY=

# Optional
DATABASE_CA=
DATABASE_CERT=
DATABASE_KEY=
SENTRY_DSN=
```

### Migrations

Migrations 📦 can be applied by accessing the Docker container 🐳 and executing the following command

```
cd apps/backend & yarn migrate
cd apps/backend && yarn migrate
```
15 changes: 14 additions & 1 deletion apps/backend/knexfile.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,20 @@
const ssl = {
rejectUnauthorized: true,
};

if (process.env.DATABASE_CA) {
ssl.ca = Buffer.from(process.env.DATABASE_CA, 'base64').toString('utf-8');
ssl.cert = Buffer.from(process.env.DATABASE_CERT, 'base64').toString('utf-8');
ssl.key = Buffer.from(process.env.DATABASE_KEY, 'base64').toString('utf-8');
}

export default {
production: {
client: 'postgresql',
connection: process.env.DATABASE_URL,
connection: {
connectionString: process.env.DATABASE_URL,
ssl: ssl?.ca ? ssl : false,
},
migrations: {
directory: './migrations',
tableName: 'knex_migrations',
Expand Down
24 changes: 11 additions & 13 deletions apps/backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,28 +20,26 @@
"seed": "NODE_ENV=production knex seed:run"
},
"dependencies": {
"@sentry/node": "7.10.0",
"@sentry/tracing": "7.10.0",
"axios": "1.6.0",
"big.js": "6.2.1",
"@sentry/node": "7.74.1",
"axios": "1.6.2",
"bree": "9.1.2",
"dayjs": "1.11.4",
"knex": "2.1.0",
"envalid": "8.0.0",
"lodash-es": "4.17.21",
"p-map": "5.5.0",
"pg": "8.7.3",
"pino": "8.1.0"
"p-map": "5.5.0"
},
"devDependencies": {
"@types/big.js": "~6.1",
"@types/lodash-es": "~4.17",
"@types/node": "~16",
"@types/node": "~20.8",
"eslint-config-custom-node": "*",
"knex-migrate-sql-file": "~2.0",
"nb-knex": "*",
"nb-logger": "*",
"nb-near": "*",
"nb-tsconfig": "*",
"nb-types": "*",
"nb-utils": "*",
"nodemon": "~2.0",
"pino-pretty": "~8.1",
"rimraf": "~3.0",
"rimraf": "~5.0",
"typescript": "~5.2"
}
}
47 changes: 22 additions & 25 deletions apps/backend/src/config.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,20 @@
import log from '#libs/log';
import { cleanEnv, str } from 'envalid';

import { Network } from '#types/enums';
import { Config } from '#types/types';

const dbUrl = process.env.DATABASE_URL;
const rpcUrl = process.env.RPC_URL;
const cmcApiKey = process.env.COINMARKETCAP_API_KEY;
const lcwApiKey = process.env.LIVECOINWATCH_API_KEY;
const coingeckoApiKey = process.env.COINGECKO_API_KEY;

if (!dbUrl || !rpcUrl || !cmcApiKey || !lcwApiKey || !coingeckoApiKey) {
log.error(
{
COINGECKO_API_KEY: coingeckoApiKey || null,
COINMARKETCAP_API_KEY: cmcApiKey || null,
DATABASE_URL: dbUrl || null,
LIVECOINWATCH_API_KEY: lcwApiKey || null,
RPC_NODE_URL: rpcUrl || null,
},
'missing config...',
);
process.exit();
}
const env = cleanEnv(process.env, {
COINGECKO_API_KEY: str(),
COINMARKETCAP_API_KEY: str(),
DATABASE_CA: str({ default: '' }),
DATABASE_CERT: str({ default: '' }),
DATABASE_KEY: str({ default: '' }),
DATABASE_URL: str(),
LIVECOINWATCH_API_KEY: str(),
NETWORK: str(),
RPC_URL: str(),
SENTRY_DSN: str({ default: '' }),
});

const network: Network =
process.env.NETWORK === Network.TESTNET ? Network.TESTNET : Network.MAINNET;
Expand All @@ -29,14 +23,17 @@ const genesisDate = network === Network.MAINNET ? '2020-07-21' : '2021-04-01';
const sentryDsn = process.env.SENTRY_DSN;

const config: Config = {
cmcApiKey,
coingeckoApiKey,
dbUrl,
cmcApiKey: env.COINMARKETCAP_API_KEY,
coingeckoApiKey: env.COINGECKO_API_KEY,
dbCa: env.DATABASE_CA,
dbCert: env.DATABASE_CERT,
dbKey: env.DATABASE_KEY,
dbUrl: env.DATABASE_URL,
genesisDate,
genesisHeight,
lcwApiKey,
lcwApiKey: env.LIVECOINWATCH_API_KEY,
network,
rpcUrl,
rpcUrl: env.RPC_URL,
sentryDsn,
};

Expand Down
15 changes: 5 additions & 10 deletions apps/backend/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@ import { fileURLToPath } from 'url';

import Bree from 'bree';

import { logger as log } from 'nb-logger';

import knex from '#libs/knex';
import log from '#libs/log';
import sentry from '#libs/sentry';

const root = path.join(path.dirname(fileURLToPath(import.meta.url)), 'jobs');

const logger: Bree.BreeLogger = {
error: () => {
//
Expand All @@ -19,16 +21,9 @@ const logger: Bree.BreeLogger = {
//
},
};

const jobs: Bree.JobOptions[] = [
// { name: 'stats', cron: '*/15 * * * * *', hasSeconds: true }, // every 15 seconds
// { name: 'dailyStats', cron: '1 1 * * *' }, // every day at 01:01
// { name: 'ftMeta', cron: '* * * * *' }, // every minute
// { name: 'nftMeta', cron: '*/20 * * * * *', hasSeconds: true }, // every 20 seconds
// { name: 'marketData', cron: '* * * * *' }, // every minute
// { name: 'marketSearch', cron: '* * * * *' }, // every minute
// { name: 'ftTotalSupply', cron: '*/5 * * * * *', hasSeconds: true }, // every 5 seconds
// { name: 'refreshViews', cron: '*/15 * * * *' }, // every 15 minutes
{ cron: '0 0 1 1 *', name: 'stats' },
{ cron: '0 0 1 1 * *', hasSeconds: true, name: 'stats' },
];

const bree = new Bree({ jobs, logger, root });
Expand Down
22 changes: 0 additions & 22 deletions apps/backend/src/jobs/dailyStats.ts

This file was deleted.

33 changes: 0 additions & 33 deletions apps/backend/src/jobs/ftMeta.ts

This file was deleted.

32 changes: 0 additions & 32 deletions apps/backend/src/jobs/ftTotalSupply.ts

This file was deleted.

36 changes: 0 additions & 36 deletions apps/backend/src/jobs/marketData.ts

This file was deleted.

50 changes: 0 additions & 50 deletions apps/backend/src/jobs/marketSearch.ts

This file was deleted.

Loading