Skip to content

Commit 10c2772

Browse files
MPParsleynvdk
andauthored
Merge master in feature-node-16-support (#52)
* add expose statement (#39) I was kind of surprised we didn't have this in here already. the [docker reference](https://docs.docker.com/engine/reference/builder/#expose) specifies this is mostly for documentation towards users of a microservice. I think it's valuable to make it clear that microservices based on the template listen on port 80. Exposed ports are also used by other "proxy type" microservices such as the letsencrypt companion to figure out how to forward calls to the container in question. From the reference: > The EXPOSE instruction informs Docker that the container listens on the specified network ports at runtime. You can specify whether the port listens on TCP or UDP, and the default is TCP if the protocol is not specified. > The EXPOSE instruction does not actually publish the port. It functions as a type of documentation between the person who builds the image and the person who runs the container, about which ports are intended to be published. To actually publish the port when running the container, use the -p flag on docker run to publish and map one or more ports, or the -P flag to publish all exposed ports and map them to high-order ports. * Allow overriding port and hostname (#47) * Allow overriding port and hostname * Update server.js * Update Dockerfile * instructions on handling delta's in a JS service (#48) * Add documentation to allow overriding port and hostname (#49) * Allow overriding port and hostname * Update server.js * Update Dockerfile * Update Dockerfile * Update README.md --------- Co-authored-by: Niels V <[email protected]>
1 parent 504f38f commit 10c2772

File tree

3 files changed

+29
-2
lines changed

3 files changed

+29
-2
lines changed

Dockerfile

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ ENV MU_SPARQL_ENDPOINT 'http://database:8890/sparql'
88
ENV MU_APPLICATION_GRAPH 'http://mu.semte.ch/application'
99
ENV NODE_ENV 'production'
1010

11+
ENV HOST '0.0.0.0'
12+
ENV PORT '80'
13+
1114
ENV LOG_SPARQL_ALL 'true'
1215
ENV DEBUG_AUTH_HEADERS 'true'
1316

@@ -19,6 +22,8 @@ COPY . /usr/src/app
1922
RUN chmod +x /usr/src/app/run-development.sh
2023
RUN chmod +x /usr/src/app/build-production.sh
2124

25+
EXPOSE ${PORT}
26+
2227
CMD sh boot.sh
2328

2429
# This stuff only runs when building an image from the template

README.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,9 @@ The following environment variables can be configured:
110110

111111
- `NODE_ENV` (default: `production`): either `"development"` or `"production"`. The environment to start the application in. The application live reloads on changes in `"development"` mode.
112112
- `MAX_BODY_SIZE` (default: `100kb`): max size of the request body. See [ExpressJS documentation](https://expressjs.com/en/resources/middleware/body-parser.html#limit).
113+
- `HOST` (default: `0.0.0.0`): The hostname you want the service to bind to.
114+
- `PORT` (default: `80`): The port you want the service to bind to
115+
113116

114117
### Mounting `/config`
115118
You may let users extend the microservice with code.
@@ -196,3 +199,20 @@ When running inside a mu.semte.ch stack, you could mount your sources and connec
196199
- /absolute/path/to/your/sources/:/app/
197200
```
198201
Now open Chromium, and visit [chrome://inspect/](chrome://inspect/). Once the service is launched, a remote target on localhost should pop up.
202+
203+
## Handling Delta's
204+
If you are building a reactive service that should execute certain logic based on changes in the database, you want to hook it up to the [delta-notifier](https://github.com/mu-semtech/delta-notifier/). Some extra steps need to be taken to properly handle delta's, specifically the route handling delta's will need to use a specific bodyParser.
205+
206+
The default bodyParser provided by the template will only accept `application/vnd.api+json` and the delta-notifier is sending `application/json` content. Aside from that the body of a delta message may be very large, often several megabytes. By specifying the bodyParser on the route accepting delta messages you can easily modify it when required.
207+
208+
An example
209+
```js
210+
// app.js
211+
import bodyParser from 'body-parser';
212+
// ...
213+
214+
app.post("/delta-updates", bodyParser.json({limit: '50mb'}), async function(req, res) {
215+
//...
216+
}
217+
```
218+

helpers/mu/server.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import bodyParser from 'body-parser';
44

55
var app = express();
66

7+
var port = process.env.PORT || '80';
8+
var hostname = process.env.HOST || '0.0.0.0';
79
var bodySizeLimit = process.env.MAX_BODY_SIZE || '100kb';
810

911
// parse JSONAPI content type
@@ -35,8 +37,8 @@ const errorHandler = function(err, req, res, next) {
3537
};
3638

3739
// start server
38-
app.listen( 80, function() {
39-
console.log(`Starting server on port 80 in ${app.get('env')} mode`);
40+
app.listen( port, hostname, function() {
41+
console.log(`Starting server on ${hostname}:${port} in ${app.get('env')} mode`);
4042
});
4143

4244
export default app;

0 commit comments

Comments
 (0)