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

Allow configuration via DB URL #327

Merged
merged 2 commits into from
May 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ Use the docker [packaged image](https://github.com/andrew-codes/playnite-web/pkg
| DB_PORT | Port of Mongo DB database | Default for MongoDB image is 27017 |
| DB_USERNAME | Username to access database | Optional, only required if disabled anonymous access |
| DB_PASSWORD | Password to access database | Optional, only required if disabled anonymous access |
| DB_URL | MongoDB connection URL | Optional, alternative to individual DB connection options |
| DEBUG | `"game-db-updater/*"` | Optional, for troubleshooting; send logs to STDIO |

#### playnite-web-app
Expand All @@ -79,6 +80,7 @@ Use the docker [packaged image](https://github.com/andrew-codes/playnite-web/pkg
| DB_PORT | Port of Mongo DB database | Default for MongoDB image is 27017 |
| DB_USERNAME | Username to access database | Optional, only required if disabled anonymous access |
| DB_PASSWORD | Password to access database | Optional, only required if disabled anonymous access |
| DB_URL | MongoDB connection URL | Optional, alternative to individual DB connection options |
| DEBUG | `"playnite-web/*"` | Optional, for troubleshooting; send logs to STDIO |
| USERNAME | | Username used to login |
| PASSWORD | | Password value used to login |
Expand Down
46 changes: 28 additions & 18 deletions apps/game-db-updater/src/dbClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@ import createDebugger from 'debug'
import { MongoClient } from 'mongodb'

let client: MongoClient

type DbConnectionString = {
url?: string
cvele marked this conversation as resolved.
Show resolved Hide resolved
}

type DbConnectionOptions = {
host?: string
port?: number
Expand All @@ -10,29 +15,34 @@ type DbConnectionOptions = {
}

const getDbClient = async (
connectionOptions?: DbConnectionOptions,
connectionOptions?: DbConnectionOptions | DbConnectionString,
): Promise<MongoClient> => {
const debug = createDebugger('game-db-updater/mqttClient')

if (!client) {
const host = connectionOptions?.host ?? process.env.DB_HOST ?? 'localhost'
const port =
connectionOptions?.port ?? parseInt(process.env.DB_PORT ?? '27017')
const username = connectionOptions?.username ?? process.env.DB_USERNAME
const password = connectionOptions?.password ?? process.env.DB_PASSWORD

debug(
`Existing DB client not found; creating one with the following options: host=${host}, port=${port}, username=${username}`,
)
if (!username && !password) {
client = new MongoClient(`mongodb://${host}:${port}`)
const url = 'url' in connectionOptions ? connectionOptions.url : process.env.DB_URL
const host = 'host' in connectionOptions ? connectionOptions.host : process.env.DB_HOST ?? 'localhost'
const port = 'port' in connectionOptions ? connectionOptions.port : parseInt(process.env.DB_PORT ?? '27017')
const username = 'username' in connectionOptions ? connectionOptions.username : process.env.DB_USERNAME
const password = 'password' in connectionOptions ? connectionOptions.password : process.env.DB_PASSWORD

if (url) {
debug(`Existing DB client not found; creating one with the provided URL: ${url}`)
client = new MongoClient(url)
} else {
client = new MongoClient(`mongodb://${host}:${port}`, {
auth: {
username,
password,
},
})
debug(
`Existing DB client not found; creating one with the following options: host=${host}, port=${port}, username=${username}`,
)
if (!username && !password) {
client = new MongoClient(`mongodb://${host}:${port}`)
} else {
client = new MongoClient(`mongodb://${host}:${port}`, {
auth: {
username,
password,
},
})
}
}
}
await client.connect()
Expand Down
50 changes: 30 additions & 20 deletions apps/playnite-web/src/api/game/playnite/databases/mongo/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,37 +2,47 @@ import createDebugger from 'debug'
import { MongoClient } from 'mongodb'

let client: MongoClient

type DbConnectionString = {
url?: string
}

type DbConnectionOptions = {
host?: string
port?: number
username?: string
password?: string
}

const getDbClient = (connectionOptions?: DbConnectionOptions): MongoClient => {
const getDbClient = (connectionOptions?: DbConnectionOptions | DbConnectionString): MongoClient => {
const debug = createDebugger('playnite-web-app/MongoDbClient')

if (!client) {
const host = connectionOptions?.host ?? process.env.DB_HOST ?? 'localhost'
const port =
connectionOptions?.port ?? parseInt(process.env.DB_PORT ?? '27017')
const username = connectionOptions?.username ?? process.env.DB_USERNAME
const password = connectionOptions?.password ?? process.env.DB_PASSWORD

debug(
`Existing DB client not found; creating one with the following options: host=${host}, port=${port}, username=${username}`,
)

if (!username && !password) {
debug(`No username or password provided; connecting without auth`)
client = new MongoClient(`mongodb://${host}:${port}`)
const url = 'url' in connectionOptions ? connectionOptions.url : process.env.DB_URL
const host = 'host' in connectionOptions ? connectionOptions.host : process.env.DB_HOST ?? 'localhost'
const port = 'port' in connectionOptions ? connectionOptions.port : parseInt(process.env.DB_PORT ?? '27017')
const username = 'username' in connectionOptions ? connectionOptions.username : process.env.DB_USERNAME
const password = 'password' in connectionOptions ? connectionOptions.password : process.env.DB_PASSWORD

if (url) {
debug(`Existing DB client not found; creating one with the provided URL: ${url}`)
client = new MongoClient(url)
} else {
client = new MongoClient(`mongodb://${host}:${port}`, {
auth: {
username,
password,
},
})
debug(
`Existing DB client not found; creating one with the following options: host=${host}, port=${port}, username=${username}`,
)

if (!username && !password) {
debug(`No username or password provided; connecting without auth`)
client = new MongoClient(`mongodb://${host}:${port}`)
} else {
client = new MongoClient(`mongodb://${host}:${port}`, {
auth: {
username,
password,
},
})
}
}
}

Expand Down
Loading