Skip to content

Commit 7808b76

Browse files
committed
fixup! refactor: replace nginx-serve with web-app-serve
1 parent 8bf1087 commit 7808b76

File tree

7 files changed

+75
-77
lines changed

7 files changed

+75
-77
lines changed

Dockerfile

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -21,25 +21,27 @@ RUN --mount=type=cache,id=pnpm,target=/pnpm/store pnpm install --frozen-lockfile
2121

2222
COPY . /code/
2323

24-
# Dynamic configs. Can be changed with containers. (Placeholder values)
25-
ENV APP_TITLE=APP_TITLE_PLACEHOLDER
26-
ENV APP_ENVIRONMENT=APP_ENVIRONMENT_PLACEHOLDER
27-
ENV APP_MAPBOX_ACCESS_TOKEN=APP_MAPBOX_ACCESS_TOKEN_PLACEHOLDER
28-
ENV APP_TINY_API_KEY=APP_TINY_API_KEY_PLACEHOLDER
29-
ENV APP_API_ENDPOINT=https://APP-API-ENDPOINT-PLACEHOLDER.COM/
30-
ENV APP_RISK_API_ENDPOINT=https://APP-RISK-API-ENDPOINT-PLACEHOLDER.COM/
31-
ENV APP_SDT_URL=https://APP-SDT-URL-PLACEHOLDER.COM/
32-
ENV APP_SENTRY_DSN=https://APP-SENTRY-DSN-PLACEHOLDER.COM
33-
34-
# Static configs (Configured when building docker image)
35-
ARG APP_SENTRY_TRACES_SAMPLE_RATE=
36-
ENV APP_SENTRY_TRACES_SAMPLE_RATE=${APP_SENTRY_TRACES_SAMPLE_RATE}
37-
ARG APP_SENTRY_REPLAYS_SESSION_SAMPLE_RATE=
38-
ENV APP_SENTRY_REPLAYS_SESSION_SAMPLE_RATE=${APP_SENTRY_REPLAYS_SESSION_SAMPLE_RATE}
39-
ARG APP_SENTRY_REPLAYS_ON_ERROR_SAMPLE_RATE=
40-
ENV APP_SENTRY_REPLAYS_ON_ERROR_SAMPLE_RATE=${APP_SENTRY_REPLAYS_ON_ERROR_SAMPLE_RATE}
41-
42-
RUN pnpm build
24+
# Configuration Example
25+
# These environment variables can be dynamicallly updated in containers.
26+
# NOTE: These variables are not included in the build but they should still be valid
27+
# See the schema field in ./app/env.ts
28+
ENV APP_TITLE=GO
29+
ENV APP_ENVIRONMENT=staging
30+
ENV APP_API_ENDPOINT=https://goadmin-example.ifrc.org/api/
31+
ENV APP_ADMIN_URL=https://goadmin-example.ifrc.org/admin/
32+
ENV APP_MAPBOX_ACCESS_TOKEN=example-mapbox-access-token
33+
ENV APP_TINY_API_KEY=example-tiny-api-key
34+
ENV APP_RISK_API_ENDPOINT=https://go-risk-example.northeurope.cloudapp.azure.com/api/
35+
ENV APP_SDT_URL=https://surveydesigner-example-api.ifrc.org
36+
ENV APP_SENTRY_DSN=https://[email protected]/123
37+
ENV APP_SENTRY_TRACES_SAMPLE_RATE=0.2
38+
ENV APP_SENTRY_REPLAYS_SESSION_SAMPLE_RATE=0.2
39+
ENV APP_SENTRY_REPLAYS_ON_ERROR_SAMPLE_RATE=0.2
40+
ENV APP_GOOGLE_ANALYTICS_ID=example-google-analytics-id
41+
42+
# NOTE: WEB_APP_SERVE_ENABLED will skip defining the above env variables
43+
# See overrideDefine field in ./app/env.ts
44+
RUN WEB_APP_SERVE_ENABLED=true pnpm build
4345

4446
# ---------------------------------------------------------------------------
4547
FROM ghcr.io/toggle-corp/web-app-serve:v0.1.1 AS web-app-serve
@@ -48,8 +50,6 @@ LABEL maintainer="IFRC"
4850
LABEL org.opencontainers.image.source="https://github.com/IFRCGo/go-web-app"
4951

5052
# NOTE: Used by apply-config.sh
51-
ENV APPLY_CONFIG__APPLY_CONFIG_PATH=/code/apply-config.sh
5253
ENV APPLY_CONFIG__SOURCE_DIRECTORY=/code/build/
5354

5455
COPY --from=web-app-build /code/build "$APPLY_CONFIG__SOURCE_DIRECTORY"
55-
COPY ./web-app-serve/apply-config.sh "$APPLY_CONFIG__APPLY_CONFIG_PATH"

app/env.ts

Lines changed: 43 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,49 @@
1-
import { defineConfig, Schema } from '@julr/vite-plugin-validate-env';
1+
import { Schema, defineConfig } from '@julr/vite-plugin-validate-env';
2+
3+
const webAppServeEnabled = process.env.WEB_APP_SERVE_ENABLED?.toLowerCase() === 'true';
4+
if (webAppServeEnabled) {
5+
console.warn('Building application for WEB_APP_SERVE')
6+
}
27

38
export default defineConfig({
4-
APP_TITLE: Schema.string(),
5-
APP_ENVIRONMENT: (key, value) => {
6-
// NOTE: APP_ENVIRONMENT_PLACEHOLDER is meant to be used with image builds
7-
// The value will be later replaced with the actual value
8-
const regex = /^production|staging|testing|alpha-\d+|development|APP_ENVIRONMENT_PLACEHOLDER$/;
9-
const valid = !!value && (value.match(regex) !== null);
10-
if (!valid) {
11-
throw new Error(`Value for environment variable "${key}" must match regex "${regex}", instead received "${value}"`);
9+
validator: "builtin",
10+
schema: {
11+
APP_TITLE: Schema.string(),
12+
APP_ENVIRONMENT: (key, value) => {
13+
const regex = /^production|staging|testing|alpha-\d+|development$/;
14+
const valid = !!value && (value.match(regex) !== null);
15+
if (!valid) {
16+
throw new Error(`Value for environment variable "${key}" must match regex "${regex}", instead received "${value}"`);
17+
}
18+
return value as ('production' | 'staging' | 'testing' | `alpha-${number}` | 'development');
19+
},
20+
APP_API_ENDPOINT: Schema.string({ format: 'url', protocol: true, tld: false }),
21+
APP_ADMIN_URL: Schema.string.optional({ format: 'url', protocol: true, tld: false }),
22+
APP_MAPBOX_ACCESS_TOKEN: Schema.string(),
23+
APP_TINY_API_KEY: Schema.string(),
24+
APP_RISK_API_ENDPOINT: Schema.string({ format: 'url', protocol: true }),
25+
APP_SDT_URL: Schema.string.optional({ format: 'url', protocol: true, tld: false }),
26+
APP_SENTRY_DSN: Schema.string.optional(),
27+
APP_SENTRY_TRACES_SAMPLE_RATE: Schema.number.optional(),
28+
APP_SENTRY_REPLAYS_SESSION_SAMPLE_RATE: Schema.number.optional(),
29+
APP_SENTRY_REPLAYS_ON_ERROR_SAMPLE_RATE: Schema.number.optional(),
30+
APP_GOOGLE_ANALYTICS_ID: Schema.string.optional(),
31+
},
32+
overrideDefine: (key, value) => {
33+
// Default:
34+
if (!webAppServeEnabled) {
35+
return JSON.stringify(value);
1236
}
13-
if (value === 'APP_ENVIRONMENT_PLACEHOLDER') {
14-
console.warn(`Using ${value} for app environment. Make sure to not use this for builds without helm chart`)
37+
// Override: Skip defining env variables if web app serve is enabled
38+
if (value === null || value === undefined) {
39+
// NOTE: value should always be defined during build
40+
throw `Value for ${key} should not be null or undefined`;
1541
}
16-
return value as ('production' | 'staging' | 'testing' | `alpha-${number}` | 'development' | 'APP_ENVIRONMENT_PLACEHOLDER');
42+
const replacement_str = `WEB_APP_SERVE_PLACEHOLDER__${key}`;
43+
// NOTE: For string values, we need to stringify 'replacement_str'
44+
// This adds double quotes around the replacement string
45+
return typeof value === 'string'
46+
? JSON.stringify(replacement_str)
47+
: replacement_str;
1748
},
18-
APP_API_ENDPOINT: Schema.string({ format: 'url', protocol: true, tld: false }),
19-
APP_ADMIN_URL: Schema.string.optional({ format: 'url', protocol: true, tld: false }),
20-
APP_MAPBOX_ACCESS_TOKEN: Schema.string(),
21-
APP_TINY_API_KEY: Schema.string(),
22-
APP_RISK_API_ENDPOINT: Schema.string({ format: 'url', protocol: true }),
23-
APP_SDT_URL: Schema.string.optional({ format: 'url', protocol: true, tld: false }),
24-
APP_SENTRY_DSN: Schema.string.optional(),
25-
APP_SENTRY_TRACES_SAMPLE_RATE: Schema.number.optional(),
26-
APP_SENTRY_REPLAYS_SESSION_SAMPLE_RATE: Schema.number.optional(),
27-
APP_SENTRY_REPLAYS_ON_ERROR_SAMPLE_RATE: Schema.number.optional(),
28-
APP_GOOGLE_ANALYTICS_ID: Schema.string.optional(),
2949
});

app/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@
6262
"@eslint/eslintrc": "^3.1.0",
6363
"@eslint/js": "^9.20.0",
6464
"@eslint/json": "^0.5.0",
65-
"@julr/vite-plugin-validate-env": "^2.2.0",
65+
"@julr/vite-plugin-validate-env": "git+https://github.com/toggle-corp/vite-plugin-validate-env#97fc110",
6666
"@types/file-saver": "^2.0.5",
6767
"@types/mapbox-gl": "^1.13.0",
6868
"@types/node": "^20.11.6",

pnpm-lock.yaml

Lines changed: 6 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

web-app-serve/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
.env

web-app-serve/apply-config.sh

Lines changed: 0 additions & 12 deletions
This file was deleted.

web-app-serve/docker-compose.yml

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,7 @@ services:
77
target: web-app-serve
88
environment:
99
APPLY_CONFIG__ENABLE_DEBUG: true
10-
# App configs (Placeholder replacement)
11-
APP_TITLE: ${APP_TITLE:-GO API}
12-
APP_ENVIRONMENT: ${APP_ENVIRONMENT:-Staging}
13-
APP_MAPBOX_ACCESS_TOKEN: ${APP_MAPBOX_ACCESS_TOKEN:-my-secret-mapbox-access-token}
14-
APP_TINY_API_KEY: ${APP_TINY_API_KEY:-my-secret-tiny-api-key}
15-
APP_API_ENDPOINT: ${APP_API_ENDPOINT:-https://goadmin-stage.ifrc.org/api/}
16-
APP_RISK_API_ENDPOINT: ${APP_RISK_API_ENDPOINT:-https://go-risk-staging.northeurope.cloudapp.azure.com/api/}
17-
APP_SDT_URL: ${APP_SDT_URL:-https://surveydesigner-stage-api.ifrc.org}
18-
APP_SENTRY_DSN: ${APP_SENTRY_DSN:-https://[email protected]/123}
10+
# NOTE: See ../Dockerfile for example values and env variables
11+
env_file: .env
1912
ports:
20-
- '8001:80'
21-
develop:
22-
watch:
23-
- action: sync+restart
24-
path: ./apply-config.sh
25-
target: /code/apply-config.sh
13+
- '8050:80'

0 commit comments

Comments
 (0)