Skip to content

Commit fa1eb81

Browse files
committed
first spike of a simple nodejs runtime for funktion
1 parent d966cb8 commit fa1eb81

4 files changed

Lines changed: 117 additions & 0 deletions

File tree

Dockerfile

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# A docker image for the func container.
2+
3+
FROM node:4-onbuild
4+
5+
ADD server.js /usr/src/app/server.js
6+
7+
EXPOSE 8888

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
# funktion-nodejs-runtime
2+
23
the NodeJS runtime for funktion

package.json

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
{
2+
"name": "funktion-nodejs-runtime",
3+
"version": "0.0.0",
4+
"author": "Soam Vasani",
5+
"contributors": [
6+
{
7+
"name": "Soam Vasani",
8+
"email": "soamvasani@platform9.com"
9+
},
10+
{
11+
"name": "Funktion Team",
12+
"email": "funktionio@googlegroups.com"
13+
}
14+
],
15+
"description": "NodeJS container for the funktion framework",
16+
"engines": {
17+
"node": ">=4.2.2"
18+
},
19+
"dependencies": {
20+
"express": "",
21+
"minimist": "",
22+
"body-parser": "",
23+
"morgan": "",
24+
25+
"co": "~4.6.0",
26+
"request": "",
27+
"request-promise": "^1.0.2",
28+
"mz": "~2.1.0",
29+
"underscore": ">=1.8.3"
30+
}
31+
}

server.js

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
'use strict';
2+
3+
const fs = require('fs');
4+
const process = require('process');
5+
const express = require('express');
6+
const app = express();
7+
const bodyParser = require('body-parser');
8+
const morgan = require('morgan');
9+
10+
// Command line opts
11+
const argv = require('minimist')(process.argv.slice(1));
12+
if (!argv.codepath) {
13+
argv.codepath = "/funktion/source.js";
14+
console.log("Codepath defaulting to ", argv.codepath);
15+
}
16+
if (!argv.port) {
17+
console.log("Port defaulting to 8888");
18+
argv.port = 8888;
19+
}
20+
21+
// User function. Starts out undefined.
22+
let userFunction;
23+
24+
//
25+
// Require the user function.
26+
// The user function is read from argv.codepath
27+
// it's expected to be placed there by the funktion runtime.
28+
//
29+
function requireUserFunction() {
30+
// Read and load the code. It's placed there securely by the fission runtime.
31+
try {
32+
var startTime = process.hrtime();
33+
userFunction = require(argv.codepath);
34+
var elapsed = process.hrtime(startTime);
35+
console.log(`user code loaded in ${elapsed[0]}sec ${elapsed[1]/1000000}ms`);
36+
} catch(e) {
37+
console.error(`user code load error: ${e}`);
38+
}
39+
}
40+
41+
// Request logger
42+
app.use(morgan('combined'))
43+
44+
app.use(bodyParser.urlencoded({ extended: false }));
45+
app.use(bodyParser.json());
46+
app.use(bodyParser.raw());
47+
48+
requireUserFunction();
49+
50+
// Generic route -- all http requests go to the user function.
51+
app.all('/', function (req, res) {
52+
if (!userFunction) {
53+
res.status(500).send("Generic container: no requests supported");
54+
return;
55+
}
56+
const context = {
57+
request: req,
58+
response: res
59+
// TODO: context should also have: URL template params, query string
60+
};
61+
function callback(status, body, headers) {
62+
if (!status)
63+
return;
64+
if (headers) {
65+
for (let name of Object.keys(headers)) {
66+
res.set(name, headers[name]);
67+
}
68+
}
69+
res.status(status).send(body);
70+
}
71+
try {
72+
userFunction(context, callback);
73+
} catch(e) {
74+
callback(500, "Internal server error")
75+
}
76+
});
77+
78+
app.listen(argv.port);

0 commit comments

Comments
 (0)