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 10
/
app.js
101 lines (89 loc) · 2.67 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
const TextToSpeechV1 = require('ibm-watson/text-to-speech/v1.js');
const path = require('path');
const express = require('express');
const app = express();
require('./config/express')(app);
// For starter kit env.
require('dotenv').config({
silent: true
});
const pEnv = process.env;
const skitJson = JSON.parse(process.env.service_watson_text_to_speech || "{}");
// Look for credentials in all the places
const apikey = process.env.TEXT_TO_SPEECH_APIKEY || process.env.TEXTTOSPEECH_APIKEY || skitJson?.apikey;
const url = process.env.TEXT_TO_SPEECH_URL ||
process.env.TEXTTOSPEECH_URL || skitJson?.url;
// A null/undefined service env var would actually cause
// the core SDK to throw an error in integration tests
// and fail the test, but if the env var is left unset
// it won't
if (apikey) {
process.env.TEXT_TO_SPEECH_APIKEY = apikey;
}
if (url) {
process.env.TEXT_TO_SPEECH_URL = url;
}
// Create Text to Speech client.
let client;
try {
client = new TextToSpeechV1({ version: '2020-06-02' });
} catch (err) {
console.error('Error creating service client: ', err);
}
// Caching issues are causing alerts. Avoid cache.
var options = {
etag: false,
maxAge: '0',
setHeaders: function (res, _path, _stat) {
res.set('Cache-Control', 'private, no-cache, no-store, must-revalidate');
res.header('Pragma', 'no-cache');
res.header('Expires', '0');
}
}
app.use(express.static(path.join(__dirname, 'build'), options ));
app.get('/health', (_, res) => {
res.json({ status: 'UP' });
});
app.get('/api/voices', async (_, res, next) => {
try {
if (client) {
const { result } = await client.listVoices();
return res.json(result);
} else {
// Return Allison for testing and user still gets creds pop-up.
return res.json(
{ voices: [
{ name: 'en-US_AllisonV3Voice',
description: 'Allison: American English female voice. Dnn technology.',
}]
});
}
} catch (err) {
console.error(err);
if (!client) {
err.statusCode = 401;
err.description =
'Could not find valid credentials for the Text to Speech service.';
err.title = 'Invalid credentials';
}
next(err);
}
});
app.get('/api/synthesize', async (req, res, next) => {
try {
const { result } = await client.synthesize(req.query);
result.pipe(res);
} catch (err) {
console.error(err);
if (!client) {
err.statusCode = 401;
err.description =
'Could not find valid credentials for the Text to Speech service.';
err.title = 'Invalid credentials';
}
next(err);
}
});
// error-handler settings for all other routes
require('./config/error-handler')(app);
module.exports = app;