Skip to content
This repository was archived by the owner on Nov 23, 2022. It is now read-only.

Commit 39a4125

Browse files
authored
Refactor handling of environment variables (#2303)
* Refactor environment variables Signed-off-by: Tilman Vatteroth <[email protected]>
1 parent e412115 commit 39a4125

File tree

85 files changed

+624
-461
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

85 files changed

+624
-461
lines changed

.env.development

+2
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
11
NEXT_PUBLIC_USE_MOCK_API=true
2+
HD_EDITOR_BASE_URL="http://localhost:3001/"
3+
HD_RENDERER_BASE_URL="http://127.0.0.1:3001/"

.env.production

+1
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
NEXT_PUBLIC_USE_MOCK_API=false
2+
NEXT_PUBLIC_TEST_MODE=false

.env.test

+2
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
11
NEXT_PUBLIC_USE_MOCK_API=true
2+
HD_EDITOR_BASE_URL="http://127.0.0.1:3001/"
3+
HD_RENDERER_BASE_URL="http://127.0.0.1:3001/"

.github/workflows/deploy-pr.yml

+3-3
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,13 @@ jobs:
3535
node-version: 18
3636

3737
- name: Patch files
38-
run: bash netlify/patch-files.sh
38+
run: bash netlify/patch-files.sh "${{ github.event.number }}"
3939

4040
- name: Install dependencies
4141
run: yarn install --immutable
4242

43-
- name: Build netlify variant
44-
run: yarn build:netlify
43+
- name: Build mock variant
44+
run: yarn build:mock
4545

4646
- name: Remove Next.js cache to avoid it being deployed
4747
run: rm -r .next/cache

.github/workflows/e2e.yml

+2
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,8 @@ jobs:
108108

109109
- name: Run server
110110
run: yarn start:ci &
111+
env:
112+
NODE_ENV: test
111113

112114
- name: Wait for server
113115
run: "sleep 3 && curl --max-time 120 http://127.0.0.1:3001/"

.reuse/dep5

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ Upstream-Name: react-client
33
Upstream-Contact: The HedgeDoc developers <[email protected]>
44
Source: https://github.com/hedgedoc/react-client
55

6-
Files: public/mock-public/*
6+
Files: public/public/*
77
Copyright: 2021 The HedgeDoc developers (see AUTHORS file)
88
License: CC0-1.0
99

@@ -15,7 +15,7 @@ Files: locales/*
1515
Copyright: 2021 The HedgeDoc developers (see AUTHORS file)
1616
License: CC-BY-SA-4.0
1717

18-
Files: public/mock-public/img/highres.jpg
18+
Files: public/public/img/highres.jpg
1919
Copyright: Vincent van Gogh
2020
License: CC0-1.0
2121

Dockerfile

+8-5
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,17 @@
44

55
# BUILD
66
FROM node:18-alpine AS builder
7-
ARG BUILD_VERSION=CLIENT_VERSION_MISSING
7+
ENV NODE_ENV=production
88
ENV NEXT_TELEMETRY_DISABLED=1
9+
ARG BUILD_VERSION=CLIENT_VERSION_MISSING
910

1011
WORKDIR /app
1112
COPY . ./
12-
RUN yarn install --immutable && \
13+
RUN rm -rf public/public && \
14+
rm -rf src/pages/api && \
15+
yarn install --immutable && \
1316
sed -i "s/CLIENT_VERSION_MISSING/${BUILD_VERSION}/" src/version.json && \
14-
yarn build:for-real-backend
17+
yarn build
1518

1619
# RUNNER
1720
FROM node:18-alpine
@@ -27,6 +30,6 @@ COPY --from=builder --chown=node:node /app/.next/standalone ./
2730

2831
USER node
2932

30-
ENV PORT 3000
31-
EXPOSE 3000/tcp
33+
ENV PORT 3001
34+
EXPOSE 3001/tcp
3235
CMD ["node", "server.js"]

README.md

+2
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,8 @@ This will build the app in production mode and save it into the `.next` folder.
7272
best performance, minimized and the filenames include a hash value of the content. Don't edit them by hand!
7373

7474
You can run the production build using the built-in server with `yarn start`.
75+
You MUST provide the environment variable `HD_EDITOR_BASE_URL` with protocol, domain and (if needed) path (e.g. `http://127.0.0.1:3001/`) so the app knows under which URL it is available in the browser.
76+
You can also provide `HD_RENDERER_BASE_URL` if the renderer should use another domain than the editor. This is recommended for security reasons but not mandatory.
7577

7678
## UI Test
7779

cypress/e2e/deleteNote.spec.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ describe('Delete note', () => {
1212
})
1313

1414
it('correctly deletes a note', () => {
15-
cy.intercept('DELETE', `/api/mock-backend/private/notes/${testNoteId}`, {
15+
cy.intercept('DELETE', `api/private/notes/${testNoteId}`, {
1616
statusCode: 204
1717
})
1818
cy.getByCypressId('sidebar.deleteNote.button').click()

cypress/e2e/fileUpload.spec.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ describe('File upload', () => {
1717
cy.intercept(
1818
{
1919
method: 'POST',
20-
url: '/api/mock-backend/private/media'
20+
url: 'api/private/media'
2121
},
2222
{
2323
statusCode: 201,
@@ -74,7 +74,7 @@ describe('File upload', () => {
7474
cy.intercept(
7575
{
7676
method: 'POST',
77-
url: '/api/mock-backend/private/media'
77+
url: 'api/private/media'
7878
},
7979
{
8080
statusCode: 400

cypress/e2e/history.spec.ts

+5-5
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ describe('History', () => {
2424
describe('is as given when not empty', () => {
2525
beforeEach(() => {
2626
cy.clearLocalStorage('history')
27-
cy.intercept('GET', '/api/mock-backend/private/me/history', {
27+
cy.intercept('GET', 'api/private/me/history', {
2828
body: [
2929
{
3030
identifier: 'cypress',
@@ -51,7 +51,7 @@ describe('History', () => {
5151
describe('is untitled when not empty', () => {
5252
beforeEach(() => {
5353
cy.clearLocalStorage('history')
54-
cy.intercept('GET', '/api/mock-backend/private/me/history', {
54+
cy.intercept('GET', 'api/private/me/history', {
5555
body: [
5656
{
5757
identifier: 'cypress-no-title',
@@ -84,7 +84,7 @@ describe('History', () => {
8484

8585
describe('working', () => {
8686
beforeEach(() => {
87-
cy.intercept('PUT', '/api/mock-backend/private/me/history/features', (req) => {
87+
cy.intercept('PUT', 'api/private/me/history/features', (req) => {
8888
req.reply(200, req.body)
8989
})
9090
})
@@ -106,7 +106,7 @@ describe('History', () => {
106106

107107
describe('failing', () => {
108108
beforeEach(() => {
109-
cy.intercept('PUT', '/api/mock-backend/private/me/history/features', {
109+
cy.intercept('PUT', 'api/private/me/history/features', {
110110
statusCode: 401
111111
})
112112
})
@@ -128,7 +128,7 @@ describe('History', () => {
128128
describe('Import', () => {
129129
beforeEach(() => {
130130
cy.clearLocalStorage('history')
131-
cy.intercept('GET', '/api/mock-backend/private/me/history', {
131+
cy.intercept('GET', 'api/private/me/history', {
132132
body: []
133133
})
134134
cy.visitHistory()

cypress/e2e/intro.spec.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
/* eslint-disable @typescript-eslint/no-unsafe-call */
88
describe('Intro page', () => {
99
beforeEach(() => {
10-
cy.intercept('/mock-public/intro.md', 'test content')
10+
cy.intercept('public/intro.md', 'test content')
1111
cy.visitHome()
1212
})
1313

@@ -17,7 +17,7 @@ describe('Intro page', () => {
1717
})
1818

1919
it("won't show anything if no content was found", () => {
20-
cy.intercept('/mock-public/intro.md', {
20+
cy.intercept('public/intro.md', {
2121
statusCode: 404
2222
})
2323
cy.visitHome()

cypress/e2e/motd.spec.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,13 @@ const motdMockHtml = 'This is the <strong>mock</strong> Motd call'
1111

1212
describe('Motd', () => {
1313
const mockExistingMotd = (useEtag?: boolean, content = motdMockContent) => {
14-
cy.intercept('GET', '/mock-public/motd.md', {
14+
cy.intercept('GET', 'public/motd.md', {
1515
statusCode: 200,
1616
headers: { [useEtag ? 'etag' : 'Last-Modified']: MOCK_LAST_MODIFIED },
1717
body: content
1818
})
1919

20-
cy.intercept('HEAD', '/mock-public/motd.md', {
20+
cy.intercept('HEAD', 'public/motd.md', {
2121
statusCode: 200,
2222
headers: { [useEtag ? 'etag' : 'Last-Modified']: MOCK_LAST_MODIFIED }
2323
})

cypress/e2e/profile.spec.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ describe('profile page', () => {
88
beforeEach(() => {
99
cy.intercept(
1010
{
11-
url: '/api/mock-backend/private/tokens',
11+
url: 'api/private/tokens',
1212
method: 'GET'
1313
},
1414
{
@@ -25,7 +25,7 @@ describe('profile page', () => {
2525
)
2626
cy.intercept(
2727
{
28-
url: '/api/mock-backend/private/tokens',
28+
url: 'api/private/tokens',
2929
method: 'POST'
3030
},
3131
{
@@ -42,7 +42,7 @@ describe('profile page', () => {
4242
)
4343
cy.intercept(
4444
{
45-
url: '/api/mock-backend/private/tokens/cypress',
45+
url: 'api/private/tokens/cypress',
4646
method: 'DELETE'
4747
},
4848
{

cypress/support/config.ts

+6-10
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* SPDX-FileCopyrightText: 2021 The HedgeDoc developers (see AUTHORS file)
2+
* SPDX-FileCopyrightText: 2022 The HedgeDoc developers (see AUTHORS file)
33
*
44
* SPDX-License-Identifier: AGPL-3.0-only
55
*/
@@ -14,7 +14,7 @@ declare namespace Cypress {
1414

1515
export const branding = {
1616
name: 'DEMO Corp',
17-
logo: '/mock-public/img/demo.png'
17+
logo: 'public/img/demo.png'
1818
}
1919

2020
export const authProviders = [
@@ -77,15 +77,11 @@ export const config = {
7777
commit: 'MOCK'
7878
},
7979
plantumlServer: 'http://mock-plantuml.local',
80-
maxDocumentLength: 200,
81-
iframeCommunication: {
82-
editorOrigin: 'http://127.0.0.1:3001/',
83-
rendererOrigin: 'http://127.0.0.1:3001/'
84-
}
80+
maxDocumentLength: 200
8581
}
8682

8783
Cypress.Commands.add('loadConfig', (additionalConfig?: Partial<typeof config>) => {
88-
return cy.intercept('/api/mock-backend/private/config', {
84+
return cy.intercept('api/private/config', {
8985
statusCode: 200,
9086
body: {
9187
...config,
@@ -97,11 +93,11 @@ Cypress.Commands.add('loadConfig', (additionalConfig?: Partial<typeof config>) =
9793
beforeEach(() => {
9894
cy.loadConfig()
9995

100-
cy.intercept('GET', '/mock-public/motd.md', {
96+
cy.intercept('GET', 'public/motd.md', {
10197
body: '404 Not Found!',
10298
statusCode: 404
10399
})
104-
cy.intercept('HEAD', '/mock-public/motd.md', {
100+
cy.intercept('HEAD', 'public/motd.md', {
105101
statusCode: 404
106102
})
107103
})

cypress/support/visit-test-editor.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
/*
2-
* SPDX-FileCopyrightText: 2021 The HedgeDoc developers (see AUTHORS file)
2+
* SPDX-FileCopyrightText: 2022 The HedgeDoc developers (see AUTHORS file)
33
*
44
* SPDX-License-Identifier: AGPL-3.0-only
55
*/
66
export const testNoteId = 'test'
77

88
beforeEach(() => {
9-
cy.intercept(`/api/mock-backend/private/notes/${testNoteId}`, {
9+
cy.intercept(`api/private/notes/${testNoteId}`, {
1010
content: '',
1111
metadata: {
1212
id: testNoteId,

netlify.toml

+3
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,6 @@ command = "echo Pseudo build command because the build is made by the CI"
44

55
[[plugins]]
66
package = "@netlify/plugin-nextjs"
7+
8+
[dev]
9+
targetPort = 3001

netlify/intro.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@
22
What you see is an UI-Test! It's filled with dummy data, not connected to a backend and no data will be saved.
33
:::
44

5-
![HedgeDoc Screenshot](/mock-public/screenshot.png)
5+
![HedgeDoc Screenshot](public/screenshot.png)
66

77
[![Deployed using netlify](https://www.netlify.com/img/global/badges/netlify-color-accent.svg)](https://www.netlify.com)

netlify/patch-files.sh

+4-2
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,11 @@
88
set -e
99

1010
echo 'Patch intro.md to include netlify banner.'
11-
cp netlify/intro.md public/mock-public/intro.md
11+
cp netlify/intro.md public/public/intro.md
1212
echo 'Patch motd.md to include privacy policy.'
13-
cp netlify/motd.md public/mock-public/motd.md
13+
cp netlify/motd.md public/public/motd.md
1414
echo 'Patch version.json to include git hash'
1515
jq ".version = \"0.0.0+${GITHUB_SHA:0:8}\"" src/version.json > src/_version.json
1616
mv src/_version.json src/version.json
17+
echo "Patch base URL"
18+
echo HD_EDITOR_BASE_URL="https://${1}--hedgedoc-ui-test.netlify.app/" >> .env.production

0 commit comments

Comments
 (0)