Skip to content

Commit c57b49c

Browse files
authored
Merge pull request #4 from watson-developer-cloud/cors-workaround
Implement CORS issue workaround for new voices
2 parents a636d45 + 6a5dd8b commit c57b49c

File tree

6 files changed

+144
-307
lines changed

6 files changed

+144
-307
lines changed

Diff for: app.js

+26-123
Original file line numberDiff line numberDiff line change
@@ -1,117 +1,17 @@
11
const TextToSpeechV1 = require('ibm-watson/text-to-speech/v1.js');
2-
const {
3-
BearerTokenAuthenticator,
4-
CloudPakForDataAuthenticator,
5-
Cp4dTokenManager,
6-
IamAuthenticator,
7-
IamTokenManager,
8-
} = require('ibm-watson/auth');
92
const path = require('path');
103
const express = require('express');
11-
const vcapServices = require('vcap_services');
124
const app = express();
135
require('./config/express')(app);
146

15-
let url = process.env.TEXT_TO_SPEECH_URL;
16-
let authUrl = process.env.TEXT_TO_SPEECH_AUTH_URL;
17-
18-
// Supply the API key for IAM authentication.
19-
let apikey = process.env.TEXT_TO_SPEECH_APIKEY;
20-
21-
// Supply the bearer token + URL for an instance on CPD (see the README for more details).
22-
let bearerToken = process.env.TEXT_TO_SPEECH_BEARER_TOKEN;
23-
24-
// Supply the username + password + URL as an alternative for an instance on CPD.
25-
let username = process.env.TEXT_TO_SPEECH_USERNAME;
26-
let password = process.env.TEXT_TO_SPEECH_PASSWORD;
27-
28-
// On Cloud Foundry, we'll have a VCAP_SERVICES environment variable with credentials.
29-
let vcapCredentials = vcapServices.getCredentials('text_to_speech');
30-
31-
// Create appropriate token manager and client.
7+
// Create Text to Speech client.
328
let client;
33-
let tokenManager;
34-
if (vcapCredentials || apikey) {
35-
// Choose credentials from VCAP if they exist.
36-
apikey = (vcapCredentials && vcapCredentials.apikey) || apikey;
37-
url = (vcapCredentials && vcapCredentials.url) || url;
38-
39-
try {
40-
tokenManager = new IamTokenManager({ apikey });
41-
client = new TextToSpeechV1({
42-
serviceUrl: url,
43-
authenticator: new IamAuthenticator({ apikey }),
44-
});
45-
} catch (err) {
46-
console.error('Error creating IAM token manager and client: ', err);
47-
}
48-
} else if (username && password && url) {
49-
try {
50-
tokenManager = new Cp4dTokenManager({ username, password, url: authUrl });
51-
client = new TextToSpeechV1({
52-
serviceUrl: url,
53-
disableSslVerification: true,
54-
authenticator: new CloudPakForDataAuthenticator({
55-
username,
56-
password,
57-
url: authUrl,
58-
disableSslVerification: true,
59-
}),
60-
});
61-
} catch (err) {
62-
console.error('Error creating CP4D token manager: ', err);
63-
}
64-
} else if (bearerToken) {
65-
client = new TextToSpeechV1({
66-
serviceUrl: url,
67-
disableSslVerification: true,
68-
authenticator: new BearerTokenAuthenticator({ bearerToken }),
69-
});
9+
try {
10+
client = new TextToSpeechV1({});
11+
} catch (err) {
12+
console.error('Error creating service client: ', err);
7013
}
7114

72-
const getToken = async () => {
73-
let tokenResponse = {};
74-
75-
try {
76-
if (tokenManager) {
77-
const token = await tokenManager.getToken();
78-
tokenResponse = {
79-
...tokenResponse,
80-
accessToken: token,
81-
url,
82-
};
83-
} else if (bearerToken && url) {
84-
tokenResponse = {
85-
...tokenResponse,
86-
accessToken: bearerToken,
87-
url,
88-
};
89-
} else {
90-
tokenResponse = {
91-
...tokenResponse,
92-
error: {
93-
title: 'No valid credentials found',
94-
description:
95-
'Could not find valid credentials for the Text to Speech service.',
96-
statusCode: 401,
97-
},
98-
};
99-
}
100-
} catch (err) {
101-
tokenResponse = {
102-
...tokenResponse,
103-
error: {
104-
title: 'Authentication error',
105-
description:
106-
'There was a problem authenticating with the Text to Speech service.',
107-
statusCode: 400,
108-
},
109-
};
110-
}
111-
112-
return tokenResponse;
113-
};
114-
11515
app.get('/', (_, res) => {
11616
res.sendFile(path.join(__dirname, 'build', 'index.html'));
11717
});
@@ -120,34 +20,37 @@ app.get('/health', (_, res) => {
12020
res.json({ status: 'UP' });
12121
});
12222

123-
app.get('/api/auth', async (_, res, next) => {
124-
const token = await getToken();
125-
126-
if (token.error) {
127-
console.error(token.error);
128-
next(token.error);
129-
} else {
130-
return res.json(token);
131-
}
132-
});
133-
13423
app.get('/api/voices', async (_, res, next) => {
13524
try {
13625
const { result } = await client.listVoices();
13726
return res.json(result);
138-
} catch (error) {
139-
console.error(error);
27+
} catch (err) {
28+
console.error(err);
14029
if (!client) {
141-
error.statusCode = 401;
142-
error.description =
30+
err.statusCode = 401;
31+
err.description =
14332
'Could not find valid credentials for the Text to Speech service.';
144-
error.title = 'Invalid credentials';
33+
err.title = 'Invalid credentials';
14534
}
146-
next(error);
35+
next(err);
14736
}
14837
});
14938

150-
app.pos;
39+
app.get('/api/synthesize', async (req, res, next) => {
40+
try {
41+
const { result } = await client.synthesize(req.query);
42+
result.pipe(res);
43+
} catch (err) {
44+
console.error(err);
45+
if (!client) {
46+
err.statusCode = 401;
47+
err.description =
48+
'Could not find valid credentials for the Text to Speech service.';
49+
err.title = 'Invalid credentials';
50+
}
51+
next(err);
52+
}
53+
});
15154

15255
// error-handler settings for all other routes
15356
require('./config/error-handler')(app);

0 commit comments

Comments
 (0)