-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
first spike of a simple nodejs runtime for funktion
- Loading branch information
Showing
4 changed files
with
117 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
# funktion-nodejs-runtime | ||
|
||
the NodeJS runtime for funktion |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |