This repository has been archived by the owner on Feb 15, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 36
/
app.js
116 lines (103 loc) · 3.23 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
const { Cp4dTokenManager, IamTokenManager } = require('ibm-watson/auth');
const path = require('path');
const express = require('express');
const vcapServices = require('vcap_services');
const app = express();
require('./config/express')(app);
// For starter kit env.
require('dotenv').config({
silent: true
});
const skitJson = JSON.parse(process.env.service_watson_speech_to_text || "{}");
const vcapCredentials = vcapServices.getCredentials('speech_to_text');
// Look for credentials in all the possible places
const apikey = process.env.SPEECH_TO_TEXT_APIKEY || process.env.SPEECHTOTEXT_APIKEY || vcapCredentials?.apikey || skitJson?.apikey;
const url = process.env.SPEECH_TO_TEXT_URL || process.env.SPEECHTOTEXT_URL || vcapCredentials?.url || skitJson?.url;
let bearerToken = process.env.SPEECH_TO_TEXT_BEARER_TOKEN;
// Ensure we have a SPEECH_TO_TEXT_AUTH_TYPE so we can get a token for the UI.
let sttAuthType = process.env.SPEECH_TO_TEXT_AUTH_TYPE;
if (!sttAuthType) {
sttAuthType = 'iam';
} else {
sttAuthType = sttAuthType.toLowerCase();
}
// Get a token manager for IAM or CP4D.
let tokenManager = false;
if (sttAuthType === 'cp4d') {
tokenManager = new Cp4dTokenManager({
username: process.env.SPEECH_TO_TEXT_USERNAME,
password: process.env.SPEECH_TO_TEXT_PASSWORD,
url: process.env.SPEECH_TO_TEXT_AUTH_URL,
disableSslVerification: process.env.SPEECH_TO_TEXT_AUTH_DISABLE_SSL || false
});
} else if (sttAuthType === 'iam') {
try {
tokenManager = new IamTokenManager({ apikey });
} catch (err) {
console.log("Error: ", err);
}
} else if (sttAuthType === 'bearertoken') {
console.log('SPEECH_TO_TEXT_AUTH_TYPE=bearertoken is for dev use only.');
} else {
console.log('SPEECH_TO_TEXT_AUTH_TYPE =', sttAuthType);
console.log('SPEECH_TO_TEXT_AUTH_TYPE is not recognized.');
}
const getToken = async () => {
let tokenResponse = {};
try {
if (tokenManager) {
const token = await tokenManager.getToken();
tokenResponse = {
...tokenResponse,
accessToken: token,
url,
};
} else if (bearerToken && url) {
tokenResponse = {
...tokenResponse,
accessToken: bearerToken,
url,
};
} else {
tokenResponse = {
...tokenResponse,
error: {
title: 'No valid credentials found',
description:
'Could not find valid credentials for the Speech to Text service.',
statusCode: 401,
},
};
}
} catch (err) {
console.log("Error: ", err);
tokenResponse = {
...tokenResponse,
error: {
title: 'Authentication error',
description:
'There was a problem authenticating with the Speech to Text service.',
statusCode: 400,
},
};
}
return tokenResponse;
};
app.get('/', (_, res) => {
res.sendFile(path.join(__dirname, 'build', 'index.html'));
});
app.get('/health', (_, res) => {
res.json({ status: 'UP' });
});
app.get('/api/auth', async (_, res, next) => {
const token = await getToken();
if (token.error) {
console.error(token.error);
next(token.error);
} else {
return res.json(token);
}
});
// error-handler settings for all other routes
require('./config/error-handler')(app);
module.exports = app;