Skip to content

Commit

Permalink
first spike of a simple nodejs runtime for funktion
Browse files Browse the repository at this point in the history
  • Loading branch information
jstrachan committed Dec 12, 2016
1 parent d966cb8 commit fa1eb81
Show file tree
Hide file tree
Showing 4 changed files with 117 additions and 0 deletions.
7 changes: 7 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# A docker image for the func container.

FROM node:4-onbuild

ADD server.js /usr/src/app/server.js

EXPOSE 8888
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
# funktion-nodejs-runtime

the NodeJS runtime for funktion
31 changes: 31 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
{
"name": "funktion-nodejs-runtime",
"version": "0.0.0",
"author": "Soam Vasani",
"contributors": [
{
"name": "Soam Vasani",
"email": "[email protected]"
},
{
"name": "Funktion Team",
"email": "[email protected]"
}
],
"description": "NodeJS container for the funktion framework",
"engines": {
"node": ">=4.2.2"
},
"dependencies": {
"express": "",
"minimist": "",
"body-parser": "",
"morgan": "",

"co": "~4.6.0",
"request": "",
"request-promise": "^1.0.2",
"mz": "~2.1.0",
"underscore": ">=1.8.3"
}
}
78 changes: 78 additions & 0 deletions server.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
'use strict';

const fs = require('fs');
const process = require('process');
const express = require('express');
const app = express();
const bodyParser = require('body-parser');
const morgan = require('morgan');

// Command line opts
const argv = require('minimist')(process.argv.slice(1));
if (!argv.codepath) {
argv.codepath = "/funktion/source.js";
console.log("Codepath defaulting to ", argv.codepath);
}
if (!argv.port) {
console.log("Port defaulting to 8888");
argv.port = 8888;
}

// User function. Starts out undefined.
let userFunction;

//
// Require the user function.
// The user function is read from argv.codepath
// it's expected to be placed there by the funktion runtime.
//
function requireUserFunction() {
// Read and load the code. It's placed there securely by the fission runtime.
try {
var startTime = process.hrtime();
userFunction = require(argv.codepath);
var elapsed = process.hrtime(startTime);
console.log(`user code loaded in ${elapsed[0]}sec ${elapsed[1]/1000000}ms`);
} catch(e) {
console.error(`user code load error: ${e}`);
}
}

// Request logger
app.use(morgan('combined'))

app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
app.use(bodyParser.raw());

requireUserFunction();

// Generic route -- all http requests go to the user function.
app.all('/', function (req, res) {
if (!userFunction) {
res.status(500).send("Generic container: no requests supported");
return;
}
const context = {
request: req,
response: res
// TODO: context should also have: URL template params, query string
};
function callback(status, body, headers) {
if (!status)
return;
if (headers) {
for (let name of Object.keys(headers)) {
res.set(name, headers[name]);
}
}
res.status(status).send(body);
}
try {
userFunction(context, callback);
} catch(e) {
callback(500, "Internal server error")
}
});

app.listen(argv.port);

0 comments on commit fa1eb81

Please sign in to comment.