2022-02-21 08:32:36 +00:00
|
|
|
import * as path from "https://deno.land/std@0.125.0/path/mod.ts";
|
|
|
|
import FileInfo = Deno.FileInfo;
|
|
|
|
|
|
|
|
import { Application, Router } from "https://deno.land/x/oak/mod.ts";
|
|
|
|
import { oakCors } from "https://deno.land/x/cors@v1.2.0/mod.ts";
|
|
|
|
import { readAll } from "https://deno.land/std@0.126.0/streams/mod.ts";
|
2022-02-22 13:18:37 +00:00
|
|
|
import { exists } from "https://deno.land/std@0.126.0/fs/mod.ts";
|
2022-02-21 08:32:36 +00:00
|
|
|
|
2022-02-22 13:18:37 +00:00
|
|
|
const fsPrefix = "/fs";
|
|
|
|
const notesPath = "../notes";
|
2022-02-21 08:32:36 +00:00
|
|
|
|
|
|
|
const fsRouter = new Router();
|
|
|
|
|
|
|
|
fsRouter.use(oakCors());
|
|
|
|
|
2022-02-22 13:18:37 +00:00
|
|
|
fsRouter.get("/", async (context) => {
|
|
|
|
const localPath = notesPath;
|
|
|
|
let fileNames: string[] = [];
|
|
|
|
for await (const dirEntry of Deno.readDir(localPath)) {
|
|
|
|
if (dirEntry.isFile) {
|
|
|
|
fileNames.push(
|
|
|
|
dirEntry.name.substring(
|
|
|
|
0,
|
|
|
|
dirEntry.name.length - path.extname(dirEntry.name).length
|
|
|
|
)
|
|
|
|
);
|
2022-02-21 08:32:36 +00:00
|
|
|
}
|
2022-02-22 13:18:37 +00:00
|
|
|
}
|
|
|
|
context.response.body = JSON.stringify(fileNames);
|
2022-02-21 08:32:36 +00:00
|
|
|
});
|
|
|
|
|
2022-02-22 13:18:37 +00:00
|
|
|
fsRouter.get("/:note", async (context) => {
|
|
|
|
const noteName = context.params.note;
|
|
|
|
const localPath = `${notesPath}/${noteName}.md`;
|
|
|
|
try {
|
|
|
|
const text = await Deno.readTextFile(localPath);
|
|
|
|
context.response.body = text;
|
|
|
|
} catch (e) {
|
|
|
|
context.response.status = 404;
|
|
|
|
context.response.body = "";
|
|
|
|
}
|
2022-02-21 08:32:36 +00:00
|
|
|
});
|
|
|
|
|
2022-02-22 13:18:37 +00:00
|
|
|
fsRouter.options("/:note", async (context) => {
|
|
|
|
const localPath = `${notesPath}/${context.params.note}.md`;
|
|
|
|
try {
|
|
|
|
const stat = await Deno.stat(localPath);
|
|
|
|
context.response.headers.set("Content-length", `${stat.size}`);
|
|
|
|
} catch (e) {
|
|
|
|
context.response.status = 200;
|
|
|
|
context.response.body = "";
|
|
|
|
}
|
|
|
|
});
|
2022-02-21 08:32:36 +00:00
|
|
|
|
2022-02-22 13:18:37 +00:00
|
|
|
fsRouter.put("/:note", async (context) => {
|
|
|
|
const noteName = context.params.note;
|
|
|
|
const localPath = `${notesPath}/${noteName}.md`;
|
|
|
|
const existingNote = await exists(localPath);
|
|
|
|
let file;
|
|
|
|
try {
|
|
|
|
file = await Deno.create(localPath);
|
|
|
|
} catch (e) {
|
|
|
|
console.error("Error opening file for writing", localPath, e);
|
|
|
|
context.response.status = 500;
|
|
|
|
context.response.body = e.message;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const result = context.request.body({ type: "reader" });
|
|
|
|
const text = await readAll(result.value);
|
|
|
|
file.write(text);
|
|
|
|
file.close();
|
|
|
|
console.log("Wrote to", localPath);
|
|
|
|
context.response.status = existingNote ? 200 : 201;
|
|
|
|
context.response.body = "OK";
|
2022-02-21 08:32:36 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
const app = new Application();
|
2022-02-22 13:18:37 +00:00
|
|
|
app.use(
|
|
|
|
new Router()
|
|
|
|
.use(fsPrefix, fsRouter.routes(), fsRouter.allowedMethods())
|
|
|
|
.routes()
|
|
|
|
);
|
2022-02-21 08:32:36 +00:00
|
|
|
app.use(async (context, next) => {
|
2022-02-22 13:18:37 +00:00
|
|
|
try {
|
|
|
|
await context.send({
|
|
|
|
root: "../webapp/dist",
|
|
|
|
index: "index.html",
|
|
|
|
});
|
|
|
|
} catch {
|
|
|
|
next();
|
|
|
|
}
|
2022-02-21 08:32:36 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
await app.listen({ port: 2222 });
|