Skip to content

Commit

Permalink
Merge pull request #10 from CirrusLabs-NPD/cosmosdbModule
Browse files Browse the repository at this point in the history
added cosmosdb module
  • Loading branch information
Aakash-bhardwaj-cirruslabs authored Jun 26, 2024
2 parents dfd813f + a4ac626 commit 5097449
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 22 deletions.
1 change: 1 addition & 0 deletions .env.sample
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
NODE_TLS_REJECT_UNAUTHORISED = 0
10 changes: 4 additions & 6 deletions apps/humanoid-ai-backend/src/app/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,17 @@ import { ChatModule } from '../chat/chat.module';
import { AIModule } from '../ai/ai.module';
import { AppController } from './app.controller';
import { AppService } from './app.service';
import { CosmosdbModule } from '../cosmosDB/commosdb.module';

@Module({
imports: [
ConfigModule.forRoot({
isGlobal: true,
envFilePath: '.env',
}),

MongooseModule.forRoot('mongodb://localhost:27017/nest'),
ConfigModule.forRoot({ isGlobal: true }),
MongooseModule.forRoot(process.env.MONGODB_URI),
AuthModule,
UsersModule,
ChatModule,
AIModule,
CosmosdbModule,
],
controllers: [AppController],
providers: [AppService],
Expand Down
37 changes: 37 additions & 0 deletions apps/humanoid-ai-backend/src/cosmosDB/commosdb.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { Module, Logger } from '@nestjs/common';
import { ConfigModule, ConfigService } from '@nestjs/config';
import { CosmosClient } from '@azure/cosmos';
import { configuration } from './config';

@Module({
imports: [
ConfigModule.forRoot({
isGlobal: true,
load: [configuration],
}),
],
providers: [
{
provide: 'COSMOS_DB_CLIENT',
useFactory: async (configService: ConfigService) => {
const endpoint = configService.get<string>('COSMOS_ACCOUNT_ENDPOINT');
const key = configService.get<string>('COSMOS_ACCOUNT_KEY');
const client = new CosmosClient({ endpoint, key, userAgentSuffix: 'CosmosDBJavascriptQuickstart' });

try {
// Perform a simple operation to verify the connection
await client.databases.readAll().fetchAll();
Logger.log('Successfully connected to Cosmos DB');
} catch (error) {
Logger.error('Failed to connect to Cosmos DB', error);
throw error;
}

return client;
},
inject: [ConfigService],
},
],
exports: ['COSMOS_DB_CLIENT'],
})
export class CosmosdbModule {}
7 changes: 7 additions & 0 deletions apps/humanoid-ai-backend/src/cosmosDB/config/config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export const configuration = () => ({
COSMOS_ACCOUNT_ENDPOINT: process.env.COSMOS_ACCOUNT_ENDPOINT,
COSMOS_ACCOUNT_KEY: process.env.COSMOS_ACCOUNT_KEY,
COSMOS_DATABASE: process.env.COSMOS_DATABASE,
COSMOS_APPNAME: process.env.COSMOS_APPNAME || 'MyApp',
COSMOS_MAX_IDLE_TIME_MS: process.env.COSMOS_MAX_IDLE_TIME_MS || '60000',
});
1 change: 1 addition & 0 deletions apps/humanoid-ai-backend/src/cosmosDB/config/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './config'
29 changes: 13 additions & 16 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,28 +8,25 @@ services:
- MONGO_INITDB_ROOT_USERNAME=admin
- MONGO_INITDB_ROOT_PASSWORD=password
ports:
- "27017:27017"
- '27017:27017'
volumes:
- mongo-data:/data/db

cosmosdb:
image: mcr.microsoft.com/cosmosdb/linux/azure-cosmos-emulator:latest
image: mcr.microsoft.com/cosmosdb/linux/azure-cosmos-emulator
container_name: cosmosdb
environment:
AZURE_COSMOS_EMULATOR_PARTITION_COUNT: 1
AZURE_COSMOS_EMULATOR_ENABLE_DATA_PERSISTENCE: "true"
ports:
- "8081:8081"
- "10251:10251"
- "10252:10252"
- "10253:10253"
- "10254:10254"
- "10255:10255"
- "10256:10256"
volumes:
- cosmosdb-data:/data
command: /bin/bash -c 'while :; do sleep 10; done' # Keep the container running
- '8081:8081'
environment:
- AZURE_COSMOS_EMULATOR_PARTITION_COUNT=1
- AZURE_COSMOS_EMULATOR_ENABLE_DATA_PERSISTENCE=true

volumes:
- ./cosmosdbdata:/data
healthcheck:
test: curl --fail http://localhost:8081/_explorer/emulator.pem || exit 1
interval: 10s
timeout: 5s
retries: 3
volumes:
mongo-data:
cosmosdb-data:

0 comments on commit 5097449

Please sign in to comment.