reverse proxy for uploads
All checks were successful
Deployment / Deploy website (push) Successful in 2m42s
Deployment / Deploy strapi (push) Successful in 2m45s

This commit is contained in:
Benjamin Palko 2025-05-06 15:14:18 -04:00
parent 0eb91d67dd
commit ce50f8a6d6
8 changed files with 44 additions and 48 deletions

2
src/app.d.ts vendored
View file

@ -1,6 +1,6 @@
// See https://svelte.dev/docs/kit/types#app.d.ts
import type { Strapi } from "$lib/server/strapi";
import type { Strapi } from '$lib/server/strapi';
// for information about these interfaces
declare global {

View file

@ -1,30 +1,22 @@
import { env } from "$env/dynamic/private";
import { Strapi } from "$lib/server/strapi";
import type { Handle, HandleServerError } from "@sveltejs/kit";
import { randomUUID } from "crypto";
import { env } from '$env/dynamic/private';
import { Strapi } from '$lib/server/strapi';
import type { Handle, HandleServerError } from '@sveltejs/kit';
import { randomUUID } from 'crypto';
export const handleError: HandleServerError = async ({
error,
event,
message,
status,
}) => {
export const handleError: HandleServerError = async ({ error, event, message, status }) => {
const errorId = randomUUID();
console.error(error, { errorId, event, message, status });
return {
message: "An Internal Error Occurred",
errorId,
message: 'An Internal Error Occurred',
errorId
};
};
export const handle: Handle = async ({ event, resolve }) => {
event.locals = {
strapi: new Strapi(
`http://${env.STRAPI_HOST}:${env.STRAPI_PORT}`,
env.STRAPI_TOKEN,
),
strapi: new Strapi(`http://${env.STRAPI_HOST}:${env.STRAPI_PORT}`, env.STRAPI_TOKEN)
};
const response = await resolve(event);

View file

@ -1,31 +1,33 @@
import { strapi, type StrapiClient } from "@strapi/client";
import { strapi, type StrapiClient } from '@strapi/client';
export class Strapi {
public readonly api: string;
public readonly uploads: string;
private readonly client: StrapiClient;
constructor(
public readonly url: string,
auth: string,
) {
constructor(url: string, auth: string) {
this.api = `${url}/api`;
this.uploads = `${url}/uploads`;
this.client = strapi({
baseURL: `${url}/api`,
auth: auth,
baseURL: this.api,
auth: auth
});
}
async Blogs() {
const blogs = this.client.collection("blogs");
const blogs = this.client.collection('blogs');
return await blogs.find({
populate: ["Header", "Topics"],
populate: ['Header', 'Topics']
});
}
async Blog(id: string) {
const blogs = this.client.collection("blogs");
const blogs = this.client.collection('blogs');
return await blogs.findOne(id, {
populate: ["Header", "Topics"],
populate: ['Header', 'Topics']
});
}
}

View file

@ -1,5 +1,5 @@
import { error } from "@sveltejs/kit";
import type { PageServerLoad } from "./$types";
import { error } from '@sveltejs/kit';
import type { PageServerLoad } from './$types';
export const load: PageServerLoad = async ({ locals }) => {
const { strapi } = locals;
@ -12,20 +12,20 @@ export const load: PageServerLoad = async ({ locals }) => {
id: post.documentId,
image: {
url: post.Header.formats.medium.url,
alt: post.Header.alternativeText,
alt: post.Header.alternativeText
},
title: post.Title,
body: post.Body,
topics: post.Topics.map((topic: { id: string; Topic: string }) => ({
id: topic.id,
name: topic.Topic,
name: topic.Topic
})),
published: post.publishedAt,
published: post.publishedAt
})),
meta,
meta
};
} catch (err) {
console.log("Uh-oh!");
console.log('Uh-oh!');
error(500, err.message);
}
};

View file

@ -1,5 +1,4 @@
<script lang="ts">
import { env } from '$env/dynamic/public';
import BlogCard from '$lib/components/BlogCard.svelte';
import type { PageProps } from './$types';
@ -16,7 +15,7 @@
{#each posts as post (post.id)}
<BlogCard
id={post.id}
img={`${env.PUBLIC_STRAPI_HOST}${post.image.url}`}
img={post.image.url}
img-alt={post.image.alt}
title={post.title}
published={post.published}

View file

@ -1,4 +1,4 @@
import type { PageServerLoad } from "./$types";
import type { PageServerLoad } from './$types';
export const load: PageServerLoad = async ({ locals, params }) => {
const { slug } = params;
@ -12,16 +12,16 @@ export const load: PageServerLoad = async ({ locals, params }) => {
image: {
url: post.Header.url,
alt: post.Header.alternativeText,
caption: post.Header.caption,
caption: post.Header.caption
},
title: post.Title,
body: post.Body,
topics: post.Topics.map((topic: { id: string; Topic: string }) => ({
id: topic.id,
name: topic.Topic,
name: topic.Topic
})),
published: post.publishedAt,
published: post.publishedAt
},
meta,
meta
};
};

View file

@ -1,5 +1,4 @@
<script lang="ts">
import { env } from '$env/dynamic/public';
import dayjs from 'dayjs';
import { compile } from 'mdsvex';
import type { PageProps } from './$types';
@ -14,11 +13,7 @@
<p class="text-gray-500">{dayjs(post.published).format('DD MMMM YYYY')}</p>
<h1 class="text-4xl font-light">{post.title}</h1>
</div>
<img
class="aspect-4/2 w-full object-cover"
src={`${env.PUBLIC_STRAPI_HOST}${post.image.url}`}
alt={post.image.alt}
/>
<img class="aspect-4/2 w-full object-cover" src={post.image.url} alt={post.image.alt} />
<div class="w-full max-w-5xl px-4">
{#await compile(post.body)}
<div class="skeleton h-48 w-full"></div>

View file

@ -0,0 +1,8 @@
import type { RequestHandler } from './$types';
export const GET: RequestHandler = async ({ locals, params }) => {
const { slug } = params;
const response = await fetch(`${locals.strapi.uploads}/${slug}`);
return response;
};