Skip to content

Commit

Permalink
perf(backend): Move config to package
Browse files Browse the repository at this point in the history
  • Loading branch information
aXenDeveloper committed Jun 19, 2024
1 parent 413de1a commit 3379f10
Show file tree
Hide file tree
Showing 6 changed files with 101 additions and 64 deletions.
14 changes: 4 additions & 10 deletions apps/backend/src/app.module.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,21 @@
import { join } from "path";

import { Module } from "@nestjs/common";
import { ConfigModule } from "@nestjs/config";
import { VitNodeCoreModule } from "vitnode-backend";

import { DatabaseModule } from "./database/database.module";
import { PluginsModule } from "./plugins/plugins.module";
import { ABSOLUTE_PATHS, configForAppModule } from "./config";
import { schemaDatabase } from "./database/schema";

@Module({
imports: [
ConfigModule.forRoot({
isGlobal: true,
load: [configForAppModule],
envFilePath: join(process.cwd(), "..", "..", ".env")
}),
PluginsModule,
DatabaseModule,
VitNodeCoreModule.register({
paths: {
uploadPublic: ABSOLUTE_PATHS.uploads.public,
graphqlSchema: join(process.cwd(), "schema.gql")
}
envFile: join(process.cwd(), "..", "..", ".env")
},
schemaDatabase
})
]
})
Expand Down
42 changes: 0 additions & 42 deletions apps/backend/src/config.ts
Original file line number Diff line number Diff line change
@@ -1,48 +1,6 @@
import * as fs from "fs";
import { join } from "path";

import { parseFrontendUrlFromEnv } from "./functions/envs";

export const configForAppModule = () => {
const frontend_url = parseFrontendUrlFromEnv();

const data = {
login_token_secret: process.env.LOGIN_TOKEN_SECRET ?? "",
frontend_url: frontend_url.url,
port: parseInt(process.env.PORT, 10) || 8080,
password_salt: 10,
cookies: {
domain:
frontend_url.hostname === "localhost"
? "localhost"
: frontend_url.hostname
.replace(/:\d+$/, "")
.split(".")
.slice(-2)
.join("."),
login_token: {
expiresIn: 3, // 3 days
expiresInRemember: 90, // 90 days
name: "vitnode-login-token",
admin: {
name: "vitnode-login-token-admin",
expiresIn: 1 // 1 day
}
},
known_device: {
name: "vitnode-device",
expiresIn: 365 // 1 year
}
}
};

if (!data.login_token_secret) {
throw new Error("`LOGIN_TOKEN_SECRET` is not defined in .env file");
}

return data;
};

const internalPaths = {
uploads: join(process.cwd(), "uploads"),
frontend: join(process.cwd(), "..", "frontend"),
Expand Down
5 changes: 4 additions & 1 deletion packages/backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,16 @@
"build": "tsc",
"dev": "tsc -w",
"lint": "eslint .",
"lint:fix": "eslint . --fix"
"lint:fix": "eslint . --fix",
"migrate:admin": "drizzle-kit up --config src/drizzle.config.ts && drizzle-kit generate --config src/drizzle.config.ts"
},
"main": "./dist/index.js",
"types": "./dist/index.d.ts",
"devDependencies": {
"@apollo/server": "^4.10.4",
"@nestjs/apollo": "^12.1.0",
"@nestjs/common": "^10.3.9",
"@nestjs/config": "^3.2.2",
"@nestjs/graphql": "^12.1.1",
"@nestjs/jwt": "^10.2.0",
"@nestjs/schedule": "^4.0.2",
Expand All @@ -36,6 +38,7 @@
"dependencies": {
"class-validator": "^0.14.1",
"cookie-parser": "^1.4.6",
"drizzle-kit": "^0.22.7",
"drizzle-orm": "^0.31.2",
"express": "^4.19.2",
"graphql": "^16.8.2",
Expand Down
86 changes: 78 additions & 8 deletions packages/backend/src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,27 +5,96 @@ import { ScheduleModule } from "@nestjs/schedule";
import { JwtModule } from "@nestjs/jwt";
import { ServeStaticModule } from "@nestjs/serve-static";
import { GraphQLModule } from "@nestjs/graphql";
import { ConfigModule } from "@nestjs/config";
import { ApolloDriver, ApolloDriverConfig } from "@nestjs/apollo";
import { ApolloServerPluginLandingPageLocalDefault } from "@apollo/server/plugin/landingPage/default";

import { Ctx } from "./utils";
import { CoreModule } from "./core/core.module";

interface Args {
paths: {
graphqlSchema: string;
uploadPublic: string;
export const ABSOLUTE_PATHS_BACKEND = {
schema: join(process.cwd(), "schema.gql"),
uploads: {
public: join(process.cwd(), "uploads", "public")
}
};

const parseFrontendUrlFromEnv = () => {
const envUrl = process.env.NEXT_PUBLIC_FRONTEND_URL;
const frontendUrl = envUrl ? envUrl : "http://localhost:3000";
const urlObj = new URL(frontendUrl);

return {
url: frontendUrl,
protocol: urlObj.protocol,
hostname: urlObj.hostname,
port: urlObj.port
};
};

const config = () => {
const frontend_url = parseFrontendUrlFromEnv();

const data = {
login_token_secret: process.env.LOGIN_TOKEN_SECRET ?? "",
frontend_url: frontend_url.url,
port: parseInt(process.env.PORT, 10) || 8080,
password_salt: 10,
cookies: {
domain:
frontend_url.hostname === "localhost"
? "localhost"
: frontend_url.hostname
.replace(/:\d+$/, "")
.split(".")
.slice(-2)
.join("."),
login_token: {
expiresIn: 3, // 3 days
expiresInRemember: 90, // 90 days
name: "vitnode-login-token",
admin: {
name: "vitnode-login-token-admin",
expiresIn: 1 // 1 day
}
},
known_device: {
name: "vitnode-device",
expiresIn: 365 // 1 year
}
}
};

if (!data.login_token_secret) {
throw new Error("`LOGIN_TOKEN_SECRET` is not defined in .env file");
}

return data;
};

export interface VitNodePaths {
envFile: string;
}

interface Args {
paths: VitNodePaths;
schemaDatabase: Record<string, unknown>;
}

@Module({})
export class VitNodeCoreModule {
static register({ paths }: Args): DynamicModule {
static register({ paths, schemaDatabase }: Args): DynamicModule {

Check warning on line 86 in packages/backend/src/app.module.ts

View workflow job for this annotation

GitHub Actions / install

'schemaDatabase' is defined but never used
return {
module: VitNodeCoreModule,
imports: [
ConfigModule.forRoot({
isGlobal: true,
load: [config],
envFilePath: paths.envFile
}),
GraphQLModule.forRoot<ApolloDriverConfig>({
driver: ApolloDriver,
autoSchemaFile: join(process.cwd(), "schema.gql"),
autoSchemaFile: ABSOLUTE_PATHS_BACKEND.schema,
sortSchema: true,
playground: false,
plugins: [ApolloServerPluginLandingPageLocalDefault()],
Expand All @@ -34,9 +103,10 @@ export class VitNodeCoreModule {
ScheduleModule.forRoot(),
JwtModule.register({ global: true }),
ServeStaticModule.forRoot({
rootPath: paths.uploadPublic,
rootPath: ABSOLUTE_PATHS_BACKEND.uploads.public,
serveRoot: "/public/"
})
}),
CoreModule
]
};
}
Expand Down
6 changes: 6 additions & 0 deletions packages/backend/src/core/core.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { Module } from "@nestjs/common";

@Module({
imports: []
})
export class CoreModule {}
12 changes: 9 additions & 3 deletions pnpm-lock.yaml

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

0 comments on commit 3379f10

Please sign in to comment.