Skip to content

Commit e3f9b0a

Browse files
committed
docs: update documentation for v4
1 parent 336703a commit e3f9b0a

14 files changed

+155
-211
lines changed

README.md

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,7 @@ yarn add -D @staticdeploy/app-server
2525
- [deploy create-react-app apps with Docker](docs/deploy-cra-apps-with-docker.md)
2626
- [deploy generic static apps with Docker](docs/deploy-apps-with-docker.md)
2727

28-
### Additional documentation
28+
### Reference documentation
2929

30+
- [app-server config options](docs/app-server-config-options.md)
3031
- [how requests are routed](docs/requests-routing.md)
31-
- [how `window.APP_CONFIG` is generated](docs/config-generation.md)
32-
- [app-server configuration options](docs/app-server-configuration-options.md)

docs/app-server-config-options.md

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
## app-server config options
2+
3+
The **app-server** binary takes the following config options:
4+
5+
- `--config`: path of the javascript file exporting the **app-server** config.
6+
Defaults to `app-server.config.js`
7+
- `--root`: root directory to serve. Defaults to `build`
8+
- `--fallbackAssetPath`: absolute path (relative to the root directory) of the
9+
asset to use as fallback when requests don't match any other asset. The asset
10+
MUST exist. Defaults to `/index.html`
11+
- `--fallbackStatusCode`: status code to use when serving the fallback asset.
12+
Defaults to `200`
13+
- `--headers`: (asset matcher, headers) map specifying which headers to assign
14+
to which assets
15+
- `--configuration`: JSON configuration object for the static app, i.e. what
16+
becomes `window.APP_CONFIG`
17+
- `--configurationKeyPrefix`: Prefix of the environment variables used to
18+
generate the configuration for the static app. Defaults to `APP_CONFIG_`
19+
- `--basePath`: static app base path. Defaults to `/`
20+
- `--port`: port to listen on. Defaults to `3000`
21+
22+
Options can also be passed via config file, a javascript file exporting an
23+
object defining one or more of the above config options. Example:
24+
25+
```js
26+
module.exports = {
27+
fallbackAssetPath: "/not-found.html",
28+
fallbackStatusCode: 404
29+
};
30+
```
31+
32+
Options can also be passed as upper-cased, snake-cased, environment variables
33+
prefixed by `APP_SERVER_`. Eg:
34+
35+
```sh
36+
export APP_SERVER_FALLBACK_ASSET_PATH=...
37+
export APP_SERVER_FALLBACK_STATUS_CODE=...
38+
```
39+
40+
Option sources have the following priority:
41+
42+
1. command line flags
43+
2. environment variables
44+
3. options defined in the config file
45+
46+
Meaning for example that when an option is provided both as a command line flag
47+
and as an environment variable, the value provided with the command line flag is
48+
used.
49+
50+
## App configuration generation
51+
52+
The JSON configuration object that is injected by **app-server** into the served
53+
static app (as the global variable `window.APP_CONFIG`) is generated by merging:
54+
55+
- the JSON object passed as the `--configuration` option
56+
- a JSON object generated from environment variables using the following
57+
algorithm:
58+
- filter variables whose name doesn't start with the prefix specified by the
59+
`--configurationKeyPrefix` option
60+
- strip the prefix from the name of the remaining variables
61+
- create the object using those key-value pairs
62+
63+
### Example
64+
65+
Given the `--configuration` option:
66+
67+
```json
68+
{
69+
"KEY_0": "OPT_VALUE_0",
70+
"KEY_1": "OPT_VALUE_1"
71+
}
72+
```
73+
74+
And given the environment:
75+
76+
```sh
77+
APP_CONFIG_KEY_1=ENV_VALUE_1
78+
APP_CONFIG_KEY_2=ENV_VALUE_2
79+
NON_PREFIXED_KEY=VALUE
80+
```
81+
82+
the following `window.APP_CONFIG` is generated:
83+
84+
```js
85+
window.APP_CONFIG = {
86+
KEY_0: "OPT_VALUE_0",
87+
KEY_1: "ENV_VALUE_1",
88+
KEY_2: "ENV_VALUE_2"
89+
};
90+
```

docs/app-server-configuration-options.md

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

docs/config-generation.md

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

docs/deploy-apps-with-docker.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ FROM node
3737
# RUN npm run build
3838

3939
# Second docker stage: copy artifacts into the staticdeploy/app-server image
40-
FROM staticdeploy/app-server
40+
FROM staticdeploy/app-server:vX.Y.Z
4141

4242
# Copy artifacts from the previous stage (assuming they were produced in the
4343
# /dist directory) into the /build directory of the image, the default directory

docs/deploy-cra-apps-with-docker.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,18 @@ build a docker image for building and serving your app is using the following
66
`Dockerfile`:
77

88
```Dockerfile
9-
FROM staticdeploy/app-server:cra-builder
10-
FROM staticdeploy/app-server:cra-runtime
9+
FROM staticdeploy/app-server:vX.Y.Z-cra-builder
10+
FROM staticdeploy/app-server:vX.Y.Z-cra-runtime
1111
```
1212

1313
The first `FROM` instruction will run the `ONBUILD` instructions of the
14-
`:cra-builder` image, which will install dependencies with **npm** (or **yarn**
15-
if your project has a `yarn.lock`) and build the app by running the `build` npm
16-
script.
14+
`:vX.Y.Z-cra-builder` image, which will install dependencies with **npm** (or
15+
**yarn** if your project has a `yarn.lock`) and build the app by running the
16+
`build` npm script.
1717

1818
The second `FROM` instruction will copy the built app into the (relatively)
19-
small `:cra-runtime` image where **app-server** is installed and configured to
20-
serve the app.
19+
small `:vX.Y.Z-cra-runtime` image where **app-server** is installed and
20+
configured to serve the app.
2121

2222
You can then run your app image passing in configuration via environment
2323
variables:

docs/usage-with-cra.md

Lines changed: 13 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,14 @@
77
First:
88

99
- add the following `<script>` to your `public/index.html`:
10-
1110
```html
12-
<script id="app-config" src="http://localhost:3456/app-config.js"></script>
13-
```
14-
15-
- modify the `start` npm script:
16-
17-
```json
18-
"start": "dev-config-server & react-scripts start"
11+
<script id="app-config">
12+
window.APP_CONFIG = {
13+
/* Your development configuration here */
14+
MY_VAR: "my_val"
15+
};
16+
</script>
1917
```
20-
21-
> Note: you can use `npm-run-all` or `concurrently` to better handle
22-
> concurrently running processes in npm scripts
23-
2418
- access the config variable in your code:
2519
```js
2620
console.log(window.APP_CONFIG.MY_VAR);
@@ -30,18 +24,12 @@ Then:
3024

3125
#### In development
3226

33-
- define configuration in the `.env` file:
34-
35-
```sh
36-
APP_CONFIG_MY_VAR=my_val
37-
```
38-
27+
- define your development configuration in the `app-config` script as seen above
3928
- start the development server with `yarn start`
4029

4130
#### In production
4231

4332
- build your app with `yarn build`
44-
4533
- run **app-server** defining configuration via environment variables:
4634
```sh
4735
env APP_CONFIG_MY_VAR=my_val app-server
@@ -51,26 +39,18 @@ Then:
5139

5240
#### In development
5341

54-
**dev-config-server** - a CLI tool provided by
55-
[staticdeploy/app-config](https://github.com/staticdeploy/app-config/),
56-
dependency of app-server - starts a server listening on port `3456`. Reading
57-
environment variables defined in the `.env` file, it generates a javascript file
58-
and serves it at `/app-config.js`. The file defines the `window.APP_CONFIG`
59-
global variable ([how it's generated](config-generation.md)).
60-
6142
**react-scripts** starts the development server of the app.
6243

6344
When the app is loaded in the browser, the `#app-config` script in `index.html`
64-
loads `/app-config.js` defining `window.APP_CONFIG`. The variable can then be
65-
accessed by the app code.
45+
defines `window.APP_CONFIG`. The variable can then be accessed by the app code.
6646

6747
#### In production
6848

69-
**app-server** starts a server listening on port `3000`, serving files under the
70-
`build` directory. It also generates - from environment variables - the
71-
javascript file defining `window.APP_CONFIG`. Instead of serving it at
72-
`/app-config.js` though, the server injects it directly as content of the
73-
`#app-config` script in `index.html` (removing the script's `src` attribute).
49+
**app-server** starts a static server for files under the `build` directory. It
50+
also generates - from environment variables, command line options, or a
51+
configuration file - the javascript snippet defining `window.APP_CONFIG`, and
52+
injects it as content of the `#app-config` script in `index.html`, replacing the
53+
development configuration.
7454

7555
When the app is loaded in the browser, the `#app-config` script is evaluated,
7656
defining `window.APP_CONFIG` that can then be accessed by the app code.

examples/create-react-app/.env

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

examples/create-react-app/Dockerfile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
FROM staticdeploy/app-server:v4.0.0-cra-builder
2-
FROM staticdeploy/app-server:v4.0.0-cra-runtime
1+
FROM staticdeploy/app-server:master-cra-builder
2+
FROM staticdeploy/app-server:master-cra-runtime

examples/create-react-app/README.md

Lines changed: 14 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -13,29 +13,19 @@ yarn install
1313
yarn start
1414
```
1515

16-
Open the app at http://localhost:3000 and see the `Hello world!` greeting. Then
17-
stop the process, change the value of the variable defined in `.env` and restart
18-
the development server. Re-open the app and see that the greeting target has
19-
changed.
16+
Open the app at http://localhost:3000 and see the `Hello world!` greeting.
17+
Change the value of the variable defined in `public/index.html` and see that the
18+
greeting target has changed.
2019

2120
### What happens in the example
2221

23-
`yarn start` starts - in parallel - **create-react-app**'s development server
24-
and [**app-config**](https://github.com/staticdeploy/app-config)'s
25-
**dev-config-server**.
26-
27-
When started, **dev-config-server**:
28-
29-
1. generates from the `.env` file a javascript script defining the global
30-
variable `window.APP_CONFIG`
31-
2. serves the script at `http://localhost:3456/app-config.js`
22+
`yarn start` simply starts **create-react-app**'s development server.
3223

3324
When the app is loaded:
3425

35-
1. the `#app-config` script in the app's `index.html` loads and evaluates
36-
`/app-config.js`
37-
2. `app-config.js` defines the variable `window.APP_CONFIG`
38-
3. the appropriate greeting is rendered
26+
1. the `#app-config` script in the app's `index.html` defines the variable
27+
`window.APP_CONFIG`
28+
2. the appropriate greeting is rendered
3929

4030
## Run the example in production mode with Docker
4131

@@ -45,20 +35,22 @@ cd app-server/examples/create-react-app
4535
# Build the app Docker image
4636
docker build -t app-server-example .
4737
# Run the image passing in the necessary configuration
48-
docker run -p 3000:80 -e APP_CONFIG_TARGET=world app-server-example
38+
docker run --rm --init -p 3000:80 -e APP_CONFIG_TARGET=world app-server-example
4939
```
5040

51-
Open the app at http://localhost:3000 and see the `Hello world!` greeting. Then
52-
stop the container and restart it passing in a different value for the
41+
Open the app at http://localhost:3000 and see the `Hello world!` greeting. Stop
42+
the container and restart it passing in a different value for the
5343
`APP_CONFIG_TARGET` variable. Re-open the app and see that the greeting target
5444
has changed.
5545

5646
### What happens in the example
5747

5848
The `docker build ...` command builds the app docker image, using the
5949
`staticdeploy/app-server:cra-builder` and `staticdeploy/app-server:cra-runtime`
60-
base images which respectively build the app into a static bundle and setup
61-
**app-server** to serve the built app.
50+
base images which respectively:
51+
52+
- build the app into a static bundle
53+
- setup**app-server** to serve the bundle
6254

6355
When the image is run with `docker run ...`, `app-server`:
6456

examples/create-react-app/package.json

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,7 @@
22
"name": "create-react-app",
33
"version": "0.0.0",
44
"scripts": {
5-
"start:config-server": "dev-config-server",
6-
"start:dev-server": "react-scripts start",
7-
"start": "npm-run-all -p start:*",
5+
"start": "react-scripts start",
86
"build": "react-scripts build",
97
"test": "react-scripts test --env=jsdom"
108
},
@@ -13,7 +11,6 @@
1311
"react-dom": "^16.9.0"
1412
},
1513
"devDependencies": {
16-
"@staticdeploy/app-config": "^2.0.2",
1714
"npm-run-all": "^4.1.5",
1815
"react-scripts": "3.1.1"
1916
},
Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
1-
<!doctype html>
1+
<!DOCTYPE html>
22
<html>
3+
<head>
4+
<title>create-react-app app</title>
5+
<script id="app-config">
6+
window.APP_CONFIG = {
7+
TARGET: "world"
8+
};
9+
</script>
10+
</head>
311

4-
<head>
5-
<title>create-react-app app</title>
6-
<script id="app-config" src="http://localhost:3456/app-config.js"></script>
7-
</head>
8-
9-
<body>
10-
<div id="root"></div>
11-
</body>
12-
12+
<body>
13+
<div id="root"></div>
14+
</body>
1315
</html>

0 commit comments

Comments
 (0)