-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.js
175 lines (155 loc) · 4.9 KB
/
server.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
require("dotenv").config();
const express = require("express");
const multer = require("multer");
const app = express();
const upload = multer();
const cors = require("cors");
const bodyParser = require("body-parser");
const axios = require("axios");
/** Define constants and configure TL API endpoints */
const TWELVE_LABS_API_KEY = process.env.REACT_APP_API_KEY;
const API_BASE_URL = "https://api.twelvelabs.io/v1.2";
const PORT_NUMBER = process.env.REACT_APP_PORT_NUMBER || 4001;
/** Set up middleware for Express */
app.use(cors());
app.use(express.json());
app.use(bodyParser.json());
app.use(
bodyParser.urlencoded({
extended: true,
})
);
/** Define error handling middleware */
const errorLogger = (error, request, response, next) => {
console.error(error);
next(error);
};
const errorHandler = (error, request, response, next) => {
return response
.status(error.status || 500)
.json(error || "Something Went Wrong...");
};
app.use(errorLogger, errorHandler);
process.on("uncaughtException", function (exception) {
console.log(exception);
});
/** Set up Express server to listen on port 4002 */
app.listen(PORT_NUMBER, () => {
console.log(`Server Running. Listening on port ${PORT_NUMBER}`);
});
const HEADERS = {
"Content-Type": "application/json",
"x-api-key": TWELVE_LABS_API_KEY,
};
/** Get videos */
app.get("/indexes/:indexId/videos", async (request, response, next) => {
const params = {
page_limit: request.query.page_limit,
};
try {
const options = {
method: "GET",
url: `${API_BASE_URL}/indexes/${request.params.indexId}/videos`,
headers: { ...HEADERS },
data: { params },
};
const apiResponse = await axios.request(options);
response.json(apiResponse.data);
} catch (error) {
const status = error.response?.status || 500;
const message = error.response?.data?.message || "Error Getting Videos";
return next({ status, message });
}
});
/** Get a video of an index */
app.get(
"/indexes/:indexId/videos/:videoId",
async (request, response, next) => {
const indexId = request.params.indexId;
const videoId = request.params.videoId;
try {
const options = {
method: "GET",
url: `${API_BASE_URL}/indexes/${indexId}/videos/${videoId}`,
headers: { ...HEADERS },
};
const apiResponse = await axios.request(options);
response.json(apiResponse.data);
} catch (error) {
const status = error.response?.status || 500;
const message = error.response?.data?.message || "Error Getting a Video";
return next({ status, message });
}
}
);
/** Generate open-ended text of a video */
app.post("/videos/:videoId/generate", async (request, response, next) => {
const videoId = request.params.videoId;
let prompt = request.body.data;
try {
const options = {
method: "POST",
url: `${API_BASE_URL}/generate`,
headers: { ...HEADERS, accept: "application/json" },
data: { ...prompt, video_id: videoId, temperature: 0.3 },
};
const apiResponse = await axios.request(options);
response.json(apiResponse.data);
} catch (error) {
const status = error.response?.status || 500;
const message = error.response?.data?.message || "Error Generating Text";
return next({ status, message });
}
});
/** Index a video and return a task ID */
app.post(
"/index",
upload.single("video_file"),
async (request, response, next) => {
const formData = new FormData();
// Append data from request.body
Object.entries(request.body).forEach(([key, value]) => {
formData.append(key, value);
});
const blob = new Blob([request.file.buffer], {
type: request.file.mimetype,
});
formData.append("video_file", blob, request.file.originalname);
const options = {
method: "POST",
url: `${API_BASE_URL}/tasks`,
headers: {
"x-api-key": TWELVE_LABS_API_KEY,
accept: "application/json",
"Content-Type":
"multipart/form-data; boundary=---011000010111000001101001",
},
data: formData,
};
try {
const apiResponse = await axios.request(options);
response.json(apiResponse.data);
} catch (error) {
const status = error.response?.status || 500;
const message = error.response?.data?.message || "Error indexing a Video";
return next({ status, message });
}
}
);
/** Check the status of a specific indexing task */
app.get("/tasks/:taskId", async (request, response, next) => {
const taskId = request.params.taskId;
try {
const options = {
method: "GET",
url: `${API_BASE_URL}/tasks/${taskId}`,
headers: { ...HEADERS },
};
const apiResponse = await axios.request(options);
response.json(apiResponse.data);
} catch (error) {
const status = error.response?.status || 500;
const message = error.response?.data?.message || "Error getting a task";
return next({ status, message });
}
});