Skip to content

Commit b8e9df8

Browse files
committed
Merge remote-tracking branch 'origin/master' into feature/woodpecker
2 parents 49def02 + 14f2ecf commit b8e9df8

14 files changed

+417
-64
lines changed

.babelrc.js

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

.drone.yml

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
---
2+
kind: pipeline
3+
type: docker
4+
name: push-latest
5+
steps:
6+
- name: submodules
7+
image: alpine/git
8+
commands:
9+
- git submodule update --init --recursive
10+
- name: build-and-push
11+
image: plugins/docker
12+
settings:
13+
repo: ${DRONE_REPO_NAMESPACE/mu-semtech/semtech}/${DRONE_REPO_NAME%-service}
14+
username:
15+
from_secret: docker_username
16+
password:
17+
from_secret: docker_password
18+
trigger:
19+
branch:
20+
- master
21+
event:
22+
exclude:
23+
- pull_request
24+
---
25+
kind: pipeline
26+
type: docker
27+
name: push-feature-build
28+
steps:
29+
- name: submodules
30+
image: alpine/git
31+
commands:
32+
- git submodule update --init --recursive
33+
- name: push-feature-build
34+
image: plugins/docker
35+
settings:
36+
repo: ${DRONE_REPO_NAMESPACE/mu-semtech/semtech}/${DRONE_REPO_NAME%-service}
37+
tags: ${DRONE_BRANCH/\//-}
38+
username:
39+
from_secret: docker_username
40+
password:
41+
from_secret: docker_password
42+
purge: true
43+
trigger:
44+
branch:
45+
- "*/*"
46+
event:
47+
exclude:
48+
- pull_request
49+
---
50+
kind: pipeline
51+
type: docker
52+
name: push-release
53+
steps:
54+
- name: submodules
55+
image: alpine/git
56+
commands:
57+
- git submodule update --init --recursive
58+
- name: build-and-push-tag
59+
image: plugins/docker
60+
settings:
61+
repo: ${DRONE_REPO_NAMESPACE/mu-semtech/semtech}/${DRONE_REPO_NAME%-service}
62+
tags: ${DRONE_TAG##v} # strips v from the tag
63+
username:
64+
from_secret: docker_username
65+
password:
66+
from_secret: docker_password
67+
trigger:
68+
event:
69+
- tag
70+
---
71+
kind: pipeline
72+
type: docker
73+
name: dry-run
74+
steps:
75+
- name: submodules
76+
image: alpine/git
77+
commands:
78+
- git submodule update --init --recursive
79+
- name: dry-run
80+
image: plugins/docker
81+
settings:
82+
repo: ${DRONE_REPO_NAMESPACE/mu-semtech/semtech}/${DRONE_REPO_NAME%-service}
83+
dry_run: true
84+
trigger:
85+
event:
86+
- pull_request
87+
---
88+
kind: secret
89+
name: docker_username
90+
data: kurKh/EctlKfrAViJSn3voT4duP9aBdrip7OQV7ZuS5HYPTVxMxy
91+
---
92+
kind: secret
93+
name: docker_password
94+
data: og9cL7Gfx5E0qWJs0PeDaqIH6fr3Z/aK1YmVlmsh2B8OC2f8kyZFMiGeLDp/dcLloZ2OX7HLFy3euhFR4O4Z7w==
95+
---
96+
kind: signature
97+
hmac: adce33d1d58f886a57a5877695d367e5c6b3347045281b61e5a18f49cf960595
98+
99+
...

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: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ The following importable variables are available:
6060
- `sparql`: [Template tag](https://www.npmjs.com/package/sparql-client-2#using-the-sparql-template-tag) to create queries with interpolated values
6161
- `sparqlEscapeString(value) => string`: Function to escape a string in SPARQL
6262
- `sparqlEscapeUri(value) => string`: Function to escape a URI in SPARQL
63+
- `sparqlEscapeDecimal(value) => string`: Function to escape an integer or float as an `xsd:decimal` in SPARQL
6364
- `sparqlEscapeInt(value) => string`: Function to escape an integer in SPARQL
6465
- `sparqlEscapeFloat(value) => string`: Function to escape a float in SPARQL
6566
- `sparqlEscapeDate(value) => string`: Function to escape a date in SPARQL. The given value is passed to the `Date` constructor.
@@ -88,16 +89,37 @@ mu.app.get('/', function( req, res ) {
8889
res.send('Hello using full import');
8990
} );
9091
```
92+
## Transpiled languages
93+
The template has second-class support for transpiling TypeScript and CoffeeScript. These are considered second-class and support may be removed in a minor release but not in a patch release.
94+
95+
Overwriting files through the config folder may require you to stick to the original format. There are currently no guarantees on this.
96+
97+
### Coffeescript
98+
Any file extending to .coffee will be transpiled from coffeescript to javascript file. Sourcemaps are included for debugging.
99+
100+
### TypeScript
101+
Any file extending in .ts will be transpiled to a javascript file. Sources are currently not typechecked though this is subject to change. Sourcemaps are included for debugging.
91102

92103
## Dependencies
93104

94105
You can install additional dependencies by including a `package.json` file next to your `app.js`. It works as you would expect: just define the packages in the `dependencies` section of the `package.json`. They will be installed automatically at build time.
95106

96107
## Configuration
108+
### Environment variables
97109
The following environment variables can be configured:
98110

99111
- `NODE_ENV` (default: `production`): either `"development"` or `"production"`. The environment to start the application in. The application live reloads on changes in `"development"` mode.
100112
- `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+
116+
117+
### Mounting `/config`
118+
You may let users extend the microservice with code.
119+
120+
When you import content from `./config/some-file`, the sources can be provided by the end user in `/config/some-file`.
121+
122+
You may provide default values for each of these files. The sources provided by the app are merged with the sources provided by the microservice, with the app's configuration taking precedence.
101123

102124
## Logging
103125

@@ -177,3 +199,20 @@ When running inside a mu.semte.ch stack, you could mount your sources and connec
177199
- /absolute/path/to/your/sources/:/app/
178200
```
179201
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+

babel.config.json

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
{
2+
"presets": [
3+
["@babel/preset-env",
4+
{
5+
"targets": {
6+
"node": 14
7+
}
8+
}
9+
],
10+
["@babel/preset-typescript"]
11+
],
12+
"plugins": [
13+
["@babel/plugin-proposal-decorators", { "legacy": true }],
14+
["@babel/plugin-proposal-class-properties"]
15+
],
16+
"ignore": [
17+
"./node_modules",
18+
"/usr/src/processing/build/node_modules"
19+
]
20+
}

boot.sh

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,25 +3,53 @@ then
33
# Run live-reload development
44
exec /usr/src/app/node_modules/.bin/nodemon \
55
--watch /app \
6-
--ext js,mjs,cjs,json \
6+
--watch /config \
7+
--ext js,coffee,ts,mjs,cjs,json \
78
--exec /usr/src/app/run-development.sh
89
elif [ "$NODE_ENV" == "production" ]
910
then
1011
diff -rq /app /app.original > /dev/null
11-
FILES_CHANGED="$?"
12+
APP_FILES_CHANGED="$?"
13+
diff -rq /config /config.original > /dev/null
14+
CONFIG_FILES_CHANGED="$?"
1215

13-
if [ ! -f /usr/src/output/app/app.js ]
16+
if [ ! -f /usr/src/build/app.js ]
1417
then
1518
echo "No built sources found. If you mount new sources, please set the NODE_ENV=\"development\" environment variable."
1619
sleep 5;
1720
exit 1;
18-
elif [ $FILES_CHANGED != "0" ]
21+
elif [ $APP_FILES_CHANGED != "0" ]
1922
then
2023
echo "Built sources are not the same as sources available in /app. If you mount new sources, please set the NODE_ENV=\"development\" environment variable."
2124
sleep 5;
2225
exit 1;
26+
elif [ $CONFIG_FILES_CHANGED != "0" ]
27+
then
28+
echo "Rebuilding sources to include /config."
29+
30+
# move new configuration into app for transpilation
31+
if [[ "$(ls -A /config 2> /dev/null)" ]]
32+
then
33+
cp -Rf /config/* /usr/src/app/app/config/
34+
fi
35+
36+
# make a backup of the used configuration so we can detect changes
37+
rm -Rf /config.original
38+
mkdir /config.original
39+
if [[ "$(ls -A /config 2> /dev/null)" ]]
40+
then
41+
cp -Rf /config/* /config.original
42+
fi
43+
44+
# transpile sources
45+
cd /usr/src/app/
46+
./transpile-sources.sh
47+
48+
# boot transpiled sources
49+
cd /usr/src/build/
50+
exec node ./app.js
2351
else
24-
cd /usr/src/output/
25-
exec node ./app/app.js
52+
cd /usr/src/build/
53+
exec node ./app.js
2654
fi
2755
fi

build-production.sh

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,23 +10,24 @@
1010
cd /usr/src/app
1111
rm -rf ./app /app.original
1212
cp -r /app ./
13+
14+
mkdir -p /config /config.original
15+
16+
if [[ "$(ls -A /app/config/ 2> /dev/null)" ]]
17+
then
18+
cp -r /app/config/* /config.original/
19+
cp -r /app/config/* /config/
20+
fi
21+
1322
cp -r /app /app.original
1423

1524
# Install custom packages if need be
1625
if [ -f ./app/package.json ]
1726
then
18-
npm install ./app
19-
rm ./app/package.json
27+
echo "Running npm install"
28+
cd /usr/src/app/app/
29+
npm install
30+
cd /usr/src/app/
2031
fi
2132

22-
# Make the build
23-
24-
## Copy over template node_modules
25-
mkdir -p /usr/src/output/node_modules
26-
cp -R /usr/src/app/node_modules /usr/src/output/
27-
28-
## Build microservice sources
29-
/usr/src/app/node_modules/.bin/babel /usr/src/app/ \
30-
--ignore app/node_modules,node_modules \
31-
--copy-files --no-copy-ignored \
32-
--out-dir /usr/src/output
33+
./transpile-sources.sh

helpers/mu/index.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ const mu = {
1414
sparqlEscape: sparql.sparqlEscape,
1515
sparqlEscapeString: sparql.sparqlEscapeString,
1616
sparqlEscapeUri: sparql.sparqlEscapeUri,
17+
sparqlEscapeDecimal: sparql.sparqlEscapeDecimal,
1718
sparqlEscapeInt: sparql.sparqlEscapeInt,
1819
sparqlEscapeFloat: sparql.sparqlEscapeFloat,
1920
sparqlEscapeDate: sparql.sparqlEscapeDate,
@@ -30,6 +31,7 @@ const SPARQL = mu.SPARQL,
3031
sparqlEscapeString = mu.sparqlEscapeString,
3132
sparqlEscapeUri = mu.sparqlEscapeUri,
3233
sparqlEscapeInt = mu.sparqlEscapeInt,
34+
sparqlEscapeDecimal = mu.sparqlEscapeDecimal,
3335
sparqlEscapeFloat = mu.sparqlEscapeFloat,
3436
sparqlEscapeDate = mu.sparqlEscapeDate,
3537
sparqlEscapeDateTime = mu.sparqlEscapeDateTime,
@@ -44,6 +46,7 @@ export {
4446
sparqlEscape,
4547
sparqlEscapeString,
4648
sparqlEscapeUri,
49+
sparqlEscapeDecimal,
4750
sparqlEscapeInt,
4851
sparqlEscapeFloat,
4952
sparqlEscapeDate,

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)