Skip to content

Commit add221c

Browse files
committed
refactor!: restructure programmerbar page data loading
1 parent 122f906 commit add221c

7 files changed

Lines changed: 41 additions & 38 deletions

File tree

programmerbar-web/src/hooks.server.ts

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { dev } from '$app/environment';
21
import { createAuth } from '$lib/auth/lucia';
32
import { FeideProvider } from '$lib/auth/feide';
43
import { createDatabase } from '$lib/db/drizzle';
@@ -24,17 +23,17 @@ import { PendingApplicationService } from '$lib/services/pending-application.ser
2423
import { csrf } from '$lib/hooks/csrf';
2524

2625
const main: Handle = async ({ event, resolve }) => {
26+
// Set up primitive services from Cloudflare environment
2727
const STATUS_KV = event.platform!.env.STATUS_KV;
2828
const R2_BUCKET = event.platform!.env.BUCKET;
2929
const DB = event.platform!.env.DB;
3030

3131
const banService = new BanService(STATUS_KV);
3232
event.locals.banService = banService;
3333

34-
const ip = event.getClientAddress();
35-
const isIpBanned = await banService.isIpBanned(ip);
36-
37-
if (isIpBanned) {
34+
// Check if IP is banned
35+
const isBanned = await banService.isBanned(event);
36+
if (isBanned) {
3837
return new Response(null, {
3938
status: 429,
4039
headers: {
@@ -63,7 +62,8 @@ const main: Handle = async ({ event, resolve }) => {
6362
);
6463
event.locals.feideProvider = feideProvider;
6564

66-
// Setup status service
65+
// Setup services
66+
6767
const statusService = new StatusService(event.platform!.env.STATUS_KV);
6868
event.locals.statusService = statusService;
6969

@@ -122,14 +122,14 @@ const main: Handle = async ({ event, resolve }) => {
122122
event.locals.session = null;
123123
}
124124

125+
// Clear cookie if no valid user
125126
if (!event.locals.user) {
126127
event.cookies.delete(auth.sessionCookieName, {
127-
path: '/',
128-
httpOnly: true,
129-
secure: !dev
128+
path: '/'
130129
});
131130
}
132131

132+
// Only board members have access to routes under /portal/admin
133133
if (event.url.pathname.startsWith('/portal/admin') && event.locals.user?.role !== 'board') {
134134
return new Response(null, {
135135
status: 307,
@@ -139,6 +139,7 @@ const main: Handle = async ({ event, resolve }) => {
139139
});
140140
}
141141

142+
// Only authenticated users have access to routes under /portal
142143
if (event.url.pathname.startsWith('/portal') && !event.locals.user) {
143144
return new Response(null, {
144145
status: 307,

programmerbar-web/src/lib/api/sanity/programmerbar.ts

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1-
import { echoSanityClient } from './client';
21
import groq from 'groq';
32

4-
const query = groq`*[_type == "studentGroup"
3+
export const PROGRAMMERBAR_GROUP_QUERY = groq`*[_type == "studentGroup"
54
&& slug.current == $slug
65
&& !(_id in path('drafts.**'))] {
76
_id,
@@ -29,7 +28,7 @@ const query = groq`*[_type == "studentGroup"
2928
}
3029
}[0]`;
3130

32-
type StudentGroup = {
31+
export type StudentGroup = {
3332
_id: string;
3433
_createdAt: string;
3534
_updatedAt: string;
@@ -53,7 +52,3 @@ type StudentGroup = {
5352
email: string;
5453
}>;
5554
};
56-
57-
export const getProgrammerbarGroup = async () => {
58-
return await echoSanityClient.fetch<StudentGroup>(query, { slug: 'programmerbar' });
59-
};

programmerbar-web/src/lib/services/ban.service.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,22 @@
1+
import { building } from '$app/environment';
2+
import type { RequestEvent } from '@sveltejs/kit';
3+
14
export class BanService {
25
#kv: KVNamespace;
36

47
constructor(kv: KVNamespace) {
58
this.#kv = kv;
69
}
710

8-
async banIp(ip: string): Promise<void> {
11+
async ban(event: RequestEvent): Promise<void> {
12+
const ip = event.getClientAddress();
913
await this.#kv.put(`banned:${ip}`, 'banned', { expirationTtl: 60 * 60 * 24 * 7 });
1014
}
1115

12-
async isIpBanned(ip: string): Promise<boolean> {
16+
async isBanned(event: RequestEvent): Promise<boolean> {
17+
if (building) return false;
18+
19+
const ip = event.getClientAddress();
1320
const banned = await this.#kv.get(`banned:${ip}`);
1421
return banned !== null;
1522
}

programmerbar-web/src/routes/(app)/common.remote.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,12 @@ const ContanctSubmissionSchema = z.object({
2121
export const createContactSubmissionAction = form(
2222
ContanctSubmissionSchema,
2323
async ({ name, email, namekjkj, emailkjkj, messagekjkj }) => {
24-
const { locals, getClientAddress } = getRequestEvent();
25-
24+
const event = getRequestEvent();
25+
const { locals, getClientAddress } = event;
2626
const ip = getClientAddress();
2727

2828
if (name || email) {
29-
await locals.banService.banIp(ip);
29+
await locals.banService.ban(event);
3030

3131
return fail(400, { success: false });
3232
}

programmerbar-web/src/routes/(app)/om-oss/+page.svelte

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,11 @@
44
import { marked } from 'marked';
55
import Heading from '$lib/components/ui/Heading.svelte';
66
import SEO from '$lib/components/SEO.svelte';
7+
import { getProgrammerbarGroup } from './data.remote';
78
8-
let { data } = $props();
9+
const programmerbar = await getProgrammerbarGroup();
910
10-
const html = marked.parse(data.programmerbar.description);
11+
const html = marked.parse(programmerbar.description);
1112
</script>
1213

1314
<SEO
@@ -18,16 +19,16 @@
1819
type="website"
1920
/>
2021

21-
<div class="bg-background mx-auto w-full max-w-screen-lg rounded-xl border-2 p-8 shadow-lg">
22+
<div class="bg-background mx-auto w-full max-w-5xl rounded-xl border-2 p-8 shadow-lg">
2223
<Heading class="mb-4 text-4xl">Om oss</Heading>
2324

2425
<div class="mb-16">
2526
<div
2627
class="mx-auto mb-4 flex h-72 w-full items-center justify-center gap-4 rounded-xl border-2 bg-gray-100 sm:float-right sm:ml-4 md:w-96"
2728
>
28-
{#if data.programmerbar.image}
29+
{#if programmerbar.image}
2930
<img
30-
src={echoUrlFor(data.programmerbar.image).url()}
31+
src={echoUrlFor(programmerbar.image).url()}
3132
alt="Programmerbar"
3233
class="h-full w-full rounded-xl object-cover"
3334
/>
@@ -39,7 +40,7 @@
3940
{/if}
4041
</div>
4142

42-
<article class="prose prose-xl text-xl break-words">
43+
<article class="prose prose-xl wrap-break-words text-xl">
4344
{@html html}
4445
</article>
4546
</div>
@@ -48,7 +49,7 @@
4849
<Heading class="text-3xl" level={2}>Styremedlemmer</Heading>
4950

5051
<ul class="mt-4 grid grid-cols-1 gap-y-10 sm:grid-cols-2 md:grid-cols-3">
51-
{#each data.programmerbar.members as member (member.profile._id)}
52+
{#each programmerbar.members as member (member.profile._id)}
5253
<li class="p-1 text-center">
5354
<div class="mb-3 flex items-center justify-center">
5455
{#if member.profile.picture}

programmerbar-web/src/routes/(app)/om-oss/+page.ts

Lines changed: 0 additions & 10 deletions
This file was deleted.
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { prerender } from '$app/server';
2+
import { echoSanityClient } from '$lib/api/sanity/client';
3+
import { PROGRAMMERBAR_GROUP_QUERY, type StudentGroup } from '$lib/api/sanity/programmerbar';
4+
5+
export const getProgrammerbarGroup = prerender(async () => {
6+
return await echoSanityClient.fetch<StudentGroup>(PROGRAMMERBAR_GROUP_QUERY, {
7+
slug: 'programmerbar'
8+
});
9+
});

0 commit comments

Comments
 (0)