Adding database properties
Some checks failed
Anatid Blog CI Workflow / test (./backend) (push) Failing after 27s
Anatid Blog CI Workflow / test (./frontend) (push) Successful in 31s
Anatid Blog CI Workflow / build-and-push-images (./backend/Dockerfile, git.anatid.net/tabris/anatid-blog-backend, ./backend) (push) Has been skipped
Anatid Blog CI Workflow / build-and-push-images (./frontend/Dockerfile, git.anatid.net/tabris/anatid-blog-frontend, ./frontend) (push) Has been skipped
Anatid Blog CI Workflow / deploy (push) Has been skipped
Some checks failed
Anatid Blog CI Workflow / test (./backend) (push) Failing after 27s
Anatid Blog CI Workflow / test (./frontend) (push) Successful in 31s
Anatid Blog CI Workflow / build-and-push-images (./backend/Dockerfile, git.anatid.net/tabris/anatid-blog-backend, ./backend) (push) Has been skipped
Anatid Blog CI Workflow / build-and-push-images (./frontend/Dockerfile, git.anatid.net/tabris/anatid-blog-frontend, ./frontend) (push) Has been skipped
Anatid Blog CI Workflow / deploy (push) Has been skipped
This commit is contained in:
parent
dc432eac98
commit
9bb442efe3
@ -1,44 +0,0 @@
|
|||||||
import { TypeOrmModuleOptions, TypeOrmOptionsFactory } from '@nestjs/typeorm';
|
|
||||||
import { Injectable } from '@nestjs/common';
|
|
||||||
import { VaultService } from './vault/vault.service';
|
|
||||||
|
|
||||||
const envData = process.env;
|
|
||||||
|
|
||||||
@Injectable()
|
|
||||||
export class TypeOrmConfigService
|
|
||||||
extends VaultService
|
|
||||||
implements TypeOrmOptionsFactory {
|
|
||||||
constructor() {
|
|
||||||
super();
|
|
||||||
}
|
|
||||||
async createTypeOrmOptions(): Promise<TypeOrmModuleOptions> {
|
|
||||||
|
|
||||||
try {
|
|
||||||
const path = "cubbyhole/blog/database";
|
|
||||||
const vaultData = await this.readSecret(path);
|
|
||||||
const result = vaultData.data;
|
|
||||||
return {
|
|
||||||
type: 'postgres',
|
|
||||||
host: 'postgres',
|
|
||||||
port: 5432,
|
|
||||||
requestTimeout: 300000,
|
|
||||||
username: 'blog',
|
|
||||||
password: result.password,
|
|
||||||
database: 'blog',
|
|
||||||
entities: [__dirname + '/entities/**/*.entity.{ts,js}'],
|
|
||||||
synchronize: false,
|
|
||||||
logging: true,
|
|
||||||
logger: 'simple-console',
|
|
||||||
migrations: [__dirname + '/migration/**/*.{ts,js}'],
|
|
||||||
migrationsRun: true,
|
|
||||||
extra: {
|
|
||||||
trustServerCertificate: true,
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
} catch (error) {
|
|
||||||
console.error('Error fetching data from Vault:', error);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
|
@ -2,18 +2,12 @@ import { Module } from '@nestjs/common';
|
|||||||
import { AppController } from './app.controller';
|
import { AppController } from './app.controller';
|
||||||
import { AppService } from './app.service';
|
import { AppService } from './app.service';
|
||||||
import { PostsModule } from './posts/posts.module';
|
import { PostsModule } from './posts/posts.module';
|
||||||
import { TypeOrmModule } from "@nestjs/typeorm";
|
import { DatabaseModule } from './database/database.module';
|
||||||
import { TypeOrmConfigService } from "./TypeOrmConfigService.service";
|
|
||||||
// import { TypeOrmLegacyService } from "./TypeOrmLegacyService.service";
|
|
||||||
|
|
||||||
require("dotenv").config();
|
|
||||||
|
|
||||||
@Module({
|
@Module({
|
||||||
imports: [
|
imports: [
|
||||||
TypeOrmModule.forRootAsync({
|
PostsModule,
|
||||||
useClass: TypeOrmConfigService,
|
DatabaseModule
|
||||||
}),
|
|
||||||
PostsModule
|
|
||||||
],
|
],
|
||||||
controllers: [AppController],
|
controllers: [AppController],
|
||||||
providers: [AppService],
|
providers: [AppService],
|
||||||
|
24
backend/src/database/database.module.ts
Normal file
24
backend/src/database/database.module.ts
Normal file
@ -0,0 +1,24 @@
|
|||||||
|
import { Module } from '@nestjs/common';
|
||||||
|
import { TypeOrmModule } from '@nestjs/typeorm';
|
||||||
|
import { ConfigModule, ConfigService } from '@nestjs/config';
|
||||||
|
|
||||||
|
@Module({
|
||||||
|
imports: [
|
||||||
|
ConfigModule.forRoot(),
|
||||||
|
TypeOrmModule.forRootAsync({
|
||||||
|
imports: [ConfigModule],
|
||||||
|
useFactory: (configService: ConfigService) => ({
|
||||||
|
type: 'postgres',
|
||||||
|
host: configService.get('POSTGRES_HOST'),
|
||||||
|
port: configService.get('POSTGRES_PORT'),
|
||||||
|
username: configService.get('BLOG_DB_USER'),
|
||||||
|
password: configService.get('BLOG_DB_PASSWORD'),
|
||||||
|
database: configService.get('BLOG_DB_NAME'),
|
||||||
|
entities: [__dirname + '/../**/*.entity{.ts,.js}'],
|
||||||
|
synchronize: true, // Be cautious about using synchronize in production
|
||||||
|
}),
|
||||||
|
inject: [ConfigService],
|
||||||
|
}),
|
||||||
|
],
|
||||||
|
})
|
||||||
|
export class DatabaseModule {}
|
@ -1,36 +0,0 @@
|
|||||||
import { Injectable } from '@nestjs/common';
|
|
||||||
import * as vault from 'node-vault';
|
|
||||||
require('dotenv').config();
|
|
||||||
@Injectable()
|
|
||||||
export class VaultService {
|
|
||||||
private vaultClient: any;
|
|
||||||
|
|
||||||
constructor() {
|
|
||||||
// Initialize the vault client
|
|
||||||
this.vaultClient = vault({
|
|
||||||
apiVersion: 'v2',
|
|
||||||
endpoint: process.env.VAULT_ADDR,
|
|
||||||
token: process.env.VAULT_TOKEN,
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
async readSecret(secretPath: string): Promise<any> {
|
|
||||||
try {
|
|
||||||
const roleId = process.env.ROLE_ID;
|
|
||||||
const secretId = process.env.SECRET_ID;
|
|
||||||
|
|
||||||
const result = await this.vaultClient.approleLogin({
|
|
||||||
role_id: roleId,
|
|
||||||
secret_id: secretId,
|
|
||||||
});
|
|
||||||
this.vaultClient.token = result.auth.client_token;
|
|
||||||
const secretData = await this.vaultClient.read(secretPath);
|
|
||||||
// secretpath is the path in vault where you have stored your secrets
|
|
||||||
return secretData.data;
|
|
||||||
} catch (error) {
|
|
||||||
console.error('Error reading secret from vault:', error);
|
|
||||||
throw error;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
Loading…
x
Reference in New Issue
Block a user