Proxy API and webapp through NGINX as well

This commit is contained in:
thiloho
2024-08-13 22:14:47 +02:00
parent 6785bd0dfa
commit cf1600671b
18 changed files with 258 additions and 315 deletions

View File

@@ -1,17 +1,14 @@
import type { Actions, PageServerLoad } from "./$types";
export const load: PageServerLoad = async ({ parent, params, cookies, fetch }) => {
const articleData = await fetch(
`http://localhost:${process.env.ARCHTIKA_API_PORT}/article?id=eq.${params.articleId}`,
{
method: "GET",
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${cookies.get("session_token")}`,
Accept: "application/vnd.pgrst.object+json"
}
const articleData = await fetch(`/api/article?id=eq.${params.articleId}`, {
method: "GET",
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${cookies.get("session_token")}`,
Accept: "application/vnd.pgrst.object+json"
}
);
});
const article = await articleData.json();
const { website } = await parent();
@@ -24,21 +21,18 @@ export const actions: Actions = {
const data = await request.formData();
const coverFile = data.get("cover-image") as File;
const uploadedImageData = await fetch(
`http://localhost:${process.env.ARCHTIKA_API_PORT}/rpc/upload_file`,
{
method: "POST",
headers: {
"Content-Type": "application/octet-stream",
Authorization: `Bearer ${cookies.get("session_token")}`,
Accept: "application/vnd.pgrst.object+json",
"X-Website-Id": params.websiteId,
"X-Mimetype": coverFile.type,
"X-Original-Filename": coverFile.name
},
body: await coverFile.arrayBuffer()
}
);
const uploadedImageData = await fetch(`/api/rpc/upload_file`, {
method: "POST",
headers: {
"Content-Type": "application/octet-stream",
Authorization: `Bearer ${cookies.get("session_token")}`,
Accept: "application/vnd.pgrst.object+json",
"X-Website-Id": params.websiteId,
"X-Mimetype": coverFile.type,
"X-Original-Filename": coverFile.name
},
body: await coverFile.arrayBuffer()
});
const uploadedImage = await uploadedImageData.json();
@@ -46,24 +40,21 @@ export const actions: Actions = {
return { success: false, message: uploadedImage.message };
}
const res = await fetch(
`http://localhost:${process.env.ARCHTIKA_API_PORT}/article?id=eq.${params.articleId}`,
{
method: "PATCH",
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${cookies.get("session_token")}`
},
body: JSON.stringify({
title: data.get("title"),
meta_description: data.get("description"),
meta_author: data.get("author"),
cover_image: uploadedImage.file_id,
publication_date: data.get("publication-date"),
main_content: data.get("main-content")
})
}
);
const res = await fetch(`/api/article?id=eq.${params.articleId}`, {
method: "PATCH",
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${cookies.get("session_token")}`
},
body: JSON.stringify({
title: data.get("title"),
meta_description: data.get("description"),
meta_author: data.get("author"),
cover_image: uploadedImage.file_id,
publication_date: data.get("publication-date"),
main_content: data.get("main-content")
})
});
if (!res.ok) {
const response = await res.json();

View File

@@ -68,10 +68,7 @@
</label>
{#if data.article.cover_image}
<Modal id="preview-cover-article-{data.article.id}" text="Preview">
<img
src={`http://localhost:${process.env.ARCHTIKA_API_PORT}/rpc/retrieve_file?id=${data.article.cover_image}`}
alt=""
/>
<img src={`/api/rpc/retrieve_file?id=${data.article.cover_image}`} alt="" />
</Modal>
{/if}
</div>