Skip to content

Commit

Permalink
Update Go version in info message
Browse files Browse the repository at this point in the history
Refers to request in: 265

Signed-off-by: Alex Ellis (OpenFaaS Ltd) <alexellis2@gmail.com>
  • Loading branch information
alexellis committed Sep 6, 2021
1 parent f5e89a4 commit cdef5a5
Show file tree
Hide file tree
Showing 8 changed files with 247 additions and 1 deletion.
2 changes: 1 addition & 1 deletion template/go/template.yml
Expand Up @@ -17,7 +17,7 @@ build_options:
- mysql-client
- mysql-dev
welcome_message: |
You have created a new function which uses Golang 1.13 and the Classic
You have created a new function which uses Go 1.15 and the Classic
OpenFaaS template.
To include third-party dependencies, use Go modules and use
Expand Down
1 change: 1 addition & 0 deletions template/node12-debian/.dockerignore
@@ -0,0 +1 @@
*/node_modules
70 changes: 70 additions & 0 deletions template/node12-debian/Dockerfile
@@ -0,0 +1,70 @@
FROM --platform=${TARGETPLATFORM:-linux/amd64} openfaas/of-watchdog:0.7.2 as watchdog
FROM --platform=${TARGETPLATFORM:-linux/amd64} node:12 as ship

ARG TARGETPLATFORM
ARG BUILDPLATFORM

COPY --from=watchdog /fwatchdog /usr/bin/fwatchdog
RUN chmod +x /usr/bin/fwatchdog

RUN apt-get update -qqy \
&& apt-get install -qqy \
--no-install-recommends \
unzip
RUN addgroup --system app \
&& adduser --system --ingroup app app

WORKDIR /root/

# Turn down the verbosity to default level.
ENV NPM_CONFIG_LOGLEVEL warn

RUN mkdir -p /home/app

# Wrapper/boot-strapper
WORKDIR /home/app
COPY package.json ./

# This ordering means the npm installation is cached for the outer function handler.
RUN npm i

# Copy outer function handler
COPY index.js ./

# COPY function node packages and install, adding this as a separate
# entry allows caching of npm install

WORKDIR /home/app/function

COPY function/*.json ./

RUN npm i || :

# COPY function files and folders
COPY function/ ./

# Run any tests that may be available
RUN npm test

# Set correct permissions to use non root user
WORKDIR /home/app/

# chmod for tmp is for a buildkit issue (@alexellis)
RUN chown app:app -R /home/app \
&& chmod 777 /tmp

USER app

ENV cgi_headers="true"
ENV fprocess="node index.js"
ENV mode="http"
ENV upstream_url="http://127.0.0.1:3000"

ENV exec_timeout="10s"
ENV write_timeout="15s"
ENV read_timeout="15s"

HEALTHCHECK --interval=3s CMD [ -e /tmp/.lock ] || exit 1

CMD ["fwatchdog"]

12 changes: 12 additions & 0 deletions template/node12-debian/function/handler.js
@@ -0,0 +1,12 @@
'use strict'

module.exports = async (event, context) => {
const result = {
'status': 'Received input: ' + JSON.stringify(event.body)
}

return context
.status(200)
.succeed(result)
}

12 changes: 12 additions & 0 deletions template/node12-debian/function/package.json
@@ -0,0 +1,12 @@
{
"name": "openfaas-function",
"version": "1.0.0",
"description": "OpenFaaS Function",
"main": "handler.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 0"
},
"keywords": [],
"author": "OpenFaaS Ltd",
"license": "MIT"
}
120 changes: 120 additions & 0 deletions template/node12-debian/index.js
@@ -0,0 +1,120 @@
// Copyright (c) Alex Ellis 2017. All rights reserved.
// Copyright (c) OpenFaaS Author(s) 2020. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

"use strict"

const express = require('express')
const app = express()
const handler = require('./function/handler');
const bodyParser = require('body-parser')

if (process.env.RAW_BODY === 'true') {
app.use(bodyParser.raw({ type: '*/*' }))
} else {
var jsonLimit = process.env.MAX_JSON_SIZE || '100kb' //body-parser default
app.use(bodyParser.json({ limit: jsonLimit}));
app.use(bodyParser.raw()); // "Content-Type: application/octet-stream"
app.use(bodyParser.text({ type : "text/*" }));
}

app.disable('x-powered-by');

class FunctionEvent {
constructor(req) {
this.body = req.body;
this.headers = req.headers;
this.method = req.method;
this.query = req.query;
this.path = req.path;
}
}

class FunctionContext {
constructor(cb) {
this.value = 200;
this.cb = cb;
this.headerValues = {};
this.cbCalled = 0;
}

status(value) {
if(!value) {
return this.value;
}

this.value = value;
return this;
}

headers(value) {
if(!value) {
return this.headerValues;
}

this.headerValues = value;
return this;
}

succeed(value) {
let err;
this.cbCalled++;
this.cb(err, value);
}

fail(value) {
let message;
this.cbCalled++;
this.cb(value, message);
}
}

var middleware = async (req, res) => {
let cb = (err, functionResult) => {
if (err) {
console.error(err);

return res.status(500).send(err.toString ? err.toString() : err);
}

if(isArray(functionResult) || isObject(functionResult)) {
res.set(fnContext.headers()).status(fnContext.status()).send(JSON.stringify(functionResult));
} else {
res.set(fnContext.headers()).status(fnContext.status()).send(functionResult);
}
};

let fnEvent = new FunctionEvent(req);
let fnContext = new FunctionContext(cb);

Promise.resolve(handler(fnEvent, fnContext, cb))
.then(res => {
if(!fnContext.cbCalled) {
fnContext.succeed(res);
}
})
.catch(e => {
cb(e);
});
};

app.post('/*', middleware);
app.get('/*', middleware);
app.patch('/*', middleware);
app.put('/*', middleware);
app.delete('/*', middleware);
app.options('/*', middleware);

const port = process.env.http_port || 3000;

app.listen(port, () => {
console.log(`OpenFaaS Node.js listening on port: ${port}`)
});

let isArray = (a) => {
return (!!a) && (a.constructor === Array);
};

let isObject = (a) => {
return (!!a) && (a.constructor === Object);
};
16 changes: 16 additions & 0 deletions template/node12-debian/package.json
@@ -0,0 +1,16 @@
{
"name": "openfaas-node12",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no tests specified\" && exit 0"
},
"keywords": [],
"author": "OpenFaaS Ltd",
"license": "MIT",
"dependencies": {
"body-parser": "^1.18.2",
"express": "^4.16.2"
}
}
15 changes: 15 additions & 0 deletions template/node12-debian/template.yml
@@ -0,0 +1,15 @@
language: node12-debian
fprocess: node index.js
welcome_message: |
You have created a new function which uses Node.js 12 (TLS) and the OpenFaaS
of-watchdog which gives greater control over HTTP responses.
This template uses Debian Linux to help with building native dependencies
that may require node-gyp.
npm i --save can be used to add third-party packages like request or cheerio
npm documentation: https://docs.npmjs.com/
Unit tests are run at build time via "npm run", edit package.json to specify
how you want to execute them.

0 comments on commit cdef5a5

Please sign in to comment.