Skip to content

Commit

Permalink
updating
Browse files Browse the repository at this point in the history
  • Loading branch information
TeaByte committed Jan 4, 2024
1 parent 14c8cf4 commit 4b90ed7
Show file tree
Hide file tree
Showing 10 changed files with 135 additions and 66 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Fresh project
# FreshPaste

Simple pastebin-like made using deno fresh

Expand Down
4 changes: 2 additions & 2 deletions fresh.gen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import * as $_404 from "./routes/_404.tsx";
import * as $_app from "./routes/_app.tsx";
import * as $api from "./routes/api.tsx";
import * as $api_paste_ from "./routes/api/[paste].ts";
import * as $api_new_paste_ from "./routes/api/new/[paste].ts";
import * as $api_new_paste_ from "./routes/api/new.ts";
import * as $api_raw_paste_ from "./routes/api/raw/[paste].ts";
import * as $index from "./routes/index.tsx";
import * as $CopyButton from "./islands/CopyButton.tsx";
Expand All @@ -20,7 +20,7 @@ const manifest = {
"./routes/_app.tsx": $_app,
"./routes/api.tsx": $api,
"./routes/api/[paste].ts": $api_paste_,
"./routes/api/new/[paste].ts": $api_new_paste_,
"./routes/api/new.ts": $api_new_paste_,
"./routes/api/raw/[paste].ts": $api_raw_paste_,
"./routes/index.tsx": $index,
},
Expand Down
2 changes: 1 addition & 1 deletion routes/[paste].tsx
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ export default function Greet(props: PageProps) {
</a>
</div>
</div>
<pre class="rounded h-96"><code class={`language-${props.data.syntax} match-braces line-numbers`}>{props.data.paste.content}</code></pre>
<pre class="rounded h-96"><code class={`language-${props.data.paste.syntax} match-braces line-numbers`}>{props.data.paste.content}</code></pre>
</section>
</>
);
Expand Down
84 changes: 69 additions & 15 deletions routes/api.tsx
Original file line number Diff line number Diff line change
@@ -1,24 +1,78 @@
import { asset } from "$fresh/runtime.ts";
import { Head } from "$fresh/runtime.ts";

export default function Error404() {
export default function API() {
return (
<>
<div class="px-4 py-8 mx-auto">
<div class="max-w-screen-md mx-auto flex flex-col items-center justify-center">
<img
class="my-6"
src="/logo.svg"
width="128"
height="128"
alt="the Fresh logo: a sliced lemon dripping with juice"
/>
<h1 class="text-4xl font-bold">Not made yet</h1>
<p class="my-4">
Come back later to see what we have to offer.
<Head>
<script src={asset("prism.js")}></script>
<link
href={asset("prism.css")}
rel="stylesheet"
>
</link>
</Head>

<section class="mt-8">
<div class="flex flex-col justify-between items-center gap-16">
<div class="flex justify-center items-center flex-col gap-2 w-full">
<div class="flex gap-1 items-center justify-center w-full">
<p class="text-1xl font-bold bg-gray-300 rounded w-fit p-1">
POST
</p>
<input
class="bg-white border-gray-500 border-2 rounded text-center p-1 w-96"
disabled
value="https://clear-cow-12.deno.dev/api/new/"
/>
</div>
<p>Allows you to create a new paste.</p>
<p>
{'{"content":"Hi", "syntax":"plain"}'}
</p>
</div>

<div class="flex justify-center items-center flex-col gap-2 w-full">
<div class="flex gap-1 items-center justify-center w-full">
<p class="text-1xl font-bold bg-gray-300 rounded w-fit p-1">
GET
</p>
<input
class="bg-white border-gray-500 border-2 rounded text-center p-1 w-96"
disabled
value="https://clear-cow-12.deno.dev/api/{id}"
/>
</div>
<p>Allows you to view the full data.</p>
<p>
{"{id} equals id of the paste"}
</p>
</div>

<div class="flex justify-center items-center flex-col gap-2 w-full">
<div class="flex gap-1 items-center justify-center w-full">
<p class="text-1xl font-bold bg-gray-300 rounded w-fit p-1">
GET
</p>
<input
class="bg-white border-gray-500 border-2 rounded text-center p-1 w-96"
disabled
value="https://clear-cow-12.deno.dev/api/raw/{id}"
/>
</div>
<p>Allows you to view the raw content.</p>
<p>
{"{id} equals id of the paste"}
</p>
</div>

<p class=" bg-gray-300 rounded w-fit p-1">
Pastes gets expired after 30 days!
</p>
<a href="/" class="underline">Go back home</a>
</div>
</div>
<div>
</div>
</section>
</>
);
}
35 changes: 19 additions & 16 deletions routes/api/[paste].ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,23 @@
import { FreshContext } from "$fresh/server.ts";
import { Handlers } from "$fresh/server.ts";
import { get } from "../../utils/db.ts";

// @ts-ignore
export const handler = async (_req: Request, _ctx: FreshContext): Response => {
const id = _ctx.params.paste;
const paste = await get(id);
if (!paste) {
return new Response(null, {
status: 404,
});
}
export const handler: Handlers = {
async GET(_, ctx) {
const id = ctx.params.paste;
const paste = await get(id);
if (!paste) {
return new Response(JSON.stringify({ error: "paste not found" }), {
status: 404,
headers: {
"content-type": "application/json",
},
});
}

// @ts-ignore
return new Response(JSON.stringify(paste), {
headers: {
"content-type": "application/json",
},
});
return new Response(JSON.stringify(paste), {
headers: {
"content-type": "application/json",
},
});
},
};
26 changes: 26 additions & 0 deletions routes/api/new.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { Handlers } from "$fresh/server.ts";
import { set } from "../../utils/db.ts";

export const handler: Handlers = {
async POST(req) {
const body = await req.json();
const [content, syntax] = [body.content, body.syntax];
if (!content || !syntax) {
return new Response(
JSON.stringify({ error: "content and syntax are required" }),
{
status: 404,
headers: {
"content-type": "application/json",
},
},
);
}
const id = set(content, syntax);
return new Response(JSON.stringify({ id }), {
headers: {
"content-type": "application/json",
},
});
},
};
17 changes: 0 additions & 17 deletions routes/api/new/[paste].ts

This file was deleted.

26 changes: 13 additions & 13 deletions routes/api/raw/[paste].ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
import { FreshContext } from "$fresh/server.ts";
import { Handlers } from "$fresh/server.ts";
import { get } from "../../../utils/db.ts";

// @ts-ignore
export const handler = async (_req: Request, _ctx: FreshContext): Response => {
const id = _ctx.params.paste;
const paste = await get(id);
if (!paste) {
return new Response(null, {
status: 404,
});
}

// @ts-ignore
return new Response(paste.content);
export const handler: Handlers = {
async GET(_, ctx) {
const id = ctx.params.paste;
const paste = await get(id);
if (!paste) {
return new Response("Paste not found!.", {
status: 404,
});
}
// @ts-ignore
return new Response(paste.content);
},
};
3 changes: 3 additions & 0 deletions routes/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ export default function Subscribe() {
method="post"
>
<textarea
minLength={5}
maxlength={5000}
required
class="px-2 w-full py-1 border-gray-500 bg-white border-2 rounded"
rows={17}
type="text"
Expand Down
2 changes: 1 addition & 1 deletion utils/db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ const kv = await Deno.openKv();

export function set(content: string, syntax: string) {
const id = crypto.randomUUID();
kv.set(["pastes", id], { content, syntax });
kv.set(["pastes", id], { content, syntax }, { expireIn: 2592000000 });
return id;
}

Expand Down

0 comments on commit 4b90ed7

Please sign in to comment.