proxy: run different versions of pocketbase

This commit is contained in:
Ben Allfree 2022-10-13 12:49:27 +00:00
parent 6c0e49fba7
commit 106c02e8b1
8 changed files with 44 additions and 23 deletions

View File

@ -3,7 +3,7 @@
"version": "0.0.1", "version": "0.0.1",
"license": "MIT", "license": "MIT",
"scripts": { "scripts": {
"build": "mkdir -p dist && esbuild src/server.ts --bundle --platform=node > dist/server.js", "build": "mkdir -p dist && esbuild src/server.ts --bundle --platform=node > dist/server.js && echo 'Build complete' `date`",
"watch": "chokidar 'src/**' -c 'yarn build' --initial", "watch": "chokidar 'src/**' -c 'yarn build' --initial",
"serve": "node dist/server.js" "serve": "node dist/server.js"
}, },

View File

@ -31,11 +31,11 @@ const tryFetch = (url: string) =>
console.log(`Trying to connect to instance ${url} `) console.log(`Trying to connect to instance ${url} `)
fetch(url) fetch(url)
.then(() => { .then(() => {
console.log(`Connection successful`) console.log(`Connection to ${url} successful`)
resolve() resolve()
}) })
.catch((e) => { .catch((e) => {
console.error(`Could not connect`) console.error(`Could not connect to ${url}`)
setTimeout(tryFetch, 1000) setTimeout(tryFetch, 1000)
}) })
} }
@ -48,9 +48,13 @@ const mkInternalUrl = (port: number) => `http://${mkInternalAddress(port)}`
export const createInstanceManger = async () => { export const createInstanceManger = async () => {
const instances: { [_: string]: Instance } = {} const instances: { [_: string]: Instance } = {}
const _spawn = async (cfg: { subdomain: string; port: number }) => { const _spawn = async (cfg: {
const { subdomain, port } = cfg subdomain: string
const cmd = `${BIN_ROOT}/pocketbase` port: number
bin: string
}) => {
const { subdomain, port, bin } = cfg
const cmd = `${BIN_ROOT}/${bin}`
const args = [ const args = [
`serve`, `serve`,
`--dir`, `--dir`,
@ -91,6 +95,7 @@ export const createInstanceManger = async () => {
const mainProcess = await _spawn({ const mainProcess = await _spawn({
subdomain: CORE_PB_SUBDOMAIN, subdomain: CORE_PB_SUBDOMAIN,
port: CORE_PB_PORT, port: CORE_PB_PORT,
bin: 'pocketbase',
}) })
instances[CORE_PB_SUBDOMAIN] = { instances[CORE_PB_SUBDOMAIN] = {
process: mainProcess, process: mainProcess,
@ -100,10 +105,14 @@ export const createInstanceManger = async () => {
} }
await tryFetch(coreInternalUrl) await tryFetch(coreInternalUrl)
try { try {
await client.adminAuthViaEmail(CORE_PB_USERNAME, CORE_PB_PASSWORD)} await client.adminAuthViaEmail(CORE_PB_USERNAME, CORE_PB_PASSWORD)
catch(e) { } catch (e) {
console.error(`***WARNING*** CANNOT AUTHENTICATE TO https://${CORE_PB_SUBDOMAIN}.${APP_DOMAIN}/_/`) console.error(
console.error(`***WARNING*** LOG IN MANUALLY, ADJUST .env, AND RESTART DOCKER`) `***WARNING*** CANNOT AUTHENTICATE TO https://${CORE_PB_SUBDOMAIN}.${APP_DOMAIN}/_/`
)
console.error(
`***WARNING*** LOG IN MANUALLY, ADJUST .env, AND RESTART DOCKER`
)
} }
const limiter = new Bottleneck({ maxConcurrent: 1 }) const limiter = new Bottleneck({ maxConcurrent: 1 })
@ -111,18 +120,20 @@ export const createInstanceManger = async () => {
const getInstance = (subdomain: string) => const getInstance = (subdomain: string) =>
limiter.schedule(async () => { limiter.schedule(async () => {
console.log(`Getting instance ${subdomain}`) console.log(`Getting instance ${subdomain}`)
const instance = instances[subdomain] {
if (instance) { const instance = instances[subdomain]
console.log(`Found in cache: ${subdomain}`) if (instance) {
instance.heartbeat() console.log(`Found in cache: ${subdomain}`)
return instance instance.heartbeat()
return instance
}
} }
console.log(`Checking ${subdomain} for permission`) console.log(`Checking ${subdomain} for permission`)
const recs = await client.getInstanceBySubdomain(subdomain) const recs = await client.getInstanceBySubdomain(subdomain)
const [item] = recs.items const [instance] = recs.items
if (!item) { if (!instance) {
console.log(`${subdomain} not found`) console.log(`${subdomain} not found`)
return return
} }
@ -134,13 +145,17 @@ export const createInstanceManger = async () => {
port: 8090, port: 8090,
exclude, exclude,
}).catch((e) => { }).catch((e) => {
console.error(`Failed to get port`) console.error(`Failed to get port for ${subdomain}`)
throw e throw e
}) })
console.log(`Found port for ${subdomain}: ${newPort}`) console.log(`Found port for ${subdomain}: ${newPort}`)
await client.updateInstanceStatus(subdomain, InstanceStatus.Starting) await client.updateInstanceStatus(subdomain, InstanceStatus.Starting)
const childProcess = await _spawn({ subdomain, port: newPort }) const childProcess = await _spawn({
subdomain,
port: newPort,
bin: instance.bin || 'pocketbase',
})
const internalUrl = mkInternalUrl(newPort) const internalUrl = mkInternalUrl(newPort)

View File

@ -1 +1 @@
pocketbase dist

View File

@ -3,7 +3,13 @@
"version": "0.0.1", "version": "0.0.1",
"license": "MIT", "license": "MIT",
"scripts": { "scripts": {
"build:arm64": "GOOS=linux GOARCH=arm64 CGO_ENABLED=0 go build", "build:arm64": "cd src && GOOS=linux GOARCH=arm64 CGO_ENABLED=0 go build -o ../dist/pocketbase && echo 'Build complete' `date`",
"build:386": "GOOS=linux GOARCH=386 CGO_ENABLED=0 go build" "build:beta:arm64": "cd src && GOOS=linux GOARCH=arm64 CGO_ENABLED=0 go build -o ../dist/pocketbase-beta && echo 'Build complete' `date`",
"build:386": "cd src && GOOS=linux GOARCH=386 CGO_ENABLED=0 go build -o ../dist/pocketbase && echo 'Build complete' `date`",
"build:beta:386": "cd src && GOOS=linux GOARCH=386 CGO_ENABLED=0 go build -o ../dist/pocketbase-beta && echo 'Build complete' `date`",
"watch:beta:386": "chokidar 'src/**/*' -c 'yarn build:beta:386' --initial"
},
"devDependencies": {
"chokidar-cli": "^3.0.0"
} }
} }

View File

@ -46,7 +46,7 @@ Join us in the discussion area.
### 0.2.1 ### 0.2.1
- Idle/running status for PB instance now shows in green - Idle/running status for PB instance now shows in green
- Ability to run separate versions of PocketBase per instance for beta/dev purposes - Ability to run separate versions of PocketBase per instance for custom cases including beta/dev
### 0.2.0 ### 0.2.0