1
1
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' ) ;
9
2
const path = require ( 'path' ) ;
10
3
const express = require ( 'express' ) ;
11
- const vcapServices = require ( 'vcap_services' ) ;
12
4
const app = express ( ) ;
13
5
require ( './config/express' ) ( app ) ;
14
6
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.
32
8
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 ) ;
70
13
}
71
14
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
-
115
15
app . get ( '/' , ( _ , res ) => {
116
16
res . sendFile ( path . join ( __dirname , 'build' , 'index.html' ) ) ;
117
17
} ) ;
@@ -120,34 +20,37 @@ app.get('/health', (_, res) => {
120
20
res . json ( { status : 'UP' } ) ;
121
21
} ) ;
122
22
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
-
134
23
app . get ( '/api/voices' , async ( _ , res , next ) => {
135
24
try {
136
25
const { result } = await client . listVoices ( ) ;
137
26
return res . json ( result ) ;
138
- } catch ( error ) {
139
- console . error ( error ) ;
27
+ } catch ( err ) {
28
+ console . error ( err ) ;
140
29
if ( ! client ) {
141
- error . statusCode = 401 ;
142
- error . description =
30
+ err . statusCode = 401 ;
31
+ err . description =
143
32
'Could not find valid credentials for the Text to Speech service.' ;
144
- error . title = 'Invalid credentials' ;
33
+ err . title = 'Invalid credentials' ;
145
34
}
146
- next ( error ) ;
35
+ next ( err ) ;
147
36
}
148
37
} ) ;
149
38
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
+ } ) ;
151
54
152
55
// error-handler settings for all other routes
153
56
require ( './config/error-handler' ) ( app ) ;
0 commit comments