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

chore(backend): migrate to nestjs #68

Merged
merged 10 commits into from
Jun 3, 2024
Prev Previous commit
Next Next commit
chore(backend): configure environment variables
  • Loading branch information
tericcabrel committed May 22, 2024
commit d9c718bc3ffaeb020bcbce2105f05b54fb31d9dc
20 changes: 20 additions & 0 deletions apps/backend/.env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
NODE_ENV=development
APP_VERSION=1.0.0
HOST=http:https://localhost
PORT=7501
ENABLE_INTROSPECTION=true
DATABASE_URL="mysql:https://root:@127.0.0.1:3311/snipcode"
ADMIN_PASSWORD=nwHSvXuQxjey43Bp
CONVERTKIT_API_KEY=convertKitApiKey
CONVERTKIT_FORM_ID=formId
CONVERTKIT_TAG_ID=tagId
REQUEST_TIMEOUT=30000
GITHUB_CLIENT_ID=<github_client_id_here>
GITHUB_CLIENT_SECRET=<github_client_secret_here>
WEB_APP_URL=http:https://localhost:7500
WEB_AUTH_SUCCESS_URL=http:https://localhost:7500/auth/success
WEB_AUTH_ERROR_URL=http:https://localhost:7500/auth/error
SESSION_LIFETIME=90# 90 days
SENTRY_DSN=
SENTRY_ENABLED=false
SNIPPET_RENDERER_URL=http:https://localhost:3000/dev
1 change: 1 addition & 0 deletions apps/backend/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ lerna-debug.log*
.env.test.local
.env.production.local
.env.local
.env.docker

# temp directory
.temp
Expand Down
4 changes: 3 additions & 1 deletion apps/backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,12 @@
},
"dependencies": {
"@nestjs/common": "10.3.8",
"@nestjs/config": "3.2.2",
"@nestjs/core": "10.3.8",
"@nestjs/platform-express": "10.3.8",
"reflect-metadata": "0.2.2",
"rxjs": "7.8.1"
"rxjs": "7.8.1",
"zod": "3.23.8"
},
"devDependencies": {
"@nestjs/cli": "10.3.2",
Expand Down
11 changes: 10 additions & 1 deletion apps/backend/src/app.module.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,20 @@
import { Module } from '@nestjs/common';
import { ConfigModule } from '@nestjs/config';

import { AppController } from './app.controller';
import { AppService } from './app.service';
import appConfig, { validate } from './configs/environment';

@Module({
controllers: [AppController],
imports: [],
imports: [
ConfigModule.forRoot({
envFilePath: ['.env', '.env.test'],
isGlobal: true,
load: [appConfig],
validate,
}),
],
providers: [AppService],
})
export class AppModule {}
26 changes: 26 additions & 0 deletions apps/backend/src/configs/environment.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { registerAs } from '@nestjs/config';
import { z } from 'zod';

export default registerAs('app', () => ({
env: process.env.NODE_ENV,
host: process.env.HOST,
port: parseInt(process.env.PORT ?? '7501', 10),
}));

const EnvironmentVariablesSchema = z.object({
HOST: z.string(),
NODE_ENV: z.union([z.literal('development'), z.literal('production'), z.literal('test')]),
PORT: z.number({ coerce: true }).min(7000).max(8000),
});

export type EnvironmentVariables = z.infer<typeof EnvironmentVariablesSchema>;

export const validate = (config: Record<string, unknown>): EnvironmentVariables => {
const result = EnvironmentVariablesSchema.safeParse(config);

if (!result.success) {
throw new Error(JSON.stringify(result.error.format(), null, 2));
}

return result.data;
};
14 changes: 13 additions & 1 deletion apps/backend/src/main.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,23 @@
import { Logger } from '@nestjs/common';
import { ConfigService } from '@nestjs/config';
import { NestFactory } from '@nestjs/core';

import { AppModule } from './app.module';
import { EnvironmentVariables } from './types/common';

const bootstrap = async () => {
const app = await NestFactory.create(AppModule);

await app.listen(3000);
const configService = app.get(ConfigService<EnvironmentVariables, true>);
const logger = new Logger('NestApplication');

const port = configService.get<number>('PORT');
const host = configService.get<string>('HOST');

await app.listen(port, () => {
logger.log(`Server ready at ${host}:${port}`);
// logger.log(`Server ready at ${host}:${port}${graphqlServer.graphqlPath}`);
});
};

void bootstrap();
1 change: 1 addition & 0 deletions apps/backend/src/types/common.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export type { EnvironmentVariables } from '../configs/environment';
31 changes: 31 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4220,6 +4220,21 @@ __metadata:
languageName: node
linkType: hard

"@nestjs/config@npm:3.2.2":
version: 3.2.2
resolution: "@nestjs/config@npm:3.2.2"
dependencies:
dotenv: "npm:16.4.5"
dotenv-expand: "npm:10.0.0"
lodash: "npm:4.17.21"
uuid: "npm:9.0.1"
peerDependencies:
"@nestjs/common": ^8.0.0 || ^9.0.0 || ^10.0.0
rxjs: ^7.1.0
checksum: 10/c7f0cea0f6c73a5168b572510437d6033d27c6abdf686fa600ae42828b668470f1dd6240a3f8897e0f3c779582f4648ce28591369874e2a4bcf1e7b34c88c9b6
languageName: node
linkType: hard

"@nestjs/core@npm:10.3.8":
version: 10.3.8
resolution: "@nestjs/core@npm:10.3.8"
Expand Down Expand Up @@ -5689,6 +5704,7 @@ __metadata:
dependencies:
"@nestjs/cli": "npm:10.3.2"
"@nestjs/common": "npm:10.3.8"
"@nestjs/config": "npm:3.2.2"
"@nestjs/core": "npm:10.3.8"
"@nestjs/platform-express": "npm:10.3.8"
"@nestjs/schematics": "npm:10.1.1"
Expand All @@ -5702,6 +5718,7 @@ __metadata:
ts-loader: "npm:9.5.1"
ts-node: "npm:10.9.2"
tsconfig-paths: "npm:4.2.0"
zod: "npm:3.23.8"
languageName: unknown
linkType: soft

Expand Down Expand Up @@ -10071,6 +10088,13 @@ __metadata:
languageName: node
linkType: hard

"dotenv-expand@npm:10.0.0":
version: 10.0.0
resolution: "dotenv-expand@npm:10.0.0"
checksum: 10/b41eb278bc96b92cbf3037ca5f3d21e8845bf165dc06b6f9a0a03d278c2bd5a01c0cfbb3528ae3a60301ba1a8a9cace30e748c54b460753bc00d4c014b675597
languageName: node
linkType: hard

"dotenv-expand@npm:^5.1.0":
version: 5.1.0
resolution: "dotenv-expand@npm:5.1.0"
Expand Down Expand Up @@ -20857,6 +20881,13 @@ __metadata:
languageName: node
linkType: hard

"zod@npm:3.23.8":
version: 3.23.8
resolution: "zod@npm:3.23.8"
checksum: 10/846fd73e1af0def79c19d510ea9e4a795544a67d5b34b7e1c4d0425bf6bfd1c719446d94cdfa1721c1987d891321d61f779e8236fde517dc0e524aa851a6eff1
languageName: node
linkType: hard

"zx@npm:8.0.2":
version: 8.0.2
resolution: "zx@npm:8.0.2"
Expand Down