Skip to content

Commit

Permalink
feat: api fixes (#11)
Browse files Browse the repository at this point in the history
* fix: request desc
* fix: faulty test parameters
* chore: env vars rearrange
  • Loading branch information
oshinongit authored Nov 28, 2023
1 parent b824df0 commit 4653d46
Show file tree
Hide file tree
Showing 5 changed files with 105 additions and 48 deletions.
66 changes: 32 additions & 34 deletions aws/ecs/taskdef-dev.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,46 +2,44 @@
"executionRoleArn": "arn:aws:iam::590877988961:role/ecsTaskExecutionRole",
"containerDefinitions": [
{
"name": "intercom-manager",
"image": "590877988961.dkr.ecr.eu-north-1.amazonaws.com/intercom-manager:sha-dd68274",
"cpu": 256,
"portMappings": [
{
"name": "intercom-manager-8000-tcp",
"containerPort": 8000,
"hostPort": 8000,
"protocol": "tcp",
"appProtocol": "http"
}
],
"environment": [
{
"name": "SMB_ADDRESS",
"value": "http://13.49.224.46:8280"
}
],
"environmentFiles": [],
"mountPoints": [],
"volumesFrom": [],
"ulimits": [],
"logConfiguration": {
"logDriver": "awslogs",
"options": {
"awslogs-create-group": "true",
"awslogs-group": "/ecs/intercom-manager",
"awslogs-region": "eu-north-1",
"awslogs-stream-prefix": "ecs"
},
"secretOptions": []
"name": "intercom-manager",
"image": "590877988961.dkr.ecr.eu-north-1.amazonaws.com/intercom-manager:sha-dd68274",
"cpu": 256,
"portMappings": [
{
"name": "intercom-manager-8000-tcp",
"containerPort": 8000,
"hostPort": 8000,
"protocol": "tcp",
"appProtocol": "http"
}
],
"environment": [
{
"name": "SMB_ADDRESS",
"value": "http://13.49.224.46:8280"
}
],
"environmentFiles": [],
"mountPoints": [],
"volumesFrom": [],
"ulimits": [],
"logConfiguration": {
"logDriver": "awslogs",
"options": {
"awslogs-create-group": "true",
"awslogs-group": "/ecs/intercom-manager",
"awslogs-region": "eu-north-1",
"awslogs-stream-prefix": "ecs"
},
"secretOptions": []
}
}
],
"family": "intercom-manager",
"networkMode": "awsvpc",
"volumes": [],
"requiresCompatibilities": [
"FARGATE"
],
"requiresCompatibilities": ["FARGATE"],
"cpu": "1024",
"memory": "3072",
"runtimePlatform": {
Expand Down
6 changes: 5 additions & 1 deletion src/api.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@ import api from './api';

describe('api', () => {
it('responds with hello, world!', async () => {
const server = api({ title: 'my awesome service' });
const server = api({
title: 'my awesome service',
smbServerBaseUrl: 'http://localhost',
endpointIdleTimeout: '60'
});
const response = await server.inject({
method: 'GET',
url: '/'
Expand Down
7 changes: 6 additions & 1 deletion src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ const healthcheck: FastifyPluginCallback<HealthcheckOptions> = (

export interface ApiOptions {
title: string;
smbServerBaseUrl: string;
endpointIdleTimeout: string;
}

export default (opts: ApiOptions) => {
Expand All @@ -65,7 +67,10 @@ export default (opts: ApiOptions) => {

api.register(healthcheck, { title: opts.title });
// register other API routes here
api.register(apiProductions);
api.register(apiProductions, {
smbServerBaseUrl: opts.smbServerBaseUrl,
endpointIdleTimeout: opts.endpointIdleTimeout
});

return api;
};
59 changes: 48 additions & 11 deletions src/api_productions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,6 @@ type Production = Static<typeof Production>;
type Line = Static<typeof Line>;

const productionManager = new ProductionManager();
const ENDPOINT_IDLE_TIMEOUT_S: string =
process.env.ENDPOINT_IDLE_TIMEOUT_S ?? '180';
const SMB_ADDRESS: string = process.env.SMB_ADDRESS ?? 'http://localhost:8080';

function generateOffer(
endpoint: SmbEndpointDescription,
Expand Down Expand Up @@ -67,15 +64,16 @@ async function createEndpoint(
lineId: string,
endpointId: string,
audio: boolean,
data: boolean
data: boolean,
endpointIdleTimeout: number
): Promise<SmbEndpointDescription> {
const endpoint: SmbEndpointDescription = await smb.allocateEndpoint(
smbServerUrl,
lineId,
endpointId,
audio,
data,
parseInt(ENDPOINT_IDLE_TIMEOUT_S, 10)
endpointIdleTimeout
);
return endpoint;
}
Expand Down Expand Up @@ -196,8 +194,20 @@ function getLine(productionLines: Line[], name: string): Line {
return line;
}

const apiProductions: FastifyPluginCallback = (fastify, opts, next) => {
const smbServerUrl = SMB_ADDRESS + '/conferences/';
export interface ApiProductionsOptions {
smbServerBaseUrl: string;
endpointIdleTimeout: string;
}

const apiProductions: FastifyPluginCallback<ApiProductionsOptions> = (
fastify,
opts,
next
) => {
const smbServerUrl = new URL(
'/conferences',
opts.smbServerBaseUrl
).toString();
const smb = new SmbProtocol();

fastify.post<{
Expand Down Expand Up @@ -234,13 +244,14 @@ const apiProductions: FastifyPluginCallback = (fastify, opts, next) => {
fastify.get<{
Reply: string[] | string;
}>(
'/productions/lines',
'/lines',
{
schema: {
// description: 'Retrieves all active Production lines.',
// description: 'Retrieves all active lines.',
response: {
200: Type.Array(Type.String())
}
},
hide: true
}
},
async (request, reply) => {
Expand All @@ -263,6 +274,31 @@ const apiProductions: FastifyPluginCallback = (fastify, opts, next) => {
}
);

fastify.get<{
Params: { name: string };
Reply: Line[] | string;
}>(
'/productions/:name/lines',
{
schema: {
// description: 'Retrieves lines for a Production.',
response: {
200: Type.Object({ Line })
}
}
},
async (request, reply) => {
try {
const production: Production = getProduction(request.params.name);
reply.code(200).send(production.lines);
} catch (err) {
reply
.code(500)
.send('Exception thrown when trying to get line: ' + err);
}
}
);

fastify.get<{
Params: { name: string; lineName: string };
Reply: Line | string;
Expand Down Expand Up @@ -321,7 +357,8 @@ const apiProductions: FastifyPluginCallback = (fastify, opts, next) => {
line.id,
request.params.username,
true,
false
false,
parseInt(opts.endpointIdleTimeout, 10)
);
if (!endpoint.audio) {
throw new Error('Missing audio when creating sdp offer for endpoint');
Expand Down
15 changes: 14 additions & 1 deletion src/server.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,19 @@
import api from './api';

const server = api({ title: 'intercom-manager' });
const SMB_ADDRESS: string = process.env.SMB_ADDRESS ?? 'http://localhost:8080';

if (!process.env.SMB_ADDRESS) {
console.warn('SMB_ADDRESS environment variable not set, using defaults');
}

const ENDPOINT_IDLE_TIMEOUT_S: string =
process.env.ENDPOINT_IDLE_TIMEOUT_S ?? '180';

const server = api({
title: 'intercom-manager',
smbServerBaseUrl: SMB_ADDRESS,
endpointIdleTimeout: ENDPOINT_IDLE_TIMEOUT_S
});

const PORT = process.env.PORT ? Number(process.env.PORT) : 8000;

Expand Down

0 comments on commit 4653d46

Please sign in to comment.