98 lines
2.7 KiB
TypeScript
98 lines
2.7 KiB
TypeScript
import { storeJson } from './fileModels/store.json'
|
|
import { i18n } from './i18n'
|
|
import { sdk } from './sdk'
|
|
import { appPort, databaseName, databaseUser } from './utils'
|
|
|
|
export const main = sdk.setupMain(async ({ effects }) => {
|
|
const secrets = await storeJson.read().once()
|
|
if (!secrets?.databasePassword || !secrets.secretKeyBase) {
|
|
throw new Error('Todo Back secrets have not been initialized')
|
|
}
|
|
|
|
const databaseUrl = `postgresql://${databaseUser}:${secrets.databasePassword}@127.0.0.1:5432/${databaseName}`
|
|
const appEnv = {
|
|
DATABASE_URL: databaseUrl,
|
|
SECRET_KEY_BASE: secrets.secretKeyBase,
|
|
RAILS_ENV: 'production',
|
|
RAILS_LOG_TO_STDOUT: 'true',
|
|
PORT: String(appPort),
|
|
}
|
|
|
|
const postgresSub = sdk.SubContainer.of(
|
|
effects,
|
|
{ imageId: 'postgres' },
|
|
sdk.Mounts.of().mountVolume({
|
|
volumeId: 'postgres',
|
|
subpath: null,
|
|
mountpoint: '/var/lib/postgresql/data',
|
|
readonly: false,
|
|
}),
|
|
'postgres-subcontainer',
|
|
)
|
|
|
|
const appSub = sdk.SubContainer.of(
|
|
effects,
|
|
{ imageId: 'app' },
|
|
sdk.Mounts.of().mountVolume({
|
|
volumeId: 'storage',
|
|
subpath: null,
|
|
mountpoint: '/app/storage',
|
|
readonly: false,
|
|
}),
|
|
'app-subcontainer',
|
|
)
|
|
|
|
return sdk.Daemons.of(effects)
|
|
.addDaemon('postgres', {
|
|
subcontainer: postgresSub,
|
|
exec: {
|
|
command: sdk.useEntrypoint(['-c', 'listen_addresses=127.0.0.1']),
|
|
env: {
|
|
POSTGRES_DB: databaseName,
|
|
POSTGRES_USER: databaseUser,
|
|
POSTGRES_PASSWORD: secrets.databasePassword,
|
|
PGDATA: '/var/lib/postgresql/data',
|
|
},
|
|
},
|
|
ready: {
|
|
display: i18n('Database'),
|
|
fn: () =>
|
|
sdk.healthCheck.runHealthScript(
|
|
[
|
|
'pg_isready',
|
|
'-h',
|
|
'127.0.0.1',
|
|
'-U',
|
|
databaseUser,
|
|
'-d',
|
|
databaseName,
|
|
],
|
|
postgresSub,
|
|
{ errorMessage: i18n('The database is not ready') },
|
|
),
|
|
},
|
|
requires: [],
|
|
})
|
|
.addOneshot('database-prepare', {
|
|
subcontainer: appSub,
|
|
exec: { command: ['bin/rails', 'db:prepare'], env: appEnv },
|
|
requires: ['postgres'],
|
|
})
|
|
.addDaemon('app', {
|
|
subcontainer: appSub,
|
|
exec: {
|
|
command: ['bundle', 'exec', 'puma', '-C', 'config/puma.rb'],
|
|
env: appEnv,
|
|
},
|
|
ready: {
|
|
display: i18n('Todo API'),
|
|
fn: () =>
|
|
sdk.healthCheck.checkWebUrl(effects, `http://127.0.0.1:${appPort}/up`, {
|
|
successMessage: i18n('The Todo API is ready'),
|
|
errorMessage: i18n('The Todo API is not ready'),
|
|
}),
|
|
},
|
|
requires: ['database-prepare'],
|
|
})
|
|
})
|