Skip to content

Commit

Permalink
Node.js Stack Updates (#1)
Browse files Browse the repository at this point in the history
* specify language as JavaScript

* default template 'default'

* mount current directory as 'function'

* move simple template to default

* use 'function' for user-app

* prettify and remove duplicate in tests

* default template updates

Print event to log by default to enable demo of updating to return.
Check for an invalid context to facilitate test examples.

* default package.json dependencies etc.

* default template tests

* set health and liveness endpoints

* simply log the context on invocation

* explicitly set run and debug kill to true
  • Loading branch information
lkingland committed Feb 12, 2020
1 parent 44a1901 commit 9945fc7
Show file tree
Hide file tree
Showing 12 changed files with 71 additions and 65 deletions.
25 changes: 13 additions & 12 deletions image/Dockerfile-stack
@@ -1,35 +1,36 @@
FROM node:12

ENV APPSODY_PROJECT_DIR=/project
ENV APPSODY_MOUNTS=/:/project/user-app
ENV APPSODY_DEPS=/project/user-app/node_modules
ENV APPSODY_MOUNTS=.:/project/function
ENV APPSODY_DEPS=/project/function/node_modules

ENV APPSODY_WATCH_DIR=/project/user-app
ENV APPSODY_WATCH_IGNORE_DIR=/project/user-app/node_modules
ENV APPSODY_WATCH_DIR=/project/function
ENV APPSODY_WATCH_IGNORE_DIR=/project/function/node_modules
ENV APPSODY_WATCH_REGEX="^.*.js$"

ENV APPSODY_TEST="npm test && npm test --prefix function"
ENV APPSODY_TEST_ON_CHANGE=""
ENV APPSODY_TEST_KILL=false

ENV APPSODY_RUN="npm start"
ENV APPSODY_RUN_ON_CHANGE="npm start"
ENV APPSODY_RUN_ON_CHANGE="$APPSODY_RUN"
ENV APPSODY_RUN_KILL=true

ENV APPSODY_DEBUG="npm start --node-options --inspect=0.0.0.0"
ENV APPSODY_DEBUG_ON_CHANGE="npm start --node-options --inspect=0.0.0.0"
ENV APPSODY_DEBUG_ON_CHANGE="$APPSODY_DEBUG"
ENV APPSODY_DEBUG_KILL=true

ENV APPSODY_TEST="npm test && npm test --prefix user-app"
ENV APPSODY_TEST_ON_CHANGE=""
ENV APPSODY_TEST_KILL=false

ENV APPSODY_PREP="npm install --prefix user-app && npm audit fix --prefix user-app"
ENV APPSODY_PREP="npm install --prefix function && npm audit fix --prefix function"

COPY ./LICENSE /licenses/
COPY ./project /project
COPY ./config /config

WORKDIR /project
RUN npm install --no-package-lock

ENV PORT=8080
ENV NODE_PATH=/project/user-app/node_modules
ENV NODE_PATH=/project/function/node_modules

EXPOSE 8080
EXPOSE 9229
6 changes: 3 additions & 3 deletions image/config/app-deploy.yaml
Expand Up @@ -5,7 +5,7 @@ metadata:
spec:
# Add fields here
version: 1.0.0
applicationImage: APPSODY_DOCKER_IMAGE
applicationImage: APPSODY_DOCKER_IMAGE
stack: APPSODY_STACK
service:
type: NodePort
Expand All @@ -18,15 +18,15 @@ spec:
readinessProbe:
failureThreshold: 12
httpGet:
path: /ready
path: /health/readiness
port: APPSODY_PORT
initialDelaySeconds: 5
periodSeconds: 2
timeoutSeconds: 1
livenessProbe:
failureThreshold: 12
httpGet:
path: /live
path: /health/liveness
port: APPSODY_PORT
initialDelaySeconds: 5
periodSeconds: 2
Expand Down
14 changes: 7 additions & 7 deletions image/project/Dockerfile
@@ -1,6 +1,6 @@
FROM node:12 as installer
ARG home_dir="/project"
ARG user_app="/project/user-app"
ARG user_app="/project/function"

ENV HOME /project

Expand All @@ -10,27 +10,27 @@ COPY ./package.json ./
RUN npm install --production --no-package-lock

# Install the user dependencies
WORKDIR /project/user-app
COPY ./user-app/package.json ./
WORKDIR /project/function
COPY ./function/package.json ./
RUN npm install --production --no-package-lock && \
[ ! -d /project/user-app/node_modules ] && mkdir /project/user-app/node_modules
[ ! -d /project/function/node_modules ] && mkdir /project/function/node_modules

# Copy the dependencies into an alpine image
FROM node:12-alpine
COPY --chown=node:node . /project

# Copy all dependencies
COPY --chown=node:node --from=installer /project/user-app/node_modules /project/user-app/node_modules
COPY --chown=node:node --from=installer /project/function/node_modules /project/function/node_modules
COPY --chown=node:node --from=installer /project/node_modules /project/node_modules

WORKDIR /project

ENV NODE_PATH=/project/user-app/node_modules
ENV NODE_PATH=/project/function/node_modules

ENV NODE_ENV production
ENV PORT 8080

USER node

EXPOSE 8080
CMD ["npm", "start"]
CMD ["npm", "start"]
4 changes: 2 additions & 2 deletions image/project/package.json
@@ -1,7 +1,7 @@
{
"name": "ce-functions",
"name": "function",
"version": "0.0.1",
"description": "Node.js Cloud Event Function Stack",
"description": "CloudEvent Handler",
"license": "Apache-2.0",
"repository": {
"type": "git",
Expand Down
4 changes: 2 additions & 2 deletions image/project/server.js
Expand Up @@ -3,14 +3,14 @@
const path = require('path');
const framework = require('@redhat/faas-js-runtime');

const userPath = path.join(`${__dirname}`, 'user-app');
const userPath = path.join(`${__dirname}`, 'function');
const LISTEN_PORT = process.env.LISTEN_PORT || 8080;

let server;

module.exports.close = function close() {
if (server) server.close();
}
};

try {
framework(require(userPath), LISTEN_PORT, srv => {
Expand Down
14 changes: 0 additions & 14 deletions image/project/test/test.js
Expand Up @@ -20,7 +20,6 @@ const request = require('supertest');
// }, { log: false });
// });


// test('Can respond via an async function', t => {
// framework(func, server => {
// t.plan(2);
Expand Down Expand Up @@ -65,17 +64,4 @@ test('Exposes liveness URL', t => {
});
});

test('Exposes liveness URL', t => {
t.plan(2);
request(server)
.get('/health/liveness')
.expect(200)
.expect('Content-type', /text/)
.end((err, res) => {
t.error(err, 'No error');
t.equal(res.text, 'OK');
t.end();
});
});

test.onFinish(runtime.close);
8 changes: 4 additions & 4 deletions stack.yaml
@@ -1,10 +1,10 @@
name: Node.js Cloud Events
name: JavaScript FaaS
version: 0.0.1
description: Serverless Node.js function runtime for Cloud Events
description: Node.js Function Runtime
license: Apache-2.0
language: nodejs
language: JavaScript
maintainers:
- name: Lance Ball
email: lball@redhat.com
github-id: lance
default-template: simple
default-template: default
5 changes: 5 additions & 0 deletions templates/default/index.js
@@ -0,0 +1,5 @@
'use strict';

module.exports = function(context) {
context.log.info(context);
};
17 changes: 17 additions & 0 deletions templates/default/package.json
@@ -0,0 +1,17 @@
{
"name": "event-handler",
"version": "0.1.0",
"description": "Node.js CloudEvent Handler",
"license": "Apache-2.0",
"repository": {
"type": "git",
"url": ""
},
"scripts": {
"test": "node test/*.js"
},
"devDependencies": {
"tape": "^4.13.0",
"cloudevents-sdk": "^1.0.0"
}
}
18 changes: 18 additions & 0 deletions templates/default/test/test.js
@@ -0,0 +1,18 @@
'use strict';

const func = require('..');
const test = require('tape');
const cloudevents = require('cloudevents-sdk/v1');

// Ensure that the function completes cleanly when passed a valid event.
test('Handles a valid event', t => {
// A valid event includes id, type and source at a minimum.
let e = cloudevents.event();
e.id('TEST-EVENT-01');
e.type('com.example.cloudevents.test');
e.source('http://localhost:8080/');

// Invoke the function with the valid event, which should compelte without error.
func({ cloudevent: e });
t.end();
});
8 changes: 0 additions & 8 deletions templates/simple/index.js

This file was deleted.

13 changes: 0 additions & 13 deletions templates/simple/package.json

This file was deleted.

0 comments on commit 9945fc7

Please sign in to comment.