Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
helpers/dist
helpers/node_modules
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
node_modules
helpers/dist
31 changes: 17 additions & 14 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -14,26 +14,29 @@ ENV PORT '80'
ENV LOG_SPARQL_ALL 'true'
ENV DEBUG_AUTH_HEADERS 'true'

WORKDIR /usr/src/app
COPY package.json /usr/src/app/package.json
COPY ./scripts /app/scripts
RUN npm install
COPY . /usr/src/app
RUN chmod +x /usr/src/app/run-development.sh
RUN chmod +x /usr/src/app/build-production.sh
WORKDIR /app
RUN mkdir /template && mkdir -p /app/src
COPY package.json /template/package.json
COPY ./scripts /template/scripts
RUN cd /template && npm install
COPY . /template
RUN chmod +x /template/run-development.sh
RUN chmod +x /template/build-production.sh
RUN chmod +x /template/build-template-package.sh
RUN /template/build-template-package.sh

EXPOSE ${PORT}

CMD bash boot.sh
CMD bash /template/boot.sh

# This stuff only runs when building an image from the template
ONBUILD RUN rm -Rf /app/scripts
ONBUILD ADD . /app/
ONBUILD RUN /usr/src/app/build-production.sh
ONBUILD RUN rm -Rf /app/src/scripts
ONBUILD ADD . /app/src
ONBUILD RUN /template/build-production.sh

ONBUILD RUN if [ -f /app/on-build.sh ]; \
ONBUILD RUN if [ -f /app/src/on-build.sh ]; \
then \
echo "Running custom on-build.sh of child" \
&& chmod +x /app/on-build.sh \
&& /bin/bash /app/on-build.sh ;\
&& chmod +x /app/src/on-build.sh \
&& /bin/bash /app/src/on-build.sh ;\
fi
12 changes: 4 additions & 8 deletions babel.config.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"presets": [
["@babel/preset-env",
[
"@babel/preset-env",
{
"targets": {
"node": 18
Expand All @@ -9,11 +10,6 @@
],
["@babel/preset-typescript"]
],
"plugins": [
["@babel/plugin-proposal-decorators", { "version": "2023-05" }]
],
"ignore": [
"./node_modules",
"/usr/src/processing/build/node_modules"
]
"plugins": [["@babel/plugin-proposal-decorators", { "version": "2023-05" }]],
"ignore": ["./node_modules", "/app/src/node_modules"]
}
34 changes: 22 additions & 12 deletions boot.sh
Original file line number Diff line number Diff line change
@@ -1,20 +1,24 @@
source /template/helpers.sh

#!/bin/bash
if [ "$NODE_ENV" == "development" ]
then
# Run live-reload development
exec /usr/src/app/node_modules/.bin/nodemon \
--watch /app \
exec /template/node_modules/.bin/nodemon \
--watch /app/src \
--watch /config \
--ignore /app/src/dist \
--ext js,coffee,ts,mjs,cjs,json \
--exec /usr/src/app/run-development.sh
--exec /template/run-development.sh
elif [ "$NODE_ENV" == "production" ]
then
diff -rq /app /app.original > /dev/null
# diff but accept items created during build process
diff -x 'dist*' -x "node_modules" -x "package-lock.json" -x "package.json" -rq /app /app.original > /dev/null
APP_FILES_CHANGED="$?"
diff -rq /config /config.original > /dev/null
CONFIG_FILES_CHANGED="$?"

if [ ! -f /usr/src/build/app.js ]
if [ ! -f /app/dist/app.js ]
then
echo "No built sources found. If you mount new sources, please set the NODE_ENV=\"development\" environment variable."
sleep 5;
Expand All @@ -31,7 +35,8 @@ then
# move new configuration into app for transpilation
if [[ "$(ls -A /config 2> /dev/null)" ]]
then
cp -Rf /config/* /usr/src/app/app/config/
mkdir -p /app/src/config/
cp -Rf /config/* /app/src/config/
fi

# make a backup of the used configuration so we can detect changes
Expand All @@ -43,14 +48,19 @@ then
fi

# transpile sources
cd /usr/src/app/
./transpile-sources.sh
cd /app/

# add node modules from template back in
docker-rsync /template/node_modules/ /app/node_modules/
/template/transpile-sources.sh

# boot transpiled sources
cd /usr/src/build/
exec node ./app.js
cd /app/
exec node /app/dist/app.js
else
cd /usr/src/build/
exec node ./app.js
cd /app/
# add node modules from template back in
docker-rsync /template/node_modules/ /app/node_modules/
exec node /app/dist/app.js
fi
fi
19 changes: 11 additions & 8 deletions build-production.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@

# Builds sources in production
#
# We want to compare the used sources from the one available in /app
# We want to compare the used sources from the one available in /app/src/
# so we can warn at runtime in case developers accidentally mount
# sources without setting the development environment variable.

# Copy sources from /app to where they can be built
cd /usr/src/app
rm -rf ./app /app.original
cp -r /app ./
cd /app
rm -rf /app/dist
mkdir /app/dist

mkdir -p /config /config.original

Expand All @@ -22,12 +22,15 @@ fi
cp -r /app /app.original

# Install custom packages if need be
if [ -f ./app/package.json ]
if [ -f /app/src/package.json ]
then
echo "Running npm install"
cd /usr/src/app/app/
cd /app/
cp /app/src/package.json /app/package.json
npm install
cd /usr/src/app/
fi

./transpile-sources.sh
# add node modules from template back in
docker-rsync /template/node_modules/ /app/node_modules/

/template/transpile-sources.sh
9 changes: 9 additions & 0 deletions build-template-package.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
mkdir /template/built-mu
cd /template
/template/node_modules/.bin/babel \
/template/helpers/mu/ \
--source-maps "true" \
--out-dir /template/built-mu \
--extensions ".js,.ts"

cp -R /template/built-mu /template/node_modules/mu
3 changes: 3 additions & 0 deletions helpers/README
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
this allows the mu-helpers to be published as a typescript package as well from within the template repository, making sure that the template and the package are always published together

One caveat is that the dependencies for the package are fewer than the dependencies that are added by default in the template (e.g. babel, coffeescript etc) so when publishing, care should be taken to keep those in sync to avoid surprises by consumers
32 changes: 16 additions & 16 deletions helpers/mu/index.js → helpers/mu/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { app, errorHandler } from './server';
import sparql from './sparql';
import { v1 as uuidV1 } from 'uuid';
import { app, errorHandler } from "./server";
import sparql from "./sparql";
import { v1 as uuidV1 } from "uuid";

// generates a uuid
const uuid = uuidV1;
Expand All @@ -21,21 +21,21 @@ const mu = {
sparqlEscapeDateTime: sparql.sparqlEscapeDateTime,
sparqlEscapeBool: sparql.sparqlEscapeBool,
uuid,
errorHandler
errorHandler,
};

const SPARQL = mu.SPARQL,
query = mu.query,
update = mu.update,
sparqlEscape = mu.sparqlEscape,
sparqlEscapeString = mu.sparqlEscapeString,
sparqlEscapeUri = mu.sparqlEscapeUri,
sparqlEscapeInt = mu.sparqlEscapeInt,
sparqlEscapeDecimal = mu.sparqlEscapeDecimal,
sparqlEscapeFloat = mu.sparqlEscapeFloat,
sparqlEscapeDate = mu.sparqlEscapeDate,
sparqlEscapeDateTime = mu.sparqlEscapeDateTime,
sparqlEscapeBool = mu.sparqlEscapeBool;
query = mu.query,
update = mu.update,
sparqlEscape = mu.sparqlEscape,
sparqlEscapeString = mu.sparqlEscapeString,
sparqlEscapeUri = mu.sparqlEscapeUri,
sparqlEscapeInt = mu.sparqlEscapeInt,
sparqlEscapeDecimal = mu.sparqlEscapeDecimal,
sparqlEscapeFloat = mu.sparqlEscapeFloat,
sparqlEscapeDate = mu.sparqlEscapeDate,
sparqlEscapeDateTime = mu.sparqlEscapeDateTime,
sparqlEscapeBool = mu.sparqlEscapeBool;

export {
app,
Expand All @@ -53,7 +53,7 @@ export {
sparqlEscapeDateTime,
sparqlEscapeBool,
uuid,
errorHandler
errorHandler,
};

export default mu;
49 changes: 0 additions & 49 deletions helpers/mu/server.js

This file was deleted.

60 changes: 60 additions & 0 deletions helpers/mu/server.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import httpContext from "express-http-context";
import express from "express";
import bodyParser from "body-parser";

var app = express();

var port = parseInt(process.env.PORT || "80") || 80;
var hostname = process.env.HOST || "0.0.0.0";
var bodySizeLimit = process.env.MAX_BODY_SIZE || "100kb";

// parse JSONAPI content type
app.use(
bodyParser.json({
type: function (req) {
const expressReq = req as express.Request;
return /^application\/vnd\.api\+json/.test(
expressReq.get("content-type") || ""
);
},
limit: bodySizeLimit,
})
);
app.use(bodyParser.urlencoded({ extended: false }));

// set JSONAPI content type
app.use("/", function (req, res, next) {
res.type("application/vnd.api+json");
next();
});

app.use(httpContext.middleware);

app.use(function (req, res, next) {
httpContext.set("request", req);
httpContext.set("response", res);
next();
});

const errorHandler: express.ErrorRequestHandler = function (
err,
req,
res,
next
) {
res.status(err.status || 400);
res.json({
errors: [{ title: err.message }],
});
};

// start server
app.listen(port, hostname, function () {
console.log(
`Starting server on ${hostname}:${port} in ${app.get("env")} mode`
);
});

export default app;

export { app, errorHandler };
1 change: 1 addition & 0 deletions helpers/mu/sparql-client-2.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
declare module "sparql-client-2";
Loading