Skip to content

Commit

Permalink
fix: korjaa nextjs api-sivujen ympäristömuuttujat (#400)
Browse files Browse the repository at this point in the history
  • Loading branch information
haapamakim authored Sep 20, 2022
1 parent 955c7b3 commit 4cd293c
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 32 deletions.
14 changes: 4 additions & 10 deletions common/BaseConfig.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,8 @@
function getEnv(name) {
const value = process.env[name];
if (!value) {
throw new Error(name + "-ympäristömuuttujaa ei ole asetettu");
}
return value;
}

class BaseConfig {
static env = getEnv("ENVIRONMENT");
static env = process.env.ENVIRONMENT || "ENVIRONMENT-ympäristömuuttujaa ei ole asetettu!";
static infraEnvironment = BaseConfig.isPermanentEnvironment() ? BaseConfig.env : "dev";
static projektiTableName = "Projekti-" + process.env.ENVIRONMENT;
static internalBucketName = `hassu-${process.env.ENVIRONMENT}-internal`;

static isPermanentEnvironment() {
return ["dev", "test", "prod"].indexOf(BaseConfig.env) >= 0;
Expand All @@ -26,4 +20,4 @@ class BaseConfig {
}
}

module.exports = { BaseConfig, getEnv };
module.exports = { BaseConfig };
37 changes: 15 additions & 22 deletions deployment/lib/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { ExecException } from "child_process";
import * as ssm from "@aws-cdk/aws-ssm";
import { SSM } from "aws-sdk";
import log from "loglevel";
import { BaseConfig, getEnv } from "../../common/BaseConfig";
import { BaseConfig } from "../../common/BaseConfig";
import { readFrontendStackOutputs } from "../bin/setupEnvironment";
import { Construct } from "@aws-cdk/core";

Expand All @@ -22,18 +22,24 @@ function execShellCommand(cmd: string): Promise<string> {
});
}

function getEnv(name: string) {
const value = process.env[name];
if (!value) {
throw new Error(name + "-ympäristömuuttujaa ei ole asetettu");
}
return value;
}

export class Config extends BaseConfig {
public static readonly uploadBucketName = `hassu-${BaseConfig.env}-upload`;
public static readonly yllapitoBucketName = `hassu-${Config.env}-yllapito`;
public static readonly publicBucketName = `hassu-${Config.env}-public`;
public static readonly internalBucketName = `hassu-${Config.env}-internal`;
public static readonly archiveBucketName = `hassu-${Config.env}-archive`;
public static readonly reportBucketName = `hassu-report`;
public readonly dmzProxyEndpoint: string;
// @ts-ignore
public frontendDomainName: string;
public readonly cloudfrontCertificateArn?: string;
public static readonly projektiTableName = "Projekti-" + getEnv("ENVIRONMENT");
public static readonly feedbackTableName = "Palaute-" + getEnv("ENVIRONMENT");
public static readonly projektiArchiveTableName = "Projekti-arkisto-" + getEnv("ENVIRONMENT");
public readonly velhoEnv;
Expand Down Expand Up @@ -67,7 +73,7 @@ export class Config extends BaseConfig {
}

private getParameter(parameterName: string) {
if (Config.env === "localstack") {
if (BaseConfig.env === "localstack") {
return "";
}
return ssm.StringParameter.valueForStringParameter(this.scope, parameterName);
Expand All @@ -77,10 +83,7 @@ export class Config extends BaseConfig {
if (Config.env === "localstack") {
return "";
}
return ssm.StringParameter.valueForStringParameter(
this.scope,
this.getInfraParameterPath(parameterName, infraEnvironment)
);
return ssm.StringParameter.valueForStringParameter(this.scope, this.getInfraParameterPath(parameterName, infraEnvironment));
}

public getInfraParameterPath(parameterName: string, infraEnvironment?: string) {
Expand All @@ -91,18 +94,11 @@ export class Config extends BaseConfig {
return Config.getSecureInfraParameterInternal({ parameterName, infraEnvironment, ssm: ssmProvider });
}

public async getGlobalSecureInfraParameter(
parameterName: string,
infraEnvironment: string = BaseConfig.infraEnvironment
) {
public async getGlobalSecureInfraParameter(parameterName: string, infraEnvironment: string = BaseConfig.infraEnvironment) {
return Config.getSecureInfraParameterInternal({ parameterName, infraEnvironment, ssm: globalSsmProvider });
}

private static async getSecureInfraParameterInternal(params: {
parameterName: string;
infraEnvironment: string;
ssm: SSM;
}) {
private static async getSecureInfraParameterInternal(params: { parameterName: string; infraEnvironment: string; ssm: SSM }) {
// Skip AWS API calls if running locally with localstack and cdklocal
if (Config.env === "localstack") {
return "dummy";
Expand All @@ -129,13 +125,10 @@ export class Config extends BaseConfig {
}

private init = async () => {
this.branch = process.env.BUILD_BRANCH
? process.env.BUILD_BRANCH
: await execShellCommand("git rev-parse --abbrev-ref HEAD");
this.branch = process.env.BUILD_BRANCH ? process.env.BUILD_BRANCH : await execShellCommand("git rev-parse --abbrev-ref HEAD");

if (Config.isDeveloperEnvironment()) {
this.frontendDomainName =
(await readFrontendStackOutputs()).CloudfrontPrivateDNSName || "please-re-run-backend-deployment";
this.frontendDomainName = (await readFrontendStackOutputs()).CloudfrontPrivateDNSName || "please-re-run-backend-deployment";
} else {
this.frontendDomainName = await this.getSecureInfraParameter("FrontendDomainName");
}
Expand Down
1 change: 1 addition & 0 deletions deployment/lib/hassu-frontend.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ export class HassuFrontendStack extends cdk.Stack {
minifyHandlers: true,
args: ["build"],
env: {
// Nämä muuttujat pitää välittää toteutukselle next.config.js:n kautta
ENVIRONMENT: Config.env,
FRONTEND_DOMAIN_NAME: config.frontendDomainName,
REACT_APP_API_KEY: this.appSyncAPIKey,
Expand Down
2 changes: 2 additions & 0 deletions next.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,8 @@ module.exports = (phase) => {
NEXT_PUBLIC_VELHO_BASE_URL: process.env.NEXT_PUBLIC_VELHO_BASE_URL,
INFRA_ENVIRONMENT: BaseConfig.infraEnvironment,
ENVIRONMENT: BaseConfig.env,
TABLE_PROJEKTI: BaseConfig.projektiTableName,
INTERNAL_BUCKET_NAME: BaseConfig.internalBucketName,
};
/**
* @type {import("next").NextConfig}
Expand Down

0 comments on commit 4cd293c

Please sign in to comment.