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 17:48:40 +09:00
|
|
|
|
2023-01-06 18:24:27 +09:00
|
|
|
const github_markdown = await Deno.readTextFile(
|
|
|
|
join(fromFileUrl(import.meta.url), "..", "static", "github-markdown.css"),
|
|
|
|
);
|
2023-01-05 18:18:07 +09:00
|
|
|
|
|
|
|
const CSSPlugin: Plugin = {
|
2023-01-06 18:24:27 +09:00
|
|
|
name: "css plugin",
|
|
|
|
// deno-lint-ignore require-await
|
|
|
|
render(ctx): PluginRenderResult {
|
|
|
|
ctx.render();
|
|
|
|
return {
|
|
|
|
styles: [{
|
|
|
|
cssText: github_markdown,
|
|
|
|
}],
|
|
|
|
};
|
|
|
|
},
|
2023-01-05 18:18:07 +09:00
|
|
|
};
|
|
|
|
await prepareSecretKey();
|
|
|
|
|
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.")
|
|
|
|
.arguments("[hostname:string]")
|
2023-01-06 18:24:27 +09:00
|
|
|
.action(async ({ debug, port, auth }, hostname) => {
|
|
|
|
hostname ??= "localhost";
|
|
|
|
if (auth) {
|
|
|
|
Deno.env.set("AUTH_REQUIRED", "true");
|
|
|
|
}
|
|
|
|
if (debug) {
|
|
|
|
console.log("Debug mode enabled.");
|
|
|
|
}
|
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
|
|
|
}
|