-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
90 lines (88 loc) · 2.72 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
import Fastify from "fastify";
import fastifyCompress from "@fastify/compress";
import fastifySensible from "@fastify/sensible";
import cors from "@fastify/cors";
const envToLogger = {
development: {
transport: {
target: "@fastify/one-line-logger",
},
// transport: {
// target: "pino-pretty",
// options: {
// translateTime: "HH:MM:ss Z",
// ignore: "pid,hostname",
// },
// },
},
};
const fastify = Fastify({
logger: envToLogger[process.env.NODE_ENV],
bodyLimit: 256 * 1024 * 1024,
});
import tusS3Uploader from "./src/index.js";
main();
async function main() {
fastify.addHook("onReady", () => {
console.log();
console.log("Registered routes:");
routes.map((r) => console.log(`${r.method}: ${r.url}`));
console.log("");
});
const routes = [];
fastify.addHook("onRoute", (route) => {
routes.push(route);
});
await fastify.register(cors, {
origin: "*",
methods: ["OPTIONS", "GET", "HEAD", "PATCH", "POST", "DELETE"],
allowedHeaders: [
"content-type",
"upload-length",
"content-length",
"upload-offset",
"upload-expires",
"location",
"upload-metadata",
"tus-resumable",
"tus-version",
"tus-max-size",
"tus-extension",
],
exposedHeaders: [
"content-type",
"upload-length",
"content-length",
"upload-offset",
"upload-expires",
"location",
"upload-metadata",
"tus-resumable",
"tus-version",
"tus-max-size",
"tus-extension",
],
});
fastify.register(fastifySensible);
fastify.register(fastifyCompress);
fastify.register((fastify, options, done) => {
fastify.addHook("preHandler", async (req, res) => {
// fake middleware checking the caller is authorised
if (req.headers.authorization !== "Bearer secret") res.badRequest();
});
fastify.register(tusS3Uploader, {
awsAccessKeyId: "root",
awsSecretAccessKey: "rootpass",
endpoint: "http://minio:9000",
forcePathStyle: true,
cachePath: "./.cache",
uploadRoutePath: "/files",
defaultUploadExpiration: { hours: 6 }, // https://date-fnsorg/v2.29.3/docs/add
});
done();
});
fastify.addHook("onRequest", async (req, res) => {
// console.log("***", req.method, req.url);
});
fastify.listen({ port: 8080, host: "0.0.0.0" }, function (err, address) {});
}