Skip to content

Commit

Permalink
Postgres Terraform config + app wiring
Browse files Browse the repository at this point in the history
  • Loading branch information
danielnaab committed Aug 19, 2024
1 parent e217878 commit 36fafa5
Show file tree
Hide file tree
Showing 7 changed files with 77 additions and 65 deletions.
16 changes: 13 additions & 3 deletions apps/server-doj/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,20 @@
import { createCustomServer } from './server.js';
import { createPostgresDatabaseContext } from '@atj/database';

const port = process.env.PORT || 4321;

const database = createPostgresDatabaseContext();
createCustomServer({ db: database }).then((server: any) =>
const getCloudGovServerSecrets = () => {
if (process.env.VCAP_SERVICES === undefined) {
throw new Error('VCAP_SERVICES not found');
}
const services = JSON.parse(process.env.VCAP_SERVICES || '{}');
return {
//loginGovClientSecret: services['user-provided']?.credentials?.SECRET_LOGIN_GOV_PRIVATE_KEY,
dbUri: services['aws-rds'].credentials.uri as string,
};
};

const secrets = getCloudGovServerSecrets();
createCustomServer({ dbUri: secrets?.dbUri }).then((server: any) =>
server.listen(port, () => {
console.log(`Server running on http://localhost:${port}`);
})
Expand Down
27 changes: 5 additions & 22 deletions apps/server-doj/src/server.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,15 @@
import { join } from 'path';
import {
type DatabaseContext,
createDatabaseGateway,
createFilesystemDatabaseContext,
createPostgresDatabaseContext,
} from '@atj/database';
import { createServer } from '@atj/server';

//const getDirname = () => dirname(fileURLToPath(import.meta.url));

const createDevDatabase = async () => {
return createFilesystemDatabaseContext(join(__dirname, '../doj.db'));
};

export const createCustomServer = async (ctx: {
db: DatabaseContext;
dbUri: string;
}): Promise<any> => {
const db = createDatabaseGateway(ctx.db || createDevDatabase());
const db = createDatabaseGateway(
await createPostgresDatabaseContext(ctx.dbUri)
);

return createServer({
title: 'DOJ Form Service',
Expand All @@ -28,14 +22,3 @@ export const createCustomServer = async (ctx: {
},
});
};

/*
const getServerSecrets = () => {
const services = JSON.parse(process.env.VCAP_SERVICES || '{}');
const loginClientSecret =
services['user-provided']?.credentials?.SECRET_LOGIN_GOV_PRIVATE_KEY;
return {
loginGovClientSecret: loginClientSecret,
};
};
*/
6 changes: 4 additions & 2 deletions apps/server-doj/tests/integration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,16 @@ import { describe, expect, test } from 'vitest';
import { describeDatabase } from '@atj/database/testing';

import { createCustomServer } from '../src/server';

describe('DOJ Form Service', () => {
test('renders the home page', async () => {
test('avoid "No test suite found in file" error', async () => {
expect(true).toBe(true);
});
});

describeDatabase('DOJ Form Service', () => {
test('renders the home page', async ({ db }) => {
const app = await createCustomServer({ db: db.ctx });
const app = await createCustomServer({ dbUri: db.ctx.connectionUri });
const response = await request(app).get('/');
expect(response.ok).toBe(true);
expect(response.text).toMatch(/DOJ Form Service/);
Expand Down
23 changes: 18 additions & 5 deletions apps/server-kansas/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,21 @@
import { createCustomServer } from './server';
import { createCustomServer } from './server.js';

const port = process.env.PORT || 4321;
const app = await createCustomServer();

app.listen(port, () => {
console.log(`Server running on http://localhost:${port}`);
});
const getCloudGovServerSecrets = () => {
if (process.env.VCAP_SERVICES === undefined) {
throw new Error('VCAP_SERVICES not found');
}
const services = JSON.parse(process.env.VCAP_SERVICES || '{}');
return {
//loginGovClientSecret: services['user-provided']?.credentials?.SECRET_LOGIN_GOV_PRIVATE_KEY,
dbUri: services['aws-rds'].credentials.uri as string,
};
};

const secrets = getCloudGovServerSecrets();
createCustomServer({ dbUri: secrets?.dbUri }).then((server: any) =>
server.listen(port, () => {
console.log(`Server running on http://localhost:${port}`);
})
);
35 changes: 11 additions & 24 deletions apps/server-kansas/src/server.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,18 @@
import path, { dirname } from 'path';
import { fileURLToPath } from 'url';
import {
createDatabaseGateway,
createPostgresDatabaseContext,
} from '@atj/database';
import { createServer } from '@atj/server';

const getDirname = () => dirname(fileURLToPath(import.meta.url));

export const createCustomServer = async (): Promise<any> => {
const { createFilesystemDatabaseContext, createDatabaseGateway } =
await import('@atj/database');
const { createServer } = await import('@atj/server');

const dbCtx = await createFilesystemDatabaseContext(
path.join(getDirname(), '../doj.db')
export const createCustomServer = async (ctx: {
dbUri: string;
}): Promise<any> => {
const db = createDatabaseGateway(
await createPostgresDatabaseContext(ctx.dbUri)
);
const db = createDatabaseGateway(dbCtx);

return createServer({
title: 'KS Courts Form Service',
title: 'DOJ Form Service',
db,
loginGovOptions: {
loginGovUrl: 'https://idp.int.identitysandbox.gov',
Expand All @@ -24,14 +22,3 @@ export const createCustomServer = async (): Promise<any> => {
},
});
};

/*
const getServerSecrets = () => {
const services = JSON.parse(process.env.VCAP_SERVICES || '{}');
const loginClientSecret =
services['user-provided']?.credentials?.SECRET_LOGIN_GOV_PRIVATE_KEY;
return {
loginGovClientSecret: loginClientSecret,
};
};
*/
18 changes: 10 additions & 8 deletions apps/server-kansas/tests/integration.test.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
import request from 'supertest';
import { beforeAll, describe, expect, test } from 'vitest';
import { describe, expect, test } from 'vitest';
import { describeDatabase } from '@atj/database/testing';

import { createCustomServer } from '../src/server';

describe('Kansas State Courts Form Service', () => {
let app: any;

beforeAll(async () => {
app = await createCustomServer();
describe('DOJ Form Service', () => {
test('avoid "No test suite found in file" error', async () => {
expect(true).toBe(true);
});
});

test('renders the home page', async () => {
describeDatabase('Kansas State Courts Form Service', () => {
test('renders the home page', async ({ db }) => {
const app = await createCustomServer({ dbUri: db.ctx.connectionUri });
const response = await request(app).get('/');
expect(response.ok).toBe(true);
expect(response.text).toMatch(/KS Courts Form Service/);
expect(response.text).toMatch(/DOJ Form Service/);
});
});
17 changes: 16 additions & 1 deletion infra/cdktf/src/lib/cloud.gov/node-astro.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,16 +41,31 @@ export class AstroService extends Construct {
}
);

const rds =
new cloudfoundry.dataCloudfoundryService.DataCloudfoundryService(
scope,
`${id}-data-aws-rds`,
{
name: 'aws-rds',
}
);

const dbInstance = new cloudfoundry.serviceInstance.ServiceInstance(
this,
`${id}-db`,
{
name: `${id}-db`,
servicePlan: 'micro-psql',
servicePlan: rds.servicePlans.lookup('micro-psql'),
space: spaceId,
jsonParams: '{"version": "15"}',
lifecycle: {
preventDestroy: true,
},
timeouts: {
create: '60m',
update: '60m',
delete: '2h',
},
}
);

Expand Down

0 comments on commit 36fafa5

Please sign in to comment.