Skip to content

Commit

Permalink
Use simple-env-utils lib
Browse files Browse the repository at this point in the history
  • Loading branch information
tjsr committed Apr 20, 2024
1 parent b9cd4cf commit 3fc8145
Show file tree
Hide file tree
Showing 10 changed files with 23 additions and 26 deletions.
2 changes: 1 addition & 1 deletion index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as dotenv from 'dotenv';

import express from 'express';
import { requireEnv } from './src/utils/requireEnv';
import { requireEnv } from '@tjsr/simple-env-utils';
import { startApp } from './src/server';

dotenv.config();
Expand Down
6 changes: 6 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
"url": "https://github.com/tjsr/tagtool/issues"
},
"dependencies": {
"@tjsr/simple-env-utils": "^0.0.4",
"cookie-parser": "^1.4.6",
"cors": "^2.8.5",
"dotenv": "^16.0.3",
Expand Down
13 changes: 7 additions & 6 deletions src/database/mysqlConnections.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,25 @@
import * as dotenv from 'dotenv';
import * as dotenv from 'dotenv-flow';

import { booleanEnv, intEnv, requireEnv } from '@tjsr/simple-env-utils';

import mysql from 'mysql';
import { requireEnv } from '../utils/requireEnv';

export type PoolConnection = mysql.PoolConnection;

dotenv.config();
dotenv.config({ silent: true });

const config: mysql.PoolConfig = {
bigNumberStrings: true,
connectTimeout: process.env['MYSQL_CONNECT_TIMEOUT'] ? parseInt(process.env['MYSQL_CONNECT_TIMEOUT']) : 2000,
connectTimeout: intEnv('MYSQL_CONNECT_TIMEOUT', 2000),
connectionLimit:
process.env.MYSQL_CONNECTION_POOL_SIZE !== undefined ?
parseInt(process.env.MYSQL_CONNECTION_POOL_SIZE) :
5,
database: requireEnv('MYSQL_DATABASE'),
debug: process.env['MYSQL_DEBUG'] === 'true' ? true : false,
debug: booleanEnv('MYSQL_DEBUG', false),
host: requireEnv('MYSQL_HOST'),
password: requireEnv('MYSQL_PASSWORD'),
port: process.env['MYSQL_PORT'] ? parseInt(process.env['MYSQL_PORT']) : 3306,
port: intEnv('MYSQL_PORT', 3306),
supportBigNumbers: true,
user: requireEnv('MYSQL_USER'),
};
Expand Down
4 changes: 3 additions & 1 deletion src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,9 @@ export const getIp = (req: express.Request): IPAddress => {

export const startApp = (sessionStore?: session.MemoryStore): express.Express => {
const app: express.Express = express();
app.use(morganLog);
if (process.env['USE_LOGGING'] !== 'false') {
app.use(morganLog);
}
app.use(cors(corsOptions));
app.use(requestIp.mw());
app.set('trust proxy', true);
Expand Down
4 changes: 3 additions & 1 deletion src/setup-tests.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import * as dotenvFlow from 'dotenv-flow';

import { setTestMode } from './utils';
import { setTestMode } from '@tjsr/simple-env-utils';

dotenvFlow.config({ path: process.cwd() });

process.env['USE_LOGGING'] = 'false';

setTestMode();
9 changes: 0 additions & 9 deletions src/utils.ts

This file was deleted.

6 changes: 0 additions & 6 deletions src/utils/requireEnv.ts

This file was deleted.

2 changes: 1 addition & 1 deletion src/utils/validateObjectId.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { ObjectId } from '../types';
import { isTestMode } from '../utils';
import { isTestMode } from '@tjsr/simple-env-utils';
import { validate } from 'uuid';

export const validateObjectId = (id: ObjectId): boolean => {
Expand Down
2 changes: 1 addition & 1 deletion src/utils/validateTag.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { isTestMode } from '../utils';
import { isTestMode } from '@tjsr/simple-env-utils';
import { validate } from 'uuid';

export const validateTag = (id: string): boolean => {
Expand Down

0 comments on commit 3fc8145

Please sign in to comment.