-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.ts
More file actions
46 lines (41 loc) · 1.52 KB
/
Copy pathindex.ts
File metadata and controls
46 lines (41 loc) · 1.52 KB
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
import * as api from "./api/index.ts";
import { MountPointManager } from "./api/mount.ts";
MountPointManager.setMountPoint(Deno.env.get("REALFS_MOUNT_POINT") || "./mnt");
try {
const exists = Deno.statSync(MountPointManager.getMountPoint());
if (!exists.isDirectory) {
throw new Error(`Mount point is not a directory`);
}
}
catch (err) {
console.error(`Mount point does not exist or is inaccessible.`);
console.error("The error was:");
console.error(err);
Deno.exit(1);
}
const handler = ((req: Request) => {
const url = new URL(req.url);
if (url.pathname === "/") {
return new Response("RealFS next generation prototype");
}
else if (url.pathname.startsWith("/api")) {
if (url.pathname === "/api") {
return new Response(JSON.stringify(Object.keys(api)), { headers: { "Content-Type": "application/json" } });
}
const version = url.pathname.split("/api/")[1].split("/")[0] as keyof typeof api;
const api_obj = api[version];
if (!api_obj) {
return new Response("404 Not Found", { status: 404 });
}
if (url.pathname === `/api/${version}`) {
return new Response(api_obj.API_VERSION, { headers: { "Content-Type": "application/json" } });
}
return api_obj.handler(req, url.pathname.split(`/api/${version}/`)[1]);
}
else {
return new Response("404 Not Found", { status: 404 });
}
});
Deno.serve({
port: parseInt(Deno.env.get("REALFS_PORT") ?? "8000")
}, handler);