Skip to content

Commit

Permalink
backend: use js config files for node-config/node-config#530
Browse files Browse the repository at this point in the history
  • Loading branch information
ddadaal committed Jul 29, 2021
1 parent 3278dd3 commit b8b2f1d
Show file tree
Hide file tree
Showing 9 changed files with 40 additions and 30 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ More commands can be found in `frontend/README.md` and `backend/README.md`.
# Deployment

1. Configure db initial password in `.env`
1. Create and modify `backend/config/production.ts` based on `backend/config/production.sample.ts`
1. Create and modify `backend/config/production.js` based on `backend/config/production.sample.js`
2. `docker-compose up`

Default configs are provided in `docker-compose.yml`. The following can be changed.
Expand All @@ -46,7 +46,7 @@ Default configs are provided in `docker-compose.yml`. The following can be chang
| backend port | 5000 | mapped from 3000 |
| frontend args (see below) | USE_MOCK=0 | |
| static files directory | ./static | |
| backend configuration file | ./backend/config/production.ts | Must be created before starting. |
| backend configuration file | ./backend/config/production.js | Must be created before starting. |
| backend db files | ./backend/distdb | MySQL |
| backend upload dir | ./backend/distupload | |

Expand Down
8 changes: 4 additions & 4 deletions backend/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,9 @@ npm run devenv:stop
[node-config](https://github.com/lorenwest/node-config) is used for configuration. Create config files on `config` dir according to its strategy.


For example, you will need a `production.ts` (which can be copied from `production.sample.ts`) for production deployment.
For example, you will need a `production.js` (which can be based on `production.sample.js`) for production deployment.

See `config/default.ts` for default config.
See `config/default.js` for default config.

# Deployment

Expand Down Expand Up @@ -92,9 +92,9 @@ On the folder with built assets shown above,
# Only install production dependencies
npm ci --only=production

# Copy `config/production.sample.ts` to `config/production.ts` and change the configs
# Copy `config/production.sample.json` to `config/production.json` and change the configs
# Or use symlink or docker mount to mount a predefined production.json to the config directory
cp config/production.sample.ts config/production.ts
cp config/production.sample.json config/production.json
vim config/production.json

# Run!
Expand Down
11 changes: 6 additions & 5 deletions backend/config/default.ts → backend/config/default.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type { Config } from "@/utils/config";
// @ts-check

export default {
/** @type {import("../src/utils/config").Config} */
const config = {
address: "0.0.0.0",
pluginTimeout: 30000,
port: 5000,
Expand All @@ -9,7 +10,6 @@ export default {
logger: true,
jwtSecret: "testsecret",
orm: {
logging: true,
connectionTimeout: 20000,
type: "mysql",
debug: true,
Expand All @@ -21,7 +21,6 @@ export default {
staticPrefix: "static",
bcryptSaltLength: 10,
mail: {
pool: true,
secure: true,
from: "yaarxiv",
ignoreError: true,
Expand All @@ -36,4 +35,6 @@ export default {
timeoutSeconds: 120 * 60,
sendIntervalSeconds: 30 * 60,
},
} as Config;
};

module.exports = config;
11 changes: 7 additions & 4 deletions backend/config/development.ts → backend/config/development.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import type { ConfigOverride } from "@/utils/config";
// @ts-check

export default {
/** @type {import("../src/utils/config").ConfigOverride} */
const config = {
loadSwagger: true,
orm: {
host: "localhost",
port: 3306,
username: "root",
user: "root",
password: "dbfordev",
dbName: "yaarxiv_dev",
},
Expand All @@ -14,4 +15,6 @@ export default {
level: "trace",
prettyPrint: true,
},
} as ConfigOverride;
};

module.exports = config;
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
export default {
// @ts-check

/** @type {import("../src/utils/config").ConfigOverride} */
const config = {
orm: {
host: "localhost",
port: 3306,
username: "root",
user: "root",
password: "dbfordev",
dbName: "yaarxiv_prod",
},
Expand All @@ -16,3 +19,5 @@ export default {
},
frontendUrl: "http://localhost:5000",
};

module.exports = config;
13 changes: 7 additions & 6 deletions backend/config/test.ts → backend/config/test.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
import type { ConfigOverride } from "@/utils/config";
// @ts-check

export default {
port: 0,
loadSwagger: false,
/** @type {import("../src/utils/config").ConfigOverride} */
const config = {
orm: {
type: "mysql",
host: "localhost",
port: 3306,
dbName: `yaarxiv_test_${process.env.JEST_WORKER_ID}`,
username: "root",
user: "root",
password: "dbfordev",
synchronize: true,
dropSchema: true,
Expand All @@ -20,4 +19,6 @@ export default {
upload: {
path: "testupload",
},
} as ConfigOverride;
};

module.exports = config;
6 changes: 3 additions & 3 deletions backend/src/plugins/orm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,14 @@ declare module "fastify" {

export const ormPlugin = fp(async (fastify) => {
// create the database if not exists.
const { dbName, dropSchema, highlight, connectTimeout, host, port, runMigrations, synchronize } = config.orm;
const { dbName, dropSchema, highlight, connectionTimeout, host, port, runMigrations, synchronize } = config.orm;

fastify.log.info("Wait for db connection.");

// wait for db to be alive
await waitOn({
resources: [`tcp:${host}:${port}`],
timeout: connectTimeout,
timeout: connectionTimeout,
});

fastify.log.info("db is started. Connecting to it.");
Expand All @@ -37,7 +37,7 @@ export const ormPlugin = fp(async (fastify) => {
logger: (msg) => fastify.log.info(msg),
entities,
driverOptions: {
connectTimeout,
connectTimeout: connectionTimeout,
},
});

Expand Down
2 changes: 1 addition & 1 deletion backend/src/utils/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export interface Config {
runMigrations?: boolean;
dropSchema?: boolean;
highlight?: boolean;
connectTimeout?: number;
connectionTimeout?: number;
synchronize?: boolean;
};
upload: {
Expand Down
6 changes: 3 additions & 3 deletions backend/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@
},
"include": [
"src/**/*",
"config",
"tests/**/*",
"mikro-orm.config.ts"
, "tests/globals.d.ts" ],
"mikro-orm.config.ts",
"tests/globals.d.ts"
],
"exclude": [
"node_modules",
"migrations",
Expand Down

0 comments on commit b8b2f1d

Please sign in to comment.