reverse proxy for uploads
This commit is contained in:
parent
0eb91d67dd
commit
ce50f8a6d6
8 changed files with 44 additions and 48 deletions
2
src/app.d.ts
vendored
2
src/app.d.ts
vendored
|
|
@ -1,6 +1,6 @@
|
||||||
// See https://svelte.dev/docs/kit/types#app.d.ts
|
// 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
|
// for information about these interfaces
|
||||||
declare global {
|
declare global {
|
||||||
|
|
|
||||||
|
|
@ -1,30 +1,22 @@
|
||||||
import { env } from "$env/dynamic/private";
|
import { env } from '$env/dynamic/private';
|
||||||
import { Strapi } from "$lib/server/strapi";
|
import { Strapi } from '$lib/server/strapi';
|
||||||
import type { Handle, HandleServerError } from "@sveltejs/kit";
|
import type { Handle, HandleServerError } from '@sveltejs/kit';
|
||||||
import { randomUUID } from "crypto";
|
import { randomUUID } from 'crypto';
|
||||||
|
|
||||||
export const handleError: HandleServerError = async ({
|
export const handleError: HandleServerError = async ({ error, event, message, status }) => {
|
||||||
error,
|
|
||||||
event,
|
|
||||||
message,
|
|
||||||
status,
|
|
||||||
}) => {
|
|
||||||
const errorId = randomUUID();
|
const errorId = randomUUID();
|
||||||
|
|
||||||
console.error(error, { errorId, event, message, status });
|
console.error(error, { errorId, event, message, status });
|
||||||
|
|
||||||
return {
|
return {
|
||||||
message: "An Internal Error Occurred",
|
message: 'An Internal Error Occurred',
|
||||||
errorId,
|
errorId
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
export const handle: Handle = async ({ event, resolve }) => {
|
export const handle: Handle = async ({ event, resolve }) => {
|
||||||
event.locals = {
|
event.locals = {
|
||||||
strapi: new Strapi(
|
strapi: new Strapi(`http://${env.STRAPI_HOST}:${env.STRAPI_PORT}`, env.STRAPI_TOKEN)
|
||||||
`http://${env.STRAPI_HOST}:${env.STRAPI_PORT}`,
|
|
||||||
env.STRAPI_TOKEN,
|
|
||||||
),
|
|
||||||
};
|
};
|
||||||
|
|
||||||
const response = await resolve(event);
|
const response = await resolve(event);
|
||||||
|
|
|
||||||
|
|
@ -1,31 +1,33 @@
|
||||||
import { strapi, type StrapiClient } from "@strapi/client";
|
import { strapi, type StrapiClient } from '@strapi/client';
|
||||||
|
|
||||||
export class Strapi {
|
export class Strapi {
|
||||||
|
public readonly api: string;
|
||||||
|
public readonly uploads: string;
|
||||||
|
|
||||||
private readonly client: StrapiClient;
|
private readonly client: StrapiClient;
|
||||||
|
|
||||||
constructor(
|
constructor(url: string, auth: string) {
|
||||||
public readonly url: string,
|
this.api = `${url}/api`;
|
||||||
auth: string,
|
this.uploads = `${url}/uploads`;
|
||||||
) {
|
|
||||||
this.client = strapi({
|
this.client = strapi({
|
||||||
baseURL: `${url}/api`,
|
baseURL: this.api,
|
||||||
auth: auth,
|
auth: auth
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
async Blogs() {
|
async Blogs() {
|
||||||
const blogs = this.client.collection("blogs");
|
const blogs = this.client.collection('blogs');
|
||||||
|
|
||||||
return await blogs.find({
|
return await blogs.find({
|
||||||
populate: ["Header", "Topics"],
|
populate: ['Header', 'Topics']
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
async Blog(id: string) {
|
async Blog(id: string) {
|
||||||
const blogs = this.client.collection("blogs");
|
const blogs = this.client.collection('blogs');
|
||||||
|
|
||||||
return await blogs.findOne(id, {
|
return await blogs.findOne(id, {
|
||||||
populate: ["Header", "Topics"],
|
populate: ['Header', 'Topics']
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,5 @@
|
||||||
import { error } from "@sveltejs/kit";
|
import { error } from '@sveltejs/kit';
|
||||||
import type { PageServerLoad } from "./$types";
|
import type { PageServerLoad } from './$types';
|
||||||
|
|
||||||
export const load: PageServerLoad = async ({ locals }) => {
|
export const load: PageServerLoad = async ({ locals }) => {
|
||||||
const { strapi } = locals;
|
const { strapi } = locals;
|
||||||
|
|
@ -12,20 +12,20 @@ export const load: PageServerLoad = async ({ locals }) => {
|
||||||
id: post.documentId,
|
id: post.documentId,
|
||||||
image: {
|
image: {
|
||||||
url: post.Header.formats.medium.url,
|
url: post.Header.formats.medium.url,
|
||||||
alt: post.Header.alternativeText,
|
alt: post.Header.alternativeText
|
||||||
},
|
},
|
||||||
title: post.Title,
|
title: post.Title,
|
||||||
body: post.Body,
|
body: post.Body,
|
||||||
topics: post.Topics.map((topic: { id: string; Topic: string }) => ({
|
topics: post.Topics.map((topic: { id: string; Topic: string }) => ({
|
||||||
id: topic.id,
|
id: topic.id,
|
||||||
name: topic.Topic,
|
name: topic.Topic
|
||||||
})),
|
})),
|
||||||
published: post.publishedAt,
|
published: post.publishedAt
|
||||||
})),
|
})),
|
||||||
meta,
|
meta
|
||||||
};
|
};
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.log("Uh-oh!");
|
console.log('Uh-oh!');
|
||||||
error(500, err.message);
|
error(500, err.message);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,4 @@
|
||||||
<script lang="ts">
|
<script lang="ts">
|
||||||
import { env } from '$env/dynamic/public';
|
|
||||||
import BlogCard from '$lib/components/BlogCard.svelte';
|
import BlogCard from '$lib/components/BlogCard.svelte';
|
||||||
import type { PageProps } from './$types';
|
import type { PageProps } from './$types';
|
||||||
|
|
||||||
|
|
@ -16,7 +15,7 @@
|
||||||
{#each posts as post (post.id)}
|
{#each posts as post (post.id)}
|
||||||
<BlogCard
|
<BlogCard
|
||||||
id={post.id}
|
id={post.id}
|
||||||
img={`${env.PUBLIC_STRAPI_HOST}${post.image.url}`}
|
img={post.image.url}
|
||||||
img-alt={post.image.alt}
|
img-alt={post.image.alt}
|
||||||
title={post.title}
|
title={post.title}
|
||||||
published={post.published}
|
published={post.published}
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,4 @@
|
||||||
import type { PageServerLoad } from "./$types";
|
import type { PageServerLoad } from './$types';
|
||||||
|
|
||||||
export const load: PageServerLoad = async ({ locals, params }) => {
|
export const load: PageServerLoad = async ({ locals, params }) => {
|
||||||
const { slug } = params;
|
const { slug } = params;
|
||||||
|
|
@ -12,16 +12,16 @@ export const load: PageServerLoad = async ({ locals, params }) => {
|
||||||
image: {
|
image: {
|
||||||
url: post.Header.url,
|
url: post.Header.url,
|
||||||
alt: post.Header.alternativeText,
|
alt: post.Header.alternativeText,
|
||||||
caption: post.Header.caption,
|
caption: post.Header.caption
|
||||||
},
|
},
|
||||||
title: post.Title,
|
title: post.Title,
|
||||||
body: post.Body,
|
body: post.Body,
|
||||||
topics: post.Topics.map((topic: { id: string; Topic: string }) => ({
|
topics: post.Topics.map((topic: { id: string; Topic: string }) => ({
|
||||||
id: topic.id,
|
id: topic.id,
|
||||||
name: topic.Topic,
|
name: topic.Topic
|
||||||
})),
|
})),
|
||||||
published: post.publishedAt,
|
published: post.publishedAt
|
||||||
},
|
},
|
||||||
meta,
|
meta
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,4 @@
|
||||||
<script lang="ts">
|
<script lang="ts">
|
||||||
import { env } from '$env/dynamic/public';
|
|
||||||
import dayjs from 'dayjs';
|
import dayjs from 'dayjs';
|
||||||
import { compile } from 'mdsvex';
|
import { compile } from 'mdsvex';
|
||||||
import type { PageProps } from './$types';
|
import type { PageProps } from './$types';
|
||||||
|
|
@ -14,11 +13,7 @@
|
||||||
<p class="text-gray-500">{dayjs(post.published).format('DD MMMM YYYY')}</p>
|
<p class="text-gray-500">{dayjs(post.published).format('DD MMMM YYYY')}</p>
|
||||||
<h1 class="text-4xl font-light">{post.title}</h1>
|
<h1 class="text-4xl font-light">{post.title}</h1>
|
||||||
</div>
|
</div>
|
||||||
<img
|
<img class="aspect-4/2 w-full object-cover" src={post.image.url} alt={post.image.alt} />
|
||||||
class="aspect-4/2 w-full object-cover"
|
|
||||||
src={`${env.PUBLIC_STRAPI_HOST}${post.image.url}`}
|
|
||||||
alt={post.image.alt}
|
|
||||||
/>
|
|
||||||
<div class="w-full max-w-5xl px-4">
|
<div class="w-full max-w-5xl px-4">
|
||||||
{#await compile(post.body)}
|
{#await compile(post.body)}
|
||||||
<div class="skeleton h-48 w-full"></div>
|
<div class="skeleton h-48 w-full"></div>
|
||||||
|
|
|
||||||
8
src/routes/uploads/[slug]/+server.ts
Normal file
8
src/routes/uploads/[slug]/+server.ts
Normal 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;
|
||||||
|
};
|
||||||
Loading…
Add table
Add a link
Reference in a new issue