Files
speckle-server/packages/server/modules/multiregion/regionConfig.ts
T
Gergő Jedlicska f210d9b749 gergo/web 2109 project region based db connection selector (#3434)
* feat(projects): add project regions, default to null

* feat(multiregion): add projectRegion Db client lookup logic

* feat(multiregion): add project region repositories and caching

* feat(multiRegion): db initialization and get project db client

* feat(docker-compose): add second db for regions testing

* feat(multiRegion): initialize region with pubs and subs working

* fix(multiRegion): get region client even if it was registered in another pod

* feat(workspaces): create workspace resolver split

* feat: update server region metadata

* feat(projects): rewrite project creation

* feat(multiRegion): getRegionDb

* fix(workspaces): get projects now can retur null

* feat(multiRegion): make local multi region DB-s work

* feat: set d efault workspace region

* CR changes

* tests

* feat(multiRegion): bind region properly

* fe update

* test fixes

* feat(multiRegion): automatically create aiven extras plugin

* ci(postgres): use published postgres with aiven extras

* fix(multiRegion): roll back the aiven extras migration, there is a better way

* tests fix

* fix(billing): we do not need to add a seat, if the workspace is on a plan, but has no sub

---------

Co-authored-by: Kristaps Fabians Geikins <fabis94@live.com>
2024-11-06 17:29:08 +01:00

69 lines
2.3 KiB
TypeScript

import { GetAvailableRegionConfig } from '@/modules/multiregion/domain/operations'
import { AllRegionsConfig } from '@/modules/multiregion/domain/types'
import { packageRoot } from '@/bootstrap'
import path from 'node:path'
import fs from 'node:fs/promises'
import {
getMultiRegionConfigPath,
isDevOrTestEnv
} from '@/modules/shared/helpers/envHelper'
import { type Optional } from '@speckle/shared'
import { multiRegionConfigSchema } from '@/modules/multiregion/helpers/validation'
import { MisconfiguredEnvironmentError } from '@/modules/shared/errors'
import { get } from 'lodash'
import { isMultiRegionEnabled } from '@/modules/multiregion/helpers'
let multiRegionConfig: Optional<AllRegionsConfig> = undefined
const getAllRegionsConfig = async (): Promise<AllRegionsConfig> => {
if (isDevOrTestEnv() && !isMultiRegionEnabled())
// this should throw somehow
return { main: { postgres: { connectionUri: '' } }, regions: {} }
if (multiRegionConfig) return multiRegionConfig
const relativePath = getMultiRegionConfigPath()
const fullPath = path.resolve(packageRoot, relativePath)
let file: string
try {
file = await fs.readFile(fullPath, 'utf-8')
} catch (e) {
if (get(e, 'code') === 'ENOENT') {
throw new MisconfiguredEnvironmentError(
`Multi-region config file not found at path: ${fullPath}`
)
}
throw e
}
let parsedJson: string
try {
parsedJson = JSON.parse(file) // This will throw if the file is not valid JSON
} catch (e) {
throw new MisconfiguredEnvironmentError(
`Multi-region config file at path '${fullPath}' is not valid JSON`
)
}
const multiRegionConfigFileResult = multiRegionConfigSchema.safeParse(parsedJson) // This will throw if the config is invalid
if (!multiRegionConfigFileResult.success)
throw new MisconfiguredEnvironmentError(
`Multi-region config file at path '${fullPath}' does not fit the schema`,
{ cause: multiRegionConfigFileResult.error, info: { parsedJson } }
)
multiRegionConfig = multiRegionConfigFileResult.data
return multiRegionConfig
}
export const getMainRegionConfig = async (): Promise<AllRegionsConfig['main']> => {
return (await getAllRegionsConfig()).main
}
export const getAvailableRegionConfig: GetAvailableRegionConfig = async () => {
return (await getAllRegionsConfig()).regions
}