This repository has been archived by the owner on May 6, 2022. It is now read-only.
/
index.ts
130 lines (121 loc) · 3.47 KB
/
index.ts
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
import {
asr,
asrSocketServer,
googleASR,
googleASRSocketServer,
spokestackMiddleware
} from 'spokestack'
import fileUpload, { UploadedFile } from 'express-fileupload'
import bodyParser from 'body-parser'
import { createServer } from 'http'
import express from 'express'
import next from 'next'
import searchGitHub from './searchGitHub'
const port = parseInt(process.env.PORT || '3000', 10)
const dev = process.env.NODE_ENV !== 'production'
const app = next({ dev })
const handle = app.getRequestHandler()
const useGoogleAsr = process.env.ASR_SERVICE === 'google'
app.prepare().then(() => {
const expressApp = express()
const middleware = spokestackMiddleware()
expressApp.use(
'/spokestack-web-worker.js',
express.static(`./node_modules/spokestack/dist/spokestack-web-worker${dev ? '' : '.min'}.js`)
)
expressApp.use('/graphql', bodyParser.json(), (req, res) => {
const accept = req.headers.accept || ''
// Show the playground only in development
if (
process.env.NODE_ENV !== 'production' &&
accept.includes('text/html') &&
!accept.includes('json')
) {
const playground = require('graphql-playground-middleware-express').default
return playground({
endpoint: '/graphql',
settings: {
'request.credentials': 'same-origin'
}
})(req, res)
}
return middleware(req, res)
})
expressApp.post('/asr', fileUpload(), (req, res) => {
if (!req.body || !req.body.sampleRate) {
res.status(400)
res.json({
message: 'Parameter required: "sampleRate"'
})
return
}
if (!req.files || !req.files.audio) {
res.status(400)
res.json({
message: 'Parameter required: "audio"'
})
return
}
const audio = req.files.audio as UploadedFile
const content = Buffer.from(audio.data.buffer)
// writeFileSync(path.join(__dirname, '..', '.files/audio.int16'), content)
if (!content) {
res.status(400)
res.json({
message: 'The audio file is not valid.'
})
return
}
const fetcher = useGoogleAsr
? googleASR(content, Number(req.body.sampleRate))
: asr(content, Number(req.body.sampleRate))
fetcher
.then((text) => {
res.status(200)
res.json({ text })
})
.catch((error: Error) => {
console.error(error)
if (error.message.includes('Authentication')) {
res.status(400)
res.json({
message: 'Spokestack Authentication Failed. Please check credentials.'
})
return
}
res.status(500)
res.json({
message: 'Unknown error during speech recognition. Check server logs.'
})
})
})
expressApp.post('/search', bodyParser.json(), (req, res) => {
if (!req.body || !req.body.term) {
res.status(400).json({
message: 'Parameter required: "term"'
})
return
}
searchGitHub(req.body.term)
.then((result) => {
res.json(result)
})
.catch((err) => {
console.error(err)
res.status(500)
res.json({
message: 'Unknown error during GitHub search. Check server logs.'
})
})
})
expressApp.all('*', (req, res) => handle(req, res))
const server = createServer(expressApp)
if (useGoogleAsr) {
googleASRSocketServer({ server })
} else {
asrSocketServer({ server })
}
server.listen(port, () => {
console.log(`Listening at http://localhost:${port}`)
})
})