-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.js
180 lines (146 loc) · 4.35 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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
import {
GetObjectCommand,
PutObjectCommand,
DeleteObjectCommand,
S3Client
} from "@aws-sdk/client-s3"
import { getSignedUrl } from "@aws-sdk/s3-request-presigner";
import express from "express"
import expressWs from "express-ws"
import path from "path"
import url from "url"
import pubsub from "./pubsub.js"
import * as db from "./db.js"
const { app, getWss } = expressWs(express())
const port = 3000
const __dirname = url.fileURLToPath(new URL('.', import.meta.url))
app.use(express.json())
app.use(express.raw({ type: 'audio/*', limit: '10mb' }))
app.set('views', path.join(__dirname, 'views'))
app.set('view engine', 'ejs')
app.use(function (error, req, res, next) {
console.error(error)
res.status(500)
res.json({ error: error.message, stack: error.stack })
})
const S3 = new S3Client()
app.get('/', async (req, res) => {
let list = await db.query('SELECT name, text FROM clips ORDER BY id')
res.locals.clips = list.rows
res.locals.timestamp = pubsub.timestamp
res.render('index')
// wake up whisper machine
if (process.env.WHISPER_URL) {
fetch(process.env.WHISPER_URL)
}
})
app.use('/', express.static(path.join(__dirname, 'public')))
app.get("/audio/:name", async (req, res, next) => {
try {
const data = await S3.send(new GetObjectCommand({
Bucket: process.env.BUCKET_NAME,
Key: req.params.name
}))
res.setHeader("Content-Type", data.ContentType)
let { start, end } = req.range()[0] || {}
if (end) {
end = Math.min(end, data.ContentLength - 1)
if (end < start) {
res.status(416)
res.end()
return
}
res.setHeader("Content-Range", `bytes ${start}-${end}/${data.ContentLength}`)
res.setHeader("Accept-Ranges", "bytes")
res.setHeader("Content-Length", end - start + 1)
res.status(206)
res.end((await data.Body.transformToByteArray()).slice(start, end + 1))
} else {
data.Body.pipe(res)
}
} catch (err) {
if (err.name === "NoSuchKey") return next()
console.error(err)
console.error(err.stack)
res.status(500)
res.setHeader("Content-Type", "application/json")
res.json(err)
}
})
app.put("/audio/:name", async (req, res) => {
try {
const data = await S3.send(new PutObjectCommand({
Bucket: process.env.BUCKET_NAME,
Key: req.params.name,
Body: req.body,
ContentType: req.headers["content-type"]
}))
await db.query("INSERT INTO clips (name) VALUES ($1)", [req.params.name])
res.json(data)
if (process.env.WHISPER_URL) {
// Fetch the presigned URL to download this clip
let clip_url = await getSignedUrl(
S3,
new GetObjectCommand({
Bucket: process.env.BUCKET_NAME,
Key: req.params.name
}),
{ expiresIn: 3600 }
)
let input = { audio: clip_url }
let response = await fetch(process.env.WHISPER_URL, {
method: "PUT",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ input })
})
let results = await response.json()
await db.query(
"UPDATE clips SET text = $1 WHERE name = $2",
[results.output.transcription, req.params.name]
)
await pubsub.publish(new Date().toISOString())
}
} catch (err) {
console.error(err)
console.error(err.stack)
res.status(500)
res.json(err)
}
})
app.delete("/audio/:name", async (req, res) => {
try {
const data = await S3.send(new DeleteObjectCommand({
Bucket: process.env.BUCKET_NAME,
Key: req.params.name
}))
await db.query("DELETE FROM clips WHERE name = $1", [req.params.name])
res.json(data)
} catch (err) {
console.error(err)
console.error(err.stack)
res.status(500)
res.json(err)
}
})
// Define web socket route
app.ws('/websocket', ws => {
// update client on a best effort basis
try {
ws.send(pubsub.timestamp.toString());
} catch (error) {
console.error(error)
}
// We don’t expect any messages on websocket, but log any ones we do get.
ws.on('message', console.log)
})
// Publish count updates to all web socket clients
pubsub.connect(getWss())
app.post("/publish", async (req, res) => {
let timestamp = pubsub.timestamp
await pubsub.publish(req.body.timestamp)
res.json({ timestamp })
})
// Start the server
app.listen(port, () => {
console.log(`Example app listening on port ${port}`)
})