diff --git a/.env.sample b/.env.sample new file mode 100644 index 0000000..8b5e5e4 --- /dev/null +++ b/.env.sample @@ -0,0 +1 @@ +NODE_TLS_REJECT_UNAUTHORISED = 0 \ No newline at end of file diff --git a/apps/humanoid-ai-backend/src/app/app.module.ts b/apps/humanoid-ai-backend/src/app/app.module.ts index 1ad550f..1cac777 100644 --- a/apps/humanoid-ai-backend/src/app/app.module.ts +++ b/apps/humanoid-ai-backend/src/app/app.module.ts @@ -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], diff --git a/apps/humanoid-ai-backend/src/cosmosDB/commosdb.module.ts b/apps/humanoid-ai-backend/src/cosmosDB/commosdb.module.ts new file mode 100644 index 0000000..4463c42 --- /dev/null +++ b/apps/humanoid-ai-backend/src/cosmosDB/commosdb.module.ts @@ -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('COSMOS_ACCOUNT_ENDPOINT'); + const key = configService.get('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 {} diff --git a/apps/humanoid-ai-backend/src/cosmosDB/config/config.ts b/apps/humanoid-ai-backend/src/cosmosDB/config/config.ts new file mode 100644 index 0000000..2357a2d --- /dev/null +++ b/apps/humanoid-ai-backend/src/cosmosDB/config/config.ts @@ -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', +}); diff --git a/apps/humanoid-ai-backend/src/cosmosDB/config/index.ts b/apps/humanoid-ai-backend/src/cosmosDB/config/index.ts new file mode 100644 index 0000000..67f4b63 --- /dev/null +++ b/apps/humanoid-ai-backend/src/cosmosDB/config/index.ts @@ -0,0 +1 @@ +export * from './config' \ No newline at end of file diff --git a/docker-compose.yml b/docker-compose.yml index 88672f4..5c84815 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -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: