simple-fs-server/main.ts

126 lines
3.5 KiB
TypeScript
Raw Normal View History

2023-01-05 18:18:07 +09:00
/// <reference no-default-lib="true" />
/// <reference lib="dom" />
/// <reference lib="dom.iterable" />
/// <reference lib="dom.asynciterable" />
/// <reference lib="deno.ns" />
2023-01-06 18:24:27 +09:00
import {
Manifest,
Plugin,
PluginRenderResult,
ServerContext,
StartOptions,
} from "$fresh/server.ts";
2023-01-05 18:18:07 +09:00
import manifest from "./fresh.gen.ts";
import twindPlugin from "$fresh/plugins/twind.ts";
import twindConfig from "./twind.config.ts";
import "https://deno.land/std@0.170.0/dotenv/load.ts";
import { Command } from "https://deno.land/x/cliffy@v0.25.6/mod.ts";
import { fromFileUrl, join } from "path/mod.ts";
import { prepareSecretKey } from "./util/secret.ts";
import { serve } from "http/server.ts";
2023-01-06 17:48:40 +09:00
import { user_command } from "./user.ts";
import { key_out_cmd } from "./keyout.ts";
2023-01-06 19:12:08 +09:00
import { prepareDocs } from "./src/store/doc.ts";
2023-01-06 22:57:57 +09:00
import { connectDB } from "./src/user/db.ts";
import * as users from "./src/user/user.ts";
2023-01-06 17:48:40 +09:00
2023-01-06 22:17:45 +09:00
const github_markdown = (await Deno.readTextFile(
2023-01-06 18:24:27 +09:00
join(fromFileUrl(import.meta.url), "..", "static", "github-markdown.css"),
2023-01-06 22:17:45 +09:00
)).replaceAll("\n", "");
2023-01-05 18:18:07 +09:00
const CSSPlugin: Plugin = {
2023-01-06 18:24:27 +09:00
name: "css plugin",
render(ctx): PluginRenderResult {
ctx.render();
return {
styles: [{
cssText: github_markdown,
}],
};
},
2023-01-05 18:18:07 +09:00
};
2023-01-06 18:24:27 +09:00
async function startServer(manifest: Manifest, options: StartOptions = {}) {
const ctx = await ServerContext.fromManifest(manifest, options);
options.port ??= 8000;
if (options.experimentalDenoServe === true) {
// @ts-ignore as `Deno.serve` is still unstable.
await Deno.serve(ctx.handler() as Deno.ServeHandler, options);
} else {
await serve(ctx.handler(), options);
}
2023-01-05 18:18:07 +09:00
}
2023-01-06 18:24:27 +09:00
async function start(
{ port = 8000, hostname = "localhost" }: {
port?: number;
hostname?: string;
} = {},
) {
await startServer(manifest, {
plugins: [twindPlugin(twindConfig), CSSPlugin],
port: port,
hostname: hostname,
});
2023-01-05 18:18:07 +09:00
}
2023-01-06 18:24:27 +09:00
if (import.meta.main) {
const cmd = new Command();
cmd.name("fs-server")
.description(
"A simple file server that supports search, upload, and download.",
)
2023-01-05 18:18:07 +09:00
.version("0.0.1")
.globalOption("-d, --debug", "Enable debug mode.")
.command("start", "Start the server.")
2023-01-06 18:24:27 +09:00
.option("-p, --port <port:number>", "The port to listen on.", {
default: 8000,
})
2023-01-05 18:18:07 +09:00
.option("--auth", "Enable authentication.")
2023-01-06 22:57:57 +09:00
.option("--db-path <path:string>", "The path to the database file.", {
default: ":memory:",
})
2023-01-06 23:22:00 +09:00
.option(
"--id-password <idpassword:string>",
"The password to use. (Not recommended). id:password format.",
)
2023-01-05 18:18:07 +09:00
.arguments("[hostname:string]")
2023-01-06 22:57:57 +09:00
.action(async ({ debug, port, auth, dbPath, idPassword }, hostname) => {
2023-01-06 18:24:27 +09:00
hostname ??= "localhost";
if (auth) {
Deno.env.set("AUTH_REQUIRED", "true");
}
2023-01-06 22:57:57 +09:00
if (dbPath) {
Deno.env.set("DB_PATH", dbPath);
}
if (idPassword) {
Deno.env.set("AUTH_REQUIRED", "true");
2023-01-06 23:22:00 +09:00
const db = await connectDB();
2023-01-06 22:57:57 +09:00
const [username, password] = idPassword.split(":");
const new_user = await users.createUser(username, password);
await users.addUser(db, new_user);
}
2023-01-06 18:24:27 +09:00
if (debug) {
console.log("Debug mode enabled.");
}
2023-01-06 23:22:00 +09:00
await prepareSecretKey();
2023-01-06 19:12:08 +09:00
await prepareDocs();
2023-01-06 18:24:27 +09:00
await start({
port: port,
hostname: hostname,
});
})
2023-01-06 17:48:40 +09:00
.command("user", user_command)
2023-01-06 18:06:10 +09:00
.command("keyout", key_out_cmd);
2023-01-06 18:24:27 +09:00
await cmd.parse(Deno.args);
} else {
2023-01-06 19:12:08 +09:00
await prepareDocs();
2023-01-06 18:24:27 +09:00
await start();
2023-01-05 18:18:07 +09:00
}