-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
135 lines (118 loc) · 3.89 KB
/
app.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
// imports
require('dotenv').config();
const Logger = require('./Logger');
const log = Logger.getLogger('Main');
const express = require('express');
const morgan = require('morgan');
const WebSocketClient = require('websocket').client;
const axios = require('axios');
const fileUpload = require('express-fileupload');
const {apiMarketRequestValidator} = require('@apimarket/apimarket-server');
// let client = new WebSocketClient({closeTimeout: 10});
const app = express();
const pjson = require('./package.json');
const cors = require('cors');
// fields
const PORT = process.env.PORT || 3000;
const CREDENTIALS = process.env.OPEN_ID_CREDENTIALS;
const OPENID_ENDPOINT = process.env.OPEN_ID_ENDPOINT;
const WS_ENDPOINT_KOR = process.env.WS_ENDPOINT_KOR;
const WS_ENDPOINT_ENG = process.env.WS_ENDPOINT_ENG;
let accessToken = {access_token: '', expiration: new Date()};
// init
Logger.init();
app.use(cors());
app.use(apiMarketRequestValidator());
app.use(fileUpload({abortOnLimit: true, limits: {fileSize: 500 * 1024}})); // limit 500kb
app.use(morgan('combined', {
stream: {
write: function (str) {
log.info(str)
}
}
}));
const sendFile = (data, connection) => {
return new Promise((resolve, reject) => {
connection.sendBytes(data, (res, error) => {
if (error) {
reject(error);
}
log.info('Sent audio');
resolve();
});
});
};
const obtainToken = () => {
return new Promise((resolve, reject) => {
if (accessToken.expiration <= new Date(new Date().getTime() + 5 * 1000)) {
const b64 = Buffer.from(CREDENTIALS).toString('base64');
axios.post(OPENID_ENDPOINT,
'grant_type=client_credentials',
{headers: {Authorization: "Basic " + b64, MediaType: 'application/x-www-form-urlencoded'}})
.then(response => {
const data = response['data'];
accessToken = {
access_token: data['access_token'],
expiration: new Date(new Date().getTime() + data['expires_in'] * 1000)
};
resolve();
})
.catch(error => {
reject(error);
})
} else {
resolve();
}
});
};
app.post('/speech-to-text', (req, res) => {
const files = req.files;
const client = new WebSocketClient({closeTimeout: 10});
if (!files) {
return res.status(400).send('No files were uploaded.');
}
if (Object.keys(files).length !== 1) {
return res.status(400).send('Too many files were uploaded. Limit: 1');
}
const key = Object.keys(files)[0];
const file = files[key];
if (!file['mimetype'].startsWith('audio')) {
return res.status(415).send(file.mimetype + '. Only audio files allowed.');
}
obtainToken()
.then(() => {
if (key === 'korean') {
client.connect(WS_ENDPOINT_KOR + accessToken['access_token']);
} else {
client.connect(WS_ENDPOINT_ENG + accessToken['access_token']);
}
})
.catch(error => {
log.error(error);
res.status(503).send('Service temporarily unavailable.');
});
client.on('connectFailed', error => {
log.error(error.toString().split('\n')[0]);
res.status(503).send('Service temporarily unavailable.');
});
client.on('connect', connection => {
log.info('Opened websocket connection');
let result = {};
sendFile(file['data'], connection)
.then(() => connection.sendUTF('EOS', () => log.info('Sent EOS')))
.catch(error => log.error(error));
connection.on('message', data => {
log.info('onMessage', data);
const json = JSON.parse(data['utf8Data']);
if(json.hasOwnProperty('transcript')) {
result = {transcript: json['transcript']};
}
});
connection.on('close', (code, reason) => {
log.info('Connection closed', code, reason);
res.send(JSON.stringify(result));
});
});
});
log.info('Starting ' + pjson.name + '-' + pjson.version);
app.listen(PORT, () => log.info(`Server listening on port: ${PORT}`));