Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Develop Service B: Implement configureSubdomain Function #138

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
97 changes: 97 additions & 0 deletions apps/domain-manager/domainManager.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
import { Route53Client, ChangeResourceRecordSetsCommand } from '@aws-sdk/client-route-53';
import { CloudFrontClient, CreateDistributionCommand } from '@aws-sdk/client-cloudfront';

const route53Client = new Route53Client({ region: process.env.AWS_REGION });
const cloudFrontClient = new CloudFrontClient({ region: process.env.AWS_REGION });

async function configureMainDomain(domainName: string, hostedZoneId: string) {
try {
// Create DNS records for the main domain in the hosted zone
const dnsRecordParams = {
HostedZoneId: hostedZoneId,
ChangeBatch: {
Changes: [
{
Action: 'CREATE',
ResourceRecordSet: {
Name: domainName,
Type: 'A',
AliasTarget: {
HostedZoneId: hostedZoneId,
DNSName: domainName,
EvaluateTargetHealth: false,
},
},
},
],
},
};
const changeResourceRecordSetsCommand = new ChangeResourceRecordSetsCommand(dnsRecordParams);
await route53Client.send(changeResourceRecordSetsCommand);

// Create a CloudFront distribution for the main domain
const distributionParams = {
DistributionConfig: {
CallerReference: `create-distribution-${Date.now()}`,
Origins: {
Quantity: 1,
Items: [
{
Id: 'origin-1',
DomainName: domainName,
OriginPath: '',
CustomHeaders: {
Quantity: 0,
},
S3OriginConfig: {
OriginAccessIdentity: '',
},
},
],
},
DefaultCacheBehavior: {
TargetOriginId: 'origin-1',
ViewerProtocolPolicy: 'redirect-to-https',
AllowedMethods: {
Quantity: 2,
Items: ['GET', 'HEAD'],
CachedMethods: {
Quantity: 2,
Items: ['GET', 'HEAD'],
},
},
ForwardedValues: {
QueryString: false,
Cookies: {
Forward: 'none',
},
Headers: {
Quantity: 0,
},
QueryStringCacheKeys: {
Quantity: 0,
},
},
MinTTL: 0,
DefaultTTL: 86400,
MaxTTL: 31536000,
},
Comment: 'CloudFront distribution for main domain',
Enabled: true,
},
};
const createDistributionCommand = new CreateDistributionCommand(distributionParams);
const distributionResponse = await cloudFrontClient.send(createDistributionCommand);

return {
domainName,
distributionId: distributionResponse.Distribution.Id,
distributionDomainName: distributionResponse.Distribution.DomainName,
};
} catch (error) {
console.error('Error configuring main domain:', error);
throw error;
}
}

export { configureMainDomain };
Empty file added apps/domain-requester/config.js
Empty file.
99 changes: 99 additions & 0 deletions apps/domain-requester/domainRequester.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
import { Route53Client, ChangeResourceRecordSetsCommand } from '@aws-sdk/client-route-53';
import { CloudFrontClient, CreateDistributionCommand } from '@aws-sdk/client-cloudfront';

const route53Client = new Route53Client({ region: process.env.AWS_REGION });
const cloudFrontClient = new CloudFrontClient({ region: process.env.AWS_REGION });

async function configureSubdomain(subdomainName, mainDomainParams) {
try {
const { hostedZoneId, distributionDomainName } = mainDomainParams;

// Create DNS records for the subdomain in the hosted zone
const dnsRecordParams = {
HostedZoneId: hostedZoneId,
ChangeBatch: {
Changes: [
{
Action: 'CREATE',
ResourceRecordSet: {
Name: subdomainName,
Type: 'A',
AliasTarget: {
HostedZoneId: hostedZoneId,
DNSName: distributionDomainName,
EvaluateTargetHealth: false,
},
},
},
],
},
};
const changeResourceRecordSetsCommand = new ChangeResourceRecordSetsCommand(dnsRecordParams);
await route53Client.send(changeResourceRecordSetsCommand);

// Create a CloudFront distribution for the subdomain if required
const distributionParams = {
DistributionConfig: {
CallerReference: `create-distribution-${Date.now()}`,
Origins: {
Quantity: 1,
Items: [
{
Id: 'origin-1',
DomainName: subdomainName,
OriginPath: '',
CustomHeaders: {
Quantity: 0,
},
S3OriginConfig: {
OriginAccessIdentity: '',
},
},
],
},
DefaultCacheBehavior: {
TargetOriginId: 'origin-1',
ViewerProtocolPolicy: 'redirect-to-https',
AllowedMethods: {
Quantity: 2,
Items: ['GET', 'HEAD'],
CachedMethods: {
Quantity: 2,
Items: ['GET', 'HEAD'],
},
},
ForwardedValues: {
QueryString: false,
Cookies: {
Forward: 'none',
},
Headers: {
Quantity: 0,
},
QueryStringCacheKeys: {
Quantity: 0,
},
},
MinTTL: 0,
DefaultTTL: 86400,
MaxTTL: 31536000,
},
Comment: 'CloudFront distribution for subdomain',
Enabled: true,
},
};
const createDistributionCommand = new CreateDistributionCommand(distributionParams);
const distributionResponse = await cloudFrontClient.send(createDistributionCommand);

return {
subdomainName,
distributionId: distributionResponse.Distribution.Id,
distributionDomainName: distributionResponse.Distribution.DomainName,
};
} catch (error) {
console.error('Error configuring subdomain:', error);
throw error;
}
}

export { configureSubdomain };
97 changes: 97 additions & 0 deletions apps/domain-requester/domainRequester.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
import { Route53Client, ChangeResourceRecordSetsCommand } from '@aws-sdk/client-route-53';
import { CloudFrontClient, CreateDistributionCommand } from '@aws-sdk/client-cloudfront';

const route53Client = new Route53Client({ region: process.env.AWS_REGION });
const cloudFrontClient = new CloudFrontClient({ region: process.env.AWS_REGION });

async function configureSubdomain(subdomainName: string, hostedZoneId: string, mainDomain: string) {
try {
// Create DNS records for the subdomain in the hosted zone
const dnsRecordParams = {
HostedZoneId: hostedZoneId,
ChangeBatch: {
Changes: [
{
Action: 'CREATE',
ResourceRecordSet: {
Name: subdomainName,
Type: 'A',
AliasTarget: {
HostedZoneId: hostedZoneId,
DNSName: mainDomain,
EvaluateTargetHealth: false,
},
},
},
],
},
};
const changeResourceRecordSetsCommand = new ChangeResourceRecordSetsCommand(dnsRecordParams);
await route53Client.send(changeResourceRecordSetsCommand);

// Create a CloudFront distribution for the subdomain
const distributionParams = {
DistributionConfig: {
CallerReference: `create-distribution-${Date.now()}`,
Origins: {
Quantity: 1,
Items: [
{
Id: 'origin-1',
DomainName: subdomainName,
OriginPath: '',
CustomHeaders: {
Quantity: 0,
},
S3OriginConfig: {
OriginAccessIdentity: '',
},
},
],
},
DefaultCacheBehavior: {
TargetOriginId: 'origin-1',
ViewerProtocolPolicy: 'redirect-to-https',
AllowedMethods: {
Quantity: 2,
Items: ['GET', 'HEAD'],
CachedMethods: {
Quantity: 2,
Items: ['GET', 'HEAD'],
},
},
ForwardedValues: {
QueryString: false,
Cookies: {
Forward: 'none',
},
Headers: {
Quantity: 0,
},
QueryStringCacheKeys: {
Quantity: 0,
},
},
MinTTL: 0,
DefaultTTL: 86400,
MaxTTL: 31536000,
},
Comment: 'CloudFront distribution for subdomain',
Enabled: true,
},
};
const createDistributionCommand = new CreateDistributionCommand(distributionParams);
const distributionResponse = await cloudFrontClient.send(createDistributionCommand);

return {
subdomainName,
distributionId: distributionResponse.Distribution.Id,
distributionDomainName: distributionResponse.Distribution.DomainName,
};
} catch (error) {
console.error('Error configuring subdomain:', error);
throw error;
}
}

export { configureSubdomain };
Empty file added apps/domain-requester/index.js
Empty file.
1 change: 1 addition & 0 deletions apps/web/src/app/client-layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ type Props = {
};

function ClientLayout({ children }: Props) {
console.log('Rendering ClientLayout');
return (
<ThemeProvider attribute="class" defaultTheme="system" enableSystem disableTransitionOnChange>
<SessionProvider refetchInterval={5 * 60} refetchOnWindowFocus={true}>
Expand Down
4 changes: 4 additions & 0 deletions apps/web/src/shared/components/header/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ const Header = () => {
const { user } = session || {};
const { email } = user || {};

console.log('Rendering Header');
console.log('Session:', session);
console.log('Network:', network);

const handleLogout = async () => {
await signOut({ callbackUrl: REDIRECT_AFTER_LOGOUT_URL });
};
Expand Down
Loading
Loading