-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.ts
130 lines (101 loc) · 3.75 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 'dotenv/config'
import { Database } from '@hocuspocus/extension-database';
import { Logger } from '@hocuspocus/extension-logger';
import { Server } from '@hocuspocus/server';
import { Bucket, Storage } from '@google-cloud/storage';
import { utils } from './stream-helper'
import cors from 'cors';
import express from 'express';
import expressWebsockets from 'express-ws';
import ShortUniqueId from 'short-unique-id';
import fileStore from './fileStore';
import path from 'node:path';
const uid = new ShortUniqueId({ length: 20 });
const serverPort = parseInt(process.env.SERVER_PORT || '8080');
let storageBucket: Bucket | typeof fileStore;
if (process.env.STORAGE_TYPE === "local") {
storageBucket = fileStore
} else {
const storageClient = new Storage();
storageBucket = storageClient.bucket(process.env.STORAGE_GCP_BUCKET_NAME || 'athene-diagram-files');
}
const server = Server.configure({
extensions: [
new Database({
fetch: async ({ documentName }) => {
const file = storageBucket.file(`collaboration/${documentName}`);
const [exists] = await file.exists();
if (!exists) {
return null;
}
const buffers = await file.download();
return Buffer.concat(buffers);
},
store: async ({ documentName, state }) => {
const file = storageBucket.file(`collaboration/${documentName}`);
const stream = file.createWriteStream();
await utils.writeAsync(stream, state);
await utils.endAsync(stream);
},
}),
new Logger({
onChange: false
}),
]
});
const { app } = expressWebsockets(express());
app.use(cors());
app.use(express.json());
app.get('/api/health', (_, response) => {
response.send({
status: 'Healthy'
});
});
app.ws('/api/collaboration', (websocket, request) => {
server.handleConnection(websocket, request, {});
});
app.get('/api/:tokenRead', async (request, response) => {
const file = storageBucket.file(request.params.tokenRead);
const [exists] = await file.exists();
if (!exists) {
response.status(404);
return;
}
response.setHeader('Content-Type', 'application/json');
file.createReadStream().pipe(response);
});
app.post('/api/', async (request, response) => {
const tokenWrite = uid.rnd();
const tokenRead = uid.rnd();
const file = storageBucket.file(tokenRead);
await file.save(JSON.stringify(request.body, undefined, 2), {
metadata: {
contentType: 'application/json',
contentLength: undefined,
'write-token': tokenWrite
}
});
return response.status(201).json({ readToken: tokenRead, writeToken: tokenWrite });
});
app.put('/api/:tokenRead/:tokenWrite', async (request, response) => {
const tokenWrite = request.params.tokenWrite;
const tokenRead = request.params.tokenRead;
const file = storageBucket.file(request.params.tokenRead);
const [exists] = await file.exists();
if (!exists) {
response.status(404);
return;
}
const [metadata] = await file.getMetadata();
if (metadata['write-token'] !== request.params.tokenWrite) {
response.status(403);
return;
}
await file.save(JSON.stringify(request.body, undefined, 2));
return response.status(201).json({ readToken: tokenRead, writeToken: tokenWrite });
});
app.use(express.static(path.join(__dirname, './ui/dist'), {}))
app.get('/*', function(req, res) {
res.sendFile(path.join(__dirname, './ui/dist/index.html'));
});
app.listen(Number(serverPort), () => console.log(`Listening on http://127.0.0.1:${serverPort}`));