This commit is contained in:
monoid 2023-01-14 03:03:58 +09:00
parent e2ee2c6375
commit 21111549b6
16 changed files with 124 additions and 110 deletions

View File

@ -12,18 +12,23 @@ export function MarkdownRenderer(props: { text: string | undefined }) {
meta = text.slice(4, index);
c = text.slice(index + 4, text.length);
}
return (<>
{meta ? <div>
<h2>Meta</h2>
<pre>{meta}</pre>
<hr class="mt-2 mb-2"></hr>
</div> : <div></div>}
<div
class="markdown-body"
dangerouslySetInnerHTML={{ __html: marked.parse(c) }}
return (
<>
{meta
? (
<div>
<h2>Meta</h2>
<pre>{meta}</pre>
<hr class="mt-2 mb-2"></hr>
</div>
)
: <div></div>}
<div
class="markdown-body"
dangerouslySetInnerHTML={{ __html: marked.parse(c) }}
>
</div>
</>
</div>
</>
);
}

14
dev.ts
View File

@ -8,11 +8,11 @@ await devUserAdd();
await dev(import.meta.url, "./main.ts");
async function devUserAdd() {
if(Deno.env.get("DB_PATH") === ":memory:") {
const db = await connectDB();
const username = "admin";
const password = "admin";
const new_user = await users.createUser(username, password);
await users.addUser(db, new_user);
}
if (Deno.env.get("DB_PATH") === ":memory:") {
const db = await connectDB();
const username = "admin";
const password = "admin";
const new_user = await users.createUser(username, password);
await users.addUser(db, new_user);
}
}

View File

@ -50,7 +50,7 @@ function FetchAndRender(props: { src: string; type: string }) {
}
}
export function RenderView(props: { src: string, mdbase?: string }) {
export function RenderView(props: { src: string; mdbase?: string }) {
const src = props.src;
const type = extToType(extname(src));
switch (type) {

View File

@ -61,7 +61,9 @@ export function DirList(props: DirListProps) {
{files.map((file) => (
<ListItem
key={file.name}
href={`/dir/${encodePath(join(data.path, file.name))}${(file.isDirectory ? "/" : "")}?pretty`}
href={`/dir/${
encodePath(join(data.path, file.name))
}${(file.isDirectory ? "/" : "")}?pretty`}
icon={file.isDirectory
? "/icon/folder.svg"
: extToIcon(extname(file.name))}

View File

@ -1,52 +1,51 @@
export default function LoginForm({
redirect = "/",
failed = false,
}: { redirect?: string
failed?: boolean }
) {
return <div class="p-4 absolute top-[50%] left-[50%] translate-x-[-50%] translate-y-[-50%]
redirect = "/",
failed = false,
}: { redirect?: string; failed?: boolean }) {
return (
<div class="p-4 absolute top-[50%] left-[50%] translate-x-[-50%] translate-y-[-50%]
flex flex-col items-center border-gray-500 border-2 rounded-md
sm:max-w-screen-sm max-w-screen-md">
<img
src="/logo.svg"
class="w-32 h-32"
alt="the fresh logo: a sliced lemon dripping with juice"
/>
<h1 class="text-2xl font-bold">Login</h1>
{failed ? <p class="text-red-500">Login failed</p> : null}
<form
action={"/login?redirect=" + redirect}
method="POST"
class="flex flex-col gap-2 items-stretch"
>
<div class="flex gap-2 flex-wrap">
<div class="basis-40 flex items-center flex-1">
<label for="username" class="w-20">Username</label>
<input
type="text"
name="username"
id="username"
class="border-b-2 focus:border-green-500 transition-colors flex-1"
/>
</div>
<div class="flex items-center flex-1">
<label for="password" class="w-20">Password</label>
<input
type="password"
name="password"
id="password"
class="border-b-2 focus:border-green-500 transition-colors flex-1"
/>
</div>
</div>
<button
type="submit"
class="bg-gray-400 p-2 rounded
<img
src="/logo.svg"
class="w-32 h-32"
alt="the fresh logo: a sliced lemon dripping with juice"
/>
<h1 class="text-2xl font-bold">Login</h1>
{failed ? <p class="text-red-500">Login failed</p> : null}
<form
action={"/login?redirect=" + redirect}
method="POST"
class="flex flex-col gap-2 items-stretch"
>
<div class="flex gap-2 flex-wrap">
<div class="basis-40 flex items-center flex-1">
<label for="username" class="w-20">Username</label>
<input
type="text"
name="username"
id="username"
class="border-b-2 focus:border-green-500 transition-colors flex-1"
/>
</div>
<div class="flex items-center flex-1">
<label for="password" class="w-20">Password</label>
<input
type="password"
name="password"
id="password"
class="border-b-2 focus:border-green-500 transition-colors flex-1"
/>
</div>
</div>
<button
type="submit"
class="bg-gray-400 p-2 rounded
m-auto"
>
Login
</button>
</form>
>
Login
</button>
</form>
</div>
);
}

View File

@ -5,7 +5,7 @@ import { encodePath } from "../util/util.ts";
function stairs(path: string) {
if (path === ".") return [];
const uplist = path.split("/").filter(x=> x.length > 0);
const uplist = path.split("/").filter((x) => x.length > 0);
let current = ".";
const stairs = [];
for (const up of uplist) {

View File

@ -4,11 +4,7 @@
/// <reference lib="dom.asynciterable" />
/// <reference lib="deno.ns" />
import {
Manifest,
ServerContext,
StartOptions,
} from "$fresh/server.ts";
import { Manifest, ServerContext, StartOptions } from "$fresh/server.ts";
import manifest from "./fresh.gen.ts";
import twindPlugin from "$fresh/plugins/twind.ts";
@ -25,7 +21,6 @@ import { prepareDocs } from "./src/store/doc.ts";
import { connectDB } from "./src/user/db.ts";
import * as users from "./src/user/user.ts";
async function startServer(manifest: Manifest, options: StartOptions = {}) {
const ctx = await ServerContext.fromManifest(manifest, options);

View File

@ -1,4 +1,8 @@
import { HandlerContext, MiddlewareHandlerContext, Status } from "$fresh/server.ts";
import {
HandlerContext,
MiddlewareHandlerContext,
Status,
} from "$fresh/server.ts";
import { getCookies } from "http/cookie.ts";
import { verify } from "djwt";
import { prepareSecretKey } from "../util/secret.ts";
@ -17,4 +21,4 @@ export const handler = async (
ctx.state["login"] = null;
}
return await ctx.next();
}
};

View File

@ -28,9 +28,13 @@ export type DirOrFileProps = DirProps | FileProps;
type RenderOption = {
fileInfo?: Deno.FileInfo;
}
};
async function renderFile(req: Request, path: string, { fileInfo }: RenderOption = {}) {
async function renderFile(
req: Request,
path: string,
{ fileInfo }: RenderOption = {},
) {
try {
if (!fileInfo) {
fileInfo = await Deno.stat(path);
@ -82,7 +86,12 @@ async function renderFile(req: Request, path: string, { fileInfo }: RenderOption
}
}
async function renderPage(_req: Request, path: string, ctx: HandlerContext, { fileInfo }: RenderOption = {}) {
async function renderPage(
_req: Request,
path: string,
ctx: HandlerContext,
{ fileInfo }: RenderOption = {},
) {
try {
if (!fileInfo) {
fileInfo = await Deno.stat(path);

View File

@ -5,7 +5,6 @@ export default function Home() {
<>
<Head>
<title>Simple file server</title>
</Head>
<div class="p-4 mx-auto max-w-screen-md">
<img

View File

@ -8,7 +8,7 @@ import { create as createJWT } from "djwt";
import { prepareSecretKey } from "../util/secret.ts";
import LoginForm from "../islands/Login.tsx";
async function GET(_req: Request, ctx: HandlerContext){
async function GET(_req: Request, ctx: HandlerContext) {
return await ctx.render();
}
@ -58,7 +58,9 @@ async function POST(req: Request, _ctx: HandlerContext): Promise<Response> {
<h1> Login Failed </h1>
<p> <a href="/"> Back to Home </a> </p>
<script>
document.location.href = "/login?failed=true&redirect=${url.searchParams.get("redirect")}";
document.location.href = "/login?failed=true&redirect=${
url.searchParams.get("redirect")
}";
</script>
</body>
</html>`,
@ -85,7 +87,7 @@ export default function Login(props: PageProps) {
<title>Simple file server - Login</title>
</Head>
<div class="">
<LoginForm redirect={redirect ?? "/"} failed={failed}/>
<LoginForm redirect={redirect ?? "/"} failed={failed} />
</div>
</>
);

View File

@ -1,20 +1,20 @@
import { MiddlewareHandlerContext, Status } from "$fresh/server.ts";
export const handler = async (
req: Request,
ctx: MiddlewareHandlerContext<Record<string, unknown>>,
) => {
const authRequired = Deno.env.get("AUTH_REQUIRED") === "true";
if (authRequired) {
const login = ctx.state["login"];
if (!login) {
return new Response(null, {
status: Status.Found,
headers: {
Location: `/login?redirect=${encodeURIComponent(req.url)}`,
}
});
}
req: Request,
ctx: MiddlewareHandlerContext<Record<string, unknown>>,
) => {
const authRequired = Deno.env.get("AUTH_REQUIRED") === "true";
if (authRequired) {
const login = ctx.state["login"];
if (!login) {
return new Response(null, {
status: Status.Found,
headers: {
Location: `/login?redirect=${encodeURIComponent(req.url)}`,
},
});
}
return await ctx.next();
}
}
return await ctx.next();
};

View File

@ -11,8 +11,7 @@ tags: ["한글", "테스트"]
2. asdf
3. sdf
* a
* b
* c
* d
- a
- b
- c
- d