Skip to content

Commit

Permalink
feat: connecting thread forum bucket and dynamodb (#326)
Browse files Browse the repository at this point in the history
* feat: adding connection to db for forum images and commenting out graphql stuff

* chore: commenting out graphql stuff

* chore: fixing identical names

* chore: fixing identical construct names in forumthreadfunctions

* chore: identical identifier in forum thread functions
  • Loading branch information
JasonNotJson authored Sep 21, 2023
1 parent 5ea40aa commit 2df6f4a
Show file tree
Hide file tree
Showing 7 changed files with 430 additions and 280 deletions.
178 changes: 110 additions & 68 deletions lib/constructs/business/api-endpoint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,39 @@ import * as flatted from 'flatted';
import { defaultHeaders } from '../../configs/api-gateway/cors';
import { STAGE } from '../../configs/common/aws';
import { API_DOMAIN } from '../../configs/route53/domain';
import { GraphqlApiService } from './graphql-api-service';
// import { GraphqlApiService } from './graphql-api-service';
import { AbstractHttpApiService } from './http-api-service';
import { RestApiService } from './rest-api-service';
import { GraphqlApiServiceId, graphqlApiServiceMap, RestApiServiceId, restApiServiceMap } from './service';
import {
// GraphqlApiServiceId,
// graphqlApiServiceMap,
RestApiServiceId,
restApiServiceMap,
} from './service';

export interface ServiceDataSources {
tableName?: string;
bucketName?: string;
}

export interface ApiEndpointProps {
zone: route53.IHostedZone;
authProvider?: cognito.IUserPool;
}

export abstract class AbstractApiEndpoint extends Construct {
abstract readonly apiEndpoint: apigw.RestApi | apigw.LambdaRestApi | apigw.SpecRestApi | apigw2.HttpApi | appsync.GraphqlApi;
abstract readonly apiEndpoint:
| apigw.RestApi
| apigw.LambdaRestApi
| apigw.SpecRestApi
| apigw2.HttpApi
| appsync.GraphqlApi;

protected constructor(scope: Construct, id: string, props?: ApiEndpointProps) {
protected constructor(
scope: Construct,
id: string,
props?: ApiEndpointProps,
) {
super(scope, id);
}
}
Expand All @@ -44,53 +63,72 @@ export abstract class AbstractRestApiEndpoint extends AbstractApiEndpoint {
}

public getDomain(): string {
const domainName: apigw.DomainName | undefined = this.apiEndpoint.domainName;
const domainName: apigw.DomainName | undefined =
this.apiEndpoint.domainName;

if (typeof domainName === 'undefined') {
throw RangeError('Domain not configured for this API endpoint.');
}
return domainName.domainName;
}

public addService(name: RestApiServiceId, dataSource?: string, auth = false): this {
this.apiServices[name] = new restApiServiceMap[name](this, `${ name }-api`, {
public addService(
name: RestApiServiceId,
dataSource?: ServiceDataSources,
auth = false,
): this {
this.apiServices[name] = new restApiServiceMap[name](this, `${name}-api`, {
dataSource: dataSource,
authorizer: auth ? this.authorizer : undefined,
validator: this.reqValidator,
});
return this;
}

public abstract deploy(): void
public abstract deploy(): void;
}

export abstract class AbstractGraphqlEndpoint extends AbstractApiEndpoint {
abstract readonly apiEndpoint: appsync.GraphqlApi;
// export abstract class AbstractGraphqlEndpoint extends AbstractApiEndpoint {
// abstract readonly apiEndpoint: appsync.GraphqlApi;

readonly apiServices: { [name: string]: GraphqlApiService };
// readonly apiServices: { [name: string]: GraphqlApiService };

protected authMode: { [mode: string]: appsync.AuthorizationMode } = {};
// protected authMode: { [mode: string]: appsync.AuthorizationMode } = {};

protected constructor(scope: Construct, id: string, props: ApiEndpointProps) {
super(scope, id, props);
}
// protected constructor(scope: Construct, id: string, props: ApiEndpointProps) {
// super(scope, id, props);
// }

public addService(name: GraphqlApiServiceId, dataSource: string, auth = 'apiKey'): this {
this.apiServices[name] = new graphqlApiServiceMap[name](this, `${ name }-api`, {
dataSource: dynamodb.Table.fromTableName(this, `${ name }-table`, dataSource),
auth: this.authMode[auth],
});
return this;
}
// public addService(
// name: GraphqlApiServiceId,
// dataSource: string,
// auth = "apiKey"
// ): this {
// this.apiServices[name] = new graphqlApiServiceMap[name](
// this,
// `${name}-api`,
// {
// dataSource: dynamodb.Table.fromTableName(
// this,
// `${name}-table`,
// dataSource
// ),
// auth: this.authMode[auth],
// }
// );
// return this;
// }

public getDomain(): string {
const domain = this.apiEndpoint.graphqlUrl.match(/https:\/\/(.*)\/graphql/g);
if (domain === null) {
return '';
}
return domain[1];
}
}
// public getDomain(): string {
// const domain = this.apiEndpoint.graphqlUrl.match(
// /https:\/\/(.*)\/graphql/g
// );
// if (domain === null) {
// return "";
// }
// return domain[1];
// }
// }

export abstract class AbstractHttpApiEndpoint extends AbstractApiEndpoint {
abstract readonly apiEndpoint: apigw2.HttpApi;
Expand Down Expand Up @@ -171,7 +209,9 @@ export class WasedaTimeRestApiEndpoint extends AbstractRestApiEndpoint {
});
new route53.ARecord(this, 'alias-record', {
zone: props.zone,
target: route53.RecordTarget.fromAlias(new route53_targets.ApiGatewayDomain(this.domain)),
target: route53.RecordTarget.fromAlias(
new route53_targets.ApiGatewayDomain(this.domain),
),
recordName: API_DOMAIN,
});
}
Expand All @@ -186,7 +226,10 @@ export class WasedaTimeRestApiEndpoint extends AbstractRestApiEndpoint {
api: this.apiEndpoint,
retainDeployments: false,
});
const hash = Buffer.from(flatted.stringify(this.apiServices), 'binary').toString('base64');
const hash = Buffer.from(
flatted.stringify(this.apiServices),
'binary',
).toString('base64');
if (STAGE === 'dev') {
devDeployment.addToLogicalId(hash);
} else if (STAGE === 'prod') {
Expand Down Expand Up @@ -227,42 +270,41 @@ export class WasedaTimeRestApiEndpoint extends AbstractRestApiEndpoint {
}
}

export class WasedaTimeGraphqlEndpoint extends AbstractGraphqlEndpoint {
readonly apiEndpoint: appsync.GraphqlApi;
readonly apiServices: { [name: string]: GraphqlApiService } = {};
// export class WasedaTimeGraphqlEndpoint extends AbstractGraphqlEndpoint {
// readonly apiEndpoint: appsync.GraphqlApi;
// readonly apiServices: { [name: string]: GraphqlApiService } = {};

constructor(scope: Construct, id: string, props: ApiEndpointProps) {

super(scope, id, props);
// constructor(scope: Construct, id: string, props: ApiEndpointProps) {
// super(scope, id, props);

const apiKeyAuth: appsync.AuthorizationMode = {
authorizationType: appsync.AuthorizationType.API_KEY,
apiKeyConfig: {
name: 'dev',
expires: Expiration.after(Duration.days(365)),
description: 'API Key for development environment.',
},
};
this.authMode.apiKey = apiKeyAuth;
const cognitoAuth: appsync.AuthorizationMode = {
authorizationType: appsync.AuthorizationType.USER_POOL,
userPoolConfig: {
userPool: props.authProvider!,
appIdClientRegex: 'web-app',
},
};
this.authMode.userPool = cognitoAuth;
// const apiKeyAuth: appsync.AuthorizationMode = {
// authorizationType: appsync.AuthorizationType.API_KEY,
// apiKeyConfig: {
// name: "dev",
// expires: Expiration.after(Duration.days(365)),
// description: "API Key for development environment.",
// },
// };
// this.authMode.apiKey = apiKeyAuth;
// const cognitoAuth: appsync.AuthorizationMode = {
// authorizationType: appsync.AuthorizationType.USER_POOL,
// userPoolConfig: {
// userPool: props.authProvider!,
// appIdClientRegex: "web-app",
// },
// };
// this.authMode.userPool = cognitoAuth;

this.apiEndpoint = new appsync.GraphqlApi(this, 'graphql-api', {
name: 'wasedatime-gql-api',
authorizationConfig: {
defaultAuthorization: apiKeyAuth,
additionalAuthorizationModes: [cognitoAuth],
},
logConfig: {
fieldLogLevel: appsync.FieldLogLevel.ALL,
},
xrayEnabled: true,
});
}
}
// this.apiEndpoint = new appsync.GraphqlApi(this, "graphql-api", {
// name: "wasedatime-gql-api",
// authorizationConfig: {
// defaultAuthorization: apiKeyAuth,
// additionalAuthorizationModes: [cognitoAuth],
// },
// logConfig: {
// fieldLogLevel: appsync.FieldLogLevel.ALL,
// },
// xrayEnabled: true,
// });
// }
// }
Loading

0 comments on commit 2df6f4a

Please sign in to comment.