-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathserver.js
51 lines (39 loc) · 1.31 KB
/
server.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
const express = require('express');
const bodyparser = require('body-parser')
const fs = require('fs');
const { spawn } = require('child_process');
const path = require('path');
// Constants
const PORT = 9000;
const HOST = '0.0.0.0';
const app = express();
app.use(bodyparser.raw({ limit: '5mb', type: '*/*' }))
// Web function invocation
app.get('/hello', (req, res) => {
res.send('Hello Serverless Cloud Function , Web Function\n');
});
app.options('/func', (req, res) => {
res.setHeader('Access-Control-Allow-Headers', '*');
res.setHeader('Access-Control-Allow-Origin', '*');
res.end();
});
app.post('/func', (req, res) => {
const wasmedge = spawn(path.join(__dirname, 'wasmedge'), [path.join(__dirname, 'grayscale.so')]);
let d = [];
wasmedge.stdout.on('data', (data) => {
d.push(data);
});
wasmedge.on('close', (code) => {
let buf = Buffer.concat(d);
res.setHeader('Content-Type', req.headers['image-type']);
res.setHeader('Access-Control-Allow-Origin', '*');
res.setHeader('Access-Control-Allow-Headers', '*');
res.send(buf);
});
wasmedge.stdin.write(req.body);
wasmedge.stdin.end('');
});
var server = app.listen(PORT, HOST);
console.log(`SCF Running on http://${HOST}:${PORT}`);
server.timeout = 0; // never timeout
server.keepAliveTimeout = 0; // keepalive, never timeout