Skip to content

Commit

Permalink
chore: improve exit log when no set MONGODB_URL env
Browse files Browse the repository at this point in the history
Signed-off-by: BobDu <i@bobdu.cc>
  • Loading branch information
BobDu committed Jan 12, 2024
1 parent 46d819f commit 05e2643
Showing 1 changed file with 12 additions and 3 deletions.
15 changes: 12 additions & 3 deletions service/src/storage/mongo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,18 @@ import { getCacheConfig } from './config'
dotenv.config()

const url = process.env.MONGODB_URL
const parsedUrl = new URL(url)
const dbName = (parsedUrl.pathname && parsedUrl.pathname !== '/') ? parsedUrl.pathname.substring(1) : 'chatgpt'
const client = new MongoClient(url)

let client: MongoClient
let dbName: string
try {
client = new MongoClient(url)
const parsedUrl = new URL(url)
dbName = (parsedUrl.pathname && parsedUrl.pathname !== '/') ? parsedUrl.pathname.substring(1) : 'chatgpt'
}
catch (e) {
globalThis.console.error('MongoDB url invalid. please ensure set valid env MONGODB_URL.', e.message)
process.exit(1)
}

const chatCol = client.db(dbName).collection<ChatInfo>('chat')
const roomCol = client.db(dbName).collection<ChatRoom>('chat_room')
Expand Down

0 comments on commit 05e2643

Please sign in to comment.