commit 60133a4a084eec16ca411efa2a50822fb7136be5 Author: JOLIMAITRE Matthieu Date: Tue May 21 18:49:16 2024 +0200 init diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..2ef4a1f --- /dev/null +++ b/.gitignore @@ -0,0 +1,12 @@ +# dotenv environment variable files +.env +.env.development.local +.env.test.local +.env.production.local +.env.local + +# Fresh build directory +_fresh/ +# npm dependencies +node_modules/ +/local \ No newline at end of file diff --git a/.vscode/extensions.json b/.vscode/extensions.json new file mode 100644 index 0000000..09cf720 --- /dev/null +++ b/.vscode/extensions.json @@ -0,0 +1,5 @@ +{ + "recommendations": [ + "denoland.vscode-deno" + ] +} diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..bf6ade0 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,18 @@ +{ + "deno.enable": true, + "deno.lint": true, + "deno.unstable": true, + "editor.defaultFormatter": "denoland.vscode-deno", + "[typescriptreact]": { + "editor.defaultFormatter": "denoland.vscode-deno" + }, + "[typescript]": { + "editor.defaultFormatter": "denoland.vscode-deno" + }, + "[javascriptreact]": { + "editor.defaultFormatter": "denoland.vscode-deno" + }, + "[javascript]": { + "editor.defaultFormatter": "denoland.vscode-deno" + } +} diff --git a/README.md b/README.md new file mode 100644 index 0000000..ec0e33e --- /dev/null +++ b/README.md @@ -0,0 +1,16 @@ +# Fresh project + +Your new Fresh project is ready to go. You can follow the Fresh "Getting +Started" guide here: https://fresh.deno.dev/docs/getting-started + +### Usage + +Make sure to install Deno: https://deno.land/manual/getting_started/installation + +Then start the project: + +``` +deno task start +``` + +This will watch the project directory and restart as necessary. diff --git a/components/Button.tsx b/components/Button.tsx new file mode 100644 index 0000000..f1b80a0 --- /dev/null +++ b/components/Button.tsx @@ -0,0 +1,12 @@ +import { JSX } from "preact"; +import { IS_BROWSER } from "$fresh/runtime.ts"; + +export function Button(props: JSX.HTMLAttributes) { + return ( + +

{props.count}

+ + + ); +} diff --git a/main.ts b/main.ts new file mode 100644 index 0000000..02ea409 --- /dev/null +++ b/main.ts @@ -0,0 +1,14 @@ +/// +/// +/// +/// +/// +/// + +import "$std/dotenv/load.ts"; + +import { start } from "$fresh/server.ts"; +import manifest from "./fresh.gen.ts"; +import config from "./fresh.config.ts"; + +await start(manifest, config); diff --git a/routes/_404.tsx b/routes/_404.tsx new file mode 100644 index 0000000..c63ae2e --- /dev/null +++ b/routes/_404.tsx @@ -0,0 +1,27 @@ +import { Head } from "$fresh/runtime.ts"; + +export default function Error404() { + return ( + <> + + 404 - Page not found + +
+
+ the Fresh logo: a sliced lemon dripping with juice +

404 - Page not found

+

+ The page you were looking for doesn't exist. +

+ Go back home +
+
+ + ); +} diff --git a/routes/_app.tsx b/routes/_app.tsx new file mode 100644 index 0000000..c280e2b --- /dev/null +++ b/routes/_app.tsx @@ -0,0 +1,16 @@ +import { type PageProps } from "$fresh/server.ts"; +export default function App({ Component }: PageProps) { + return ( + + + + + twifeur + + + + + + + ); +} diff --git a/routes/api/joke.ts b/routes/api/joke.ts new file mode 100644 index 0000000..db17edd --- /dev/null +++ b/routes/api/joke.ts @@ -0,0 +1,21 @@ +import { FreshContext } from "$fresh/server.ts"; + +// Jokes courtesy of https://punsandoneliners.com/randomness/programmer-jokes/ +const JOKES = [ + "Why do Java developers often wear glasses? They can't C#.", + "A SQL query walks into a bar, goes up to two tables and says “can I join you?”", + "Wasn't hard to crack Forrest Gump's password. 1forrest1.", + "I love pressing the F5 key. It's refreshing.", + "Called IT support and a chap from Australia came to fix my network connection. I asked “Do you come from a LAN down under?”", + "There are 10 types of people in the world. Those who understand binary and those who don't.", + "Why are assembly programmers often wet? They work below C level.", + "My favourite computer based band is the Black IPs.", + "What programme do you use to predict the music tastes of former US presidential candidates? An Al Gore Rhythm.", + "An SEO expert walked into a bar, pub, inn, tavern, hostelry, public house.", +]; + +export const handler = (_req: Request, _ctx: FreshContext): Response => { + const randomIndex = Math.floor(Math.random() * JOKES.length); + const body = JOKES[randomIndex]; + return new Response(body); +}; diff --git a/routes/greet/[name].tsx b/routes/greet/[name].tsx new file mode 100644 index 0000000..9c06827 --- /dev/null +++ b/routes/greet/[name].tsx @@ -0,0 +1,5 @@ +import { PageProps } from "$fresh/server.ts"; + +export default function Greet(props: PageProps) { + return
Hello {props.params.name}
; +} diff --git a/routes/index.tsx b/routes/index.tsx new file mode 100644 index 0000000..c5990aa --- /dev/null +++ b/routes/index.tsx @@ -0,0 +1,39 @@ +import { useSignal } from "@preact/signals"; +import Counter from "../islands/Counter.tsx"; +import { Handlers } from "$fresh/server.ts"; +import { getCookies } from "$std/http/cookie.ts"; +import { User } from "../storage/models/User.ts"; + +interface Data { + user: User; +} + +export const handler: Handlers = { + GET(req, ctx) { + const cookies = getCookies(req.headers); + return ctx.render!({ allowed: cookies.auth === "bar" }); + }, +}; + +export default function Home() { + const count = useSignal(3); + return ( +
+
+ the Fresh logo: a sliced lemon dripping with juice +

Welcome to Fresh

+

+ Try updating this message in the + ./routes/index.tsx file, and refresh. +

+ +
+
+ ); +} diff --git a/static/favicon.ico b/static/favicon.ico new file mode 100644 index 0000000..1cfaaa2 Binary files /dev/null and b/static/favicon.ico differ diff --git a/static/logo.svg b/static/logo.svg new file mode 100644 index 0000000..ef2fbe4 --- /dev/null +++ b/static/logo.svg @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/static/styles.css b/static/styles.css new file mode 100644 index 0000000..e94132d --- /dev/null +++ b/static/styles.css @@ -0,0 +1,129 @@ + +*, +*::before, +*::after { + box-sizing: border-box; +} +* { + margin: 0; +} +button { + color: inherit; +} +button, [role="button"] { + cursor: pointer; +} +code { + font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, + "Liberation Mono", "Courier New", monospace; + font-size: 1em; +} +img, +svg { + display: block; +} +img, +video { + max-width: 100%; + height: auto; +} + +html { + line-height: 1.5; + -webkit-text-size-adjust: 100%; + font-family: ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, + "Segoe UI", Roboto, "Helvetica Neue", Arial, "Noto Sans", sans-serif, + "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol", "Noto Color Emoji"; +} +.transition-colors { + transition-property: background-color, border-color, color, fill, stroke; + transition-timing-function: cubic-bezier(0.4, 0, 0.2, 1); + transition-duration: 150ms; +} +.my-6 { + margin-bottom: 1.5rem; + margin-top: 1.5rem; +} +.text-4xl { + font-size: 2.25rem; + line-height: 2.5rem; +} +.mx-2 { + margin-left: 0.5rem; + margin-right: 0.5rem; +} +.my-4 { + margin-bottom: 1rem; + margin-top: 1rem; +} +.mx-auto { + margin-left: auto; + margin-right: auto; +} +.px-4 { + padding-left: 1rem; + padding-right: 1rem; +} +.py-8 { + padding-bottom: 2rem; + padding-top: 2rem; +} +.bg-\[\#86efac\] { + background-color: #86efac; +} +.text-3xl { + font-size: 1.875rem; + line-height: 2.25rem; +} +.py-6 { + padding-bottom: 1.5rem; + padding-top: 1.5rem; +} +.px-2 { + padding-left: 0.5rem; + padding-right: 0.5rem; +} +.py-1 { + padding-bottom: 0.25rem; + padding-top: 0.25rem; +} +.border-gray-500 { + border-color: #6b7280; +} +.bg-white { + background-color: #fff; +} +.flex { + display: flex; +} +.gap-8 { + grid-gap: 2rem; + gap: 2rem; +} +.font-bold { + font-weight: 700; +} +.max-w-screen-md { + max-width: 768px; +} +.flex-col { + flex-direction: column; +} +.items-center { + align-items: center; +} +.justify-center { + justify-content: center; +} +.border-2 { + border-width: 2px; +} +.rounded { + border-radius: 0.25rem; +} +.hover\:bg-gray-200:hover { + background-color: #e5e7eb; +} +.tabular-nums { + font-variant-numeric: tabular-nums; +} diff --git a/storage/models/User.ts b/storage/models/User.ts new file mode 100644 index 0000000..8859b48 --- /dev/null +++ b/storage/models/User.ts @@ -0,0 +1,9 @@ +import { z } from "https://deno.land/x/zod@v3.23.8/mod.ts"; + +export const UserModel = z.object({ + id: z.string().uuid().describe("primary"), + name: z.string(), + email: z.string(), + password: z.string(), + pfp_url: z.string(), +}); diff --git a/storage/store.ts b/storage/store.ts new file mode 100644 index 0000000..aaddf15 --- /dev/null +++ b/storage/store.ts @@ -0,0 +1,58 @@ +import { createPentagon } from "https://deno.land/x/pentagon@v0.1.5/mod.ts"; +import { project_root_dir } from "../utils.ts"; +import { z } from "https://deno.land/x/zod@v3.21.4/mod.ts"; +import mimeDbV1520 from "$std/media_types/vendor/mime-db.v1.52.0.ts"; + +const kv = await Deno.openKv(project_root_dir() + "local/kv"); + +export type User = z.infer; +export const user_model = z.object({ + id: z.string().uuid().describe("primary"), + name: z.string(), + email: z.string(), + password: z.string(), + pfp_url: z.string(), + like_set: z.set(z.string().uuid()), +}); + +export type Post = z.infer; +export const post_model = z.object({ + id: z.string().uuid().describe("primary"), + title: z.string(), + content: z.string(), + date: z.number(), + like_count: z.number(), + author_id: z.string().uuid(), +}); + +export type Comment = z.infer; +export const comment_model = z.object({ + id: z.string().uuid().describe("primary"), + date: z.number(), + post_id: z.string().uuid(), + author_id: z.string().uuid(), +}); + +export const db = createPentagon(kv, { + users: { + schema: user_model, + relations: { + posts: ["posts", [post_model], "id", "author_id"], + comments: ["comments", [comment_model], "id", "author_id"], + }, + }, + posts: { + schema: post_model, + relations: { + author: ["users", user_model, "author_id", "id"], + comments: ["comments", [comment_model], "id", "post_id"], + }, + }, + comments: { + schema: comment_model, + relations: { + author: ["users", user_model, "author_id", "id"], + post: ["post", post_model, "post_id", "id"], + }, + }, +}); diff --git a/utils.ts b/utils.ts new file mode 100644 index 0000000..749ad4d --- /dev/null +++ b/utils.ts @@ -0,0 +1,7 @@ +import { dirname } from "$std/path/dirname.ts"; + +export function project_root_dir() { + const this_url = new URL(import.meta.url); + const this_dir = dirname(this_url.pathname); + return this_dir + "/"; +}