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

@@ -67,7 +67,7 @@
web = { web = {
type = "app"; type = "app";
program = "${pkgs.writeShellScriptBin "web-wrapper" '' program = "${pkgs.writeShellScriptBin "web-wrapper" ''
ORIGIN=http://localhost:4000 HOST=127.0.0.1 PORT=4000 ARCHTIKA_API_PORT=3000 ARCHTIKA_NGINX_PORT=18000 ${pkgs.nodejs_22}/bin/node ${ ORIGIN=http://localhost:4000 HOST=127.0.0.1 PORT=4000 ${pkgs.nodejs_22}/bin/node ${
self.packages.${system}.default self.packages.${system}.default
}/web-app }/web-app
''}/bin/web-wrapper"; ''}/bin/web-wrapper";

View File

@@ -27,8 +27,8 @@
networkmanager.enable = true; networkmanager.enable = true;
firewall = { firewall = {
allowedTCPPorts = [ allowedTCPPorts = [
10000 80
15000 443
]; ];
}; };
}; };

View File

@@ -39,7 +39,7 @@
} }
{ {
from = "host"; from = "host";
host.port = 18000; host.port = 80;
guest.port = 80; guest.port = 80;
} }
]; ];

View File

@@ -50,12 +50,6 @@ in
default = 10000; default = 10000;
description = "Port on which the web application runs."; description = "Port on which the web application runs.";
}; };
nginxPort = mkOption {
type = types.port;
default = 15000;
description = "Port on which NGINX runs.";
};
}; };
config = mkIf cfg.enable { config = mkIf cfg.enable {
@@ -110,7 +104,7 @@ in
}; };
script = '' script = ''
ORIGIN=http://localhost:${toString cfg.webAppPort} PORT=${toString cfg.webAppPort} ARCHTIKA_API_PORT=${toString cfg.port} ARCHTIKA_NGINX_PORT=${toString cfg.nginxPort} ${pkgs.nodejs_22}/bin/node ${cfg.package}/web-app ORIGIN=https://demo.archtika.com PORT=${toString cfg.webAppPort} ${pkgs.nodejs_22}/bin/node ${cfg.package}/web-app
''; '';
}; };
@@ -134,24 +128,40 @@ in
recommendedProxySettings = true; recommendedProxySettings = true;
recommendedTlsSettings = true; recommendedTlsSettings = true;
virtualHosts."archtika" = { virtualHosts = {
listen = [ "demo.archtika.com" = {
{ enableACME = true;
addr = "0.0.0.0"; forceSSL = true;
port = cfg.nginxPort; locations = {
} "/" = {
]; proxyPass = "http://localhost:${toString cfg.webAppPort}";
locations = { };
"/" = { "/user-websites/" = {
root = "/var/www/archtika-websites"; alias = "/var/www/archtika-websites/";
index = "index.html"; index = "index.html";
tryFiles = "$uri $uri/ $uri/index.html =404"; tryFiles = "$uri $uri/ $uri/index.html =404";
extraConfig = '' extraConfig = ''
autoindex on; autoindex on;
''; '';
};
"/api/" = {
proxyPass = "http://localhost:${toString cfg.port}/";
extraConfig = ''
default_type application/json;
proxy_hide_header Content-Location;
add_header Content-Location /api/$upstream_http_content_location;
proxy_set_header Connection "";
proxy_http_version 1.1;
'';
};
}; };
}; };
}; };
}; };
security.acme = {
acceptTerms = true;
defaults.email = "thilo.hohlt@tutanota.com";
};
}; };
} }

View File

@@ -3,9 +3,9 @@
"version": "0.0.1", "version": "0.0.1",
"private": true, "private": true,
"scripts": { "scripts": {
"dev": "ARCHTIKA_API_PORT=3000 ARCHTIKA_NGINX_PORT=18000 vite dev", "dev": "vite dev",
"build": "vite build", "build": "vite build",
"preview": "ARCHTIKA_API_PORT=3000 ARCHTIKA_NGINX_PORT=18000 vite preview", "preview": "vite preview",
"check": "svelte-kit sync && svelte-check --tsconfig ./tsconfig.json", "check": "svelte-kit sync && svelte-check --tsconfig ./tsconfig.json",
"check:watch": "svelte-kit sync && svelte-check --tsconfig ./tsconfig.json --watch", "check:watch": "svelte-kit sync && svelte-check --tsconfig ./tsconfig.json --watch",
"lint": "prettier --check .", "lint": "prettier --check .",

View File

@@ -1,27 +1,29 @@
import { redirect } from "@sveltejs/kit"; import { redirect } from "@sveltejs/kit";
export const handle = async ({ event, resolve }) => { export const handle = async ({ event, resolve }) => {
const userData = await event.fetch(`http://localhost:${process.env.ARCHTIKA_API_PORT}/account`, { if (!event.url.pathname.startsWith("/api/")) {
method: "GET", const userData = await event.fetch(`/api/account`, {
headers: { method: "GET",
"Content-Type": "application/json", headers: {
Authorization: `Bearer ${event.cookies.get("session_token")}`, "Content-Type": "application/json",
Accept: "application/vnd.pgrst.object+json" Authorization: `Bearer ${event.cookies.get("session_token")}`,
} Accept: "application/vnd.pgrst.object+json"
}); }
});
if (!userData.ok && !["/login", "/register"].includes(event.url.pathname)) { if (!userData.ok && !["/login", "/register"].includes(event.url.pathname)) {
throw redirect(303, "/login"); throw redirect(303, "/login");
}
if (userData.ok) {
if (["/login", "/register"].includes(event.url.pathname)) {
throw redirect(303, "/");
} }
const user = await userData.json(); if (userData.ok) {
if (["/login", "/register"].includes(event.url.pathname)) {
throw redirect(303, "/");
}
event.locals.user = user; const user = await userData.json();
event.locals.user = user;
}
} }
return await resolve(event); return await resolve(event);

View File

@@ -4,7 +4,7 @@ export const actions: Actions = {
default: async ({ request, cookies, fetch }) => { default: async ({ request, cookies, fetch }) => {
const data = await request.formData(); const data = await request.formData();
const res = await fetch(`http://localhost:${process.env.ARCHTIKA_API_PORT}/rpc/login`, { const res = await fetch(`/api/rpc/login`, {
method: "POST", method: "POST",
headers: { "Content-Type": "application/json" }, headers: { "Content-Type": "application/json" },
body: JSON.stringify({ body: JSON.stringify({

View File

@@ -4,7 +4,7 @@ export const actions: Actions = {
default: async ({ request, fetch }) => { default: async ({ request, fetch }) => {
const data = await request.formData(); const data = await request.formData();
const res = await fetch(`http://localhost:${process.env.ARCHTIKA_API_PORT}/rpc/register`, { const res = await fetch(`/api/rpc/register`, {
method: "POST", method: "POST",
headers: { "Content-Type": "application/json" }, headers: { "Content-Type": "application/json" },
body: JSON.stringify({ body: JSON.stringify({

View File

@@ -6,7 +6,7 @@ export const load: PageServerLoad = async ({ fetch, cookies, url }) => {
const params = new URLSearchParams(); const params = new URLSearchParams();
const baseFetchUrl = `http://localhost:${process.env.ARCHTIKA_API_PORT}/website`; const baseFetchUrl = `/api/website`;
if (searchQuery) { if (searchQuery) {
params.append("title", `ilike.*${searchQuery}*`); params.append("title", `ilike.*${searchQuery}*`);
@@ -63,20 +63,17 @@ export const actions: Actions = {
createWebsite: async ({ request, fetch, cookies }) => { createWebsite: async ({ request, fetch, cookies }) => {
const data = await request.formData(); const data = await request.formData();
const res = await fetch( const res = await fetch(`/api/rpc/create_website`, {
`http://localhost:${process.env.ARCHTIKA_API_PORT}/rpc/create_website`, method: "POST",
{ headers: {
method: "POST", "Content-Type": "application/json",
headers: { Authorization: `Bearer ${cookies.get("session_token")}`
"Content-Type": "application/json", },
Authorization: `Bearer ${cookies.get("session_token")}` body: JSON.stringify({
}, content_type: data.get("content-type"),
body: JSON.stringify({ title: data.get("title")
content_type: data.get("content-type"), })
title: data.get("title") });
})
}
);
if (!res.ok) { if (!res.ok) {
const response = await res.json(); const response = await res.json();
@@ -88,19 +85,16 @@ export const actions: Actions = {
updateWebsite: async ({ request, cookies, fetch }) => { updateWebsite: async ({ request, cookies, fetch }) => {
const data = await request.formData(); const data = await request.formData();
const res = await fetch( const res = await fetch(`/api/website?id=eq.${data.get("id")}`, {
`http://localhost:${process.env.ARCHTIKA_API_PORT}/website?id=eq.${data.get("id")}`, method: "PATCH",
{ headers: {
method: "PATCH", "Content-Type": "application/json",
headers: { Authorization: `Bearer ${cookies.get("session_token")}`
"Content-Type": "application/json", },
Authorization: `Bearer ${cookies.get("session_token")}` body: JSON.stringify({
}, title: data.get("title")
body: JSON.stringify({ })
title: data.get("title") });
})
}
);
if (!res.ok) { if (!res.ok) {
const response = await res.json(); const response = await res.json();
@@ -112,16 +106,13 @@ export const actions: Actions = {
deleteWebsite: async ({ request, cookies, fetch }) => { deleteWebsite: async ({ request, cookies, fetch }) => {
const data = await request.formData(); const data = await request.formData();
const res = await fetch( const res = await fetch(`/api/website?id=eq.${data.get("id")}`, {
`http://localhost:${process.env.ARCHTIKA_API_PORT}/website?id=eq.${data.get("id")}`, method: "DELETE",
{ headers: {
method: "DELETE", "Content-Type": "application/json",
headers: { Authorization: `Bearer ${cookies.get("session_token")}`
"Content-Type": "application/json",
Authorization: `Bearer ${cookies.get("session_token")}`
}
} }
); });
if (!res.ok) { if (!res.ok) {
const response = await res.json(); const response = await res.json();

View File

@@ -15,19 +15,16 @@ export const actions: Actions = {
deleteAccount: async ({ request, fetch, cookies }) => { deleteAccount: async ({ request, fetch, cookies }) => {
const data = await request.formData(); const data = await request.formData();
const res = await fetch( const res = await fetch(`/api/rpc/delete_account`, {
`http://localhost:${process.env.ARCHTIKA_API_PORT}/rpc/delete_account`, method: "POST",
{ headers: {
method: "POST", "Content-Type": "application/json",
headers: { Authorization: `Bearer ${cookies.get("session_token")}`
"Content-Type": "application/json", },
Authorization: `Bearer ${cookies.get("session_token")}` body: JSON.stringify({
}, password: data.get("password")
body: JSON.stringify({ })
password: data.get("password") });
})
}
);
const response = await res.json(); const response = await res.json();

View File

@@ -1,29 +1,23 @@
import type { LayoutServerLoad } from "./$types"; import type { LayoutServerLoad } from "./$types";
export const load: LayoutServerLoad = async ({ params, fetch, cookies }) => { export const load: LayoutServerLoad = async ({ params, fetch, cookies }) => {
const websiteData = await fetch( const websiteData = await fetch(`/api/website?id=eq.${params.websiteId}`, {
`http://localhost:${process.env.ARCHTIKA_API_PORT}/website?id=eq.${params.websiteId}`, method: "GET",
{ headers: {
method: "GET", "Content-Type": "application/json",
headers: { Authorization: `Bearer ${cookies.get("session_token")}`,
"Content-Type": "application/json", Accept: "application/vnd.pgrst.object+json"
Authorization: `Bearer ${cookies.get("session_token")}`,
Accept: "application/vnd.pgrst.object+json"
}
} }
); });
const homeData = await fetch( const homeData = await fetch(`/api/home?website_id=eq.${params.websiteId}`, {
`http://localhost:${process.env.ARCHTIKA_API_PORT}/home?website_id=eq.${params.websiteId}`, method: "GET",
{ headers: {
method: "GET", "Content-Type": "application/json",
headers: { Authorization: `Bearer ${cookies.get("session_token")}`,
"Content-Type": "application/json", Accept: "application/vnd.pgrst.object+json"
Authorization: `Bearer ${cookies.get("session_token")}`,
Accept: "application/vnd.pgrst.object+json"
}
} }
); });
const website = await websiteData.json(); const website = await websiteData.json();
const home = await homeData.json(); const home = await homeData.json();

View File

@@ -1,41 +1,32 @@
import type { Actions, PageServerLoad } from "./$types"; import type { Actions, PageServerLoad } from "./$types";
export const load: PageServerLoad = async ({ params, fetch, cookies, url }) => { export const load: PageServerLoad = async ({ params, fetch, cookies, url }) => {
const globalSettingsData = await fetch( const globalSettingsData = await fetch(`/api/settings?website_id=eq.${params.websiteId}`, {
`http://localhost:${process.env.ARCHTIKA_API_PORT}/settings?website_id=eq.${params.websiteId}`, method: "GET",
{ headers: {
method: "GET", "Content-Type": "application/json",
headers: { Authorization: `Bearer ${cookies.get("session_token")}`,
"Content-Type": "application/json", Accept: "application/vnd.pgrst.object+json"
Authorization: `Bearer ${cookies.get("session_token")}`,
Accept: "application/vnd.pgrst.object+json"
}
} }
); });
const headerData = await fetch( const headerData = await fetch(`/api/header?website_id=eq.${params.websiteId}`, {
`http://localhost:${process.env.ARCHTIKA_API_PORT}/header?website_id=eq.${params.websiteId}`, method: "GET",
{ headers: {
method: "GET", "Content-Type": "application/json",
headers: { Authorization: `Bearer ${cookies.get("session_token")}`,
"Content-Type": "application/json", Accept: "application/vnd.pgrst.object+json"
Authorization: `Bearer ${cookies.get("session_token")}`,
Accept: "application/vnd.pgrst.object+json"
}
} }
); });
const footerData = await fetch( const footerData = await fetch(`/api/footer?website_id=eq.${params.websiteId}`, {
`http://localhost:${process.env.ARCHTIKA_API_PORT}/footer?website_id=eq.${params.websiteId}`, method: "GET",
{ headers: {
method: "GET", "Content-Type": "application/json",
headers: { Authorization: `Bearer ${cookies.get("session_token")}`,
"Content-Type": "application/json", Accept: "application/vnd.pgrst.object+json"
Authorization: `Bearer ${cookies.get("session_token")}`,
Accept: "application/vnd.pgrst.object+json"
}
} }
); });
const globalSettings = await globalSettingsData.json(); const globalSettings = await globalSettingsData.json();
const header = await headerData.json(); const header = await headerData.json();
@@ -53,21 +44,18 @@ export const actions: Actions = {
const data = await request.formData(); const data = await request.formData();
const faviconFile = data.get("favicon") as File; const faviconFile = data.get("favicon") as File;
const uploadedImageData = await fetch( const uploadedImageData = await fetch(`/api/rpc/upload_file`, {
`http://localhost:${process.env.ARCHTIKA_API_PORT}/rpc/upload_file`, method: "POST",
{ headers: {
method: "POST", "Content-Type": "application/octet-stream",
headers: { Authorization: `Bearer ${cookies.get("session_token")}`,
"Content-Type": "application/octet-stream", Accept: "application/vnd.pgrst.object+json",
Authorization: `Bearer ${cookies.get("session_token")}`, "X-Website-Id": params.websiteId,
Accept: "application/vnd.pgrst.object+json", "X-Mimetype": faviconFile.type,
"X-Website-Id": params.websiteId, "X-Original-Filename": faviconFile.name
"X-Mimetype": faviconFile.type, },
"X-Original-Filename": faviconFile.name body: await faviconFile.arrayBuffer()
}, });
body: await faviconFile.arrayBuffer()
}
);
const uploadedImage = await uploadedImageData.json(); const uploadedImage = await uploadedImageData.json();
@@ -75,21 +63,18 @@ export const actions: Actions = {
return { success: false, message: uploadedImage.message }; return { success: false, message: uploadedImage.message };
} }
const res = await fetch( const res = await fetch(`/api/settings?website_id=eq.${params.websiteId}`, {
`http://localhost:${process.env.ARCHTIKA_API_PORT}/settings?website_id=eq.${params.websiteId}`, method: "PATCH",
{ headers: {
method: "PATCH", "Content-Type": "application/json",
headers: { Authorization: `Bearer ${cookies.get("session_token")}`
"Content-Type": "application/json", },
Authorization: `Bearer ${cookies.get("session_token")}` body: JSON.stringify({
}, accent_color_light_theme: data.get("accent-color-light"),
body: JSON.stringify({ accent_color_dark_theme: data.get("accent-color-dark"),
accent_color_light_theme: data.get("accent-color-light"), favicon_image: uploadedImage.file_id
accent_color_dark_theme: data.get("accent-color-dark"), })
favicon_image: uploadedImage.file_id });
})
}
);
if (!res.ok) { if (!res.ok) {
const response = await res.json(); const response = await res.json();
@@ -105,21 +90,18 @@ export const actions: Actions = {
const data = await request.formData(); const data = await request.formData();
const logoImage = data.get("logo-image") as File; const logoImage = data.get("logo-image") as File;
const uploadedImageData = await fetch( const uploadedImageData = await fetch(`/api/rpc/upload_file`, {
`http://localhost:${process.env.ARCHTIKA_API_PORT}/rpc/upload_file`, method: "POST",
{ headers: {
method: "POST", "Content-Type": "application/octet-stream",
headers: { Authorization: `Bearer ${cookies.get("session_token")}`,
"Content-Type": "application/octet-stream", Accept: "application/vnd.pgrst.object+json",
Authorization: `Bearer ${cookies.get("session_token")}`, "X-Website-Id": params.websiteId,
Accept: "application/vnd.pgrst.object+json", "X-Mimetype": logoImage.type,
"X-Website-Id": params.websiteId, "X-Original-Filename": logoImage.name
"X-Mimetype": logoImage.type, },
"X-Original-Filename": logoImage.name body: await logoImage.arrayBuffer()
}, });
body: await logoImage.arrayBuffer()
}
);
const uploadedImage = await uploadedImageData.json(); const uploadedImage = await uploadedImageData.json();
@@ -127,21 +109,18 @@ export const actions: Actions = {
return { success: false, message: uploadedImage.message }; return { success: false, message: uploadedImage.message };
} }
const res = await fetch( const res = await fetch(`/api/header?website_id=eq.${params.websiteId}`, {
`http://localhost:${process.env.ARCHTIKA_API_PORT}/header?website_id=eq.${params.websiteId}`, method: "PATCH",
{ headers: {
method: "PATCH", "Content-Type": "application/json",
headers: { Authorization: `Bearer ${cookies.get("session_token")}`
"Content-Type": "application/json", },
Authorization: `Bearer ${cookies.get("session_token")}` body: JSON.stringify({
}, logo_type: data.get("logo-type"),
body: JSON.stringify({ logo_text: data.get("logo-text"),
logo_type: data.get("logo-type"), logo_image: uploadedImage.file_id
logo_text: data.get("logo-text"), })
logo_image: uploadedImage.file_id });
})
}
);
if (!res.ok) { if (!res.ok) {
const response = await res.json(); const response = await res.json();
@@ -156,19 +135,16 @@ export const actions: Actions = {
updateHome: async ({ request, fetch, cookies, params }) => { updateHome: async ({ request, fetch, cookies, params }) => {
const data = await request.formData(); const data = await request.formData();
const res = await fetch( const res = await fetch(`/api/home?website_id=eq.${params.websiteId}`, {
`http://localhost:${process.env.ARCHTIKA_API_PORT}/home?website_id=eq.${params.websiteId}`, method: "PATCH",
{ headers: {
method: "PATCH", "Content-Type": "application/json",
headers: { Authorization: `Bearer ${cookies.get("session_token")}`
"Content-Type": "application/json", },
Authorization: `Bearer ${cookies.get("session_token")}` body: JSON.stringify({
}, main_content: data.get("main-content")
body: JSON.stringify({ })
main_content: data.get("main-content") });
})
}
);
if (!res.ok) { if (!res.ok) {
const response = await res.json(); const response = await res.json();
@@ -180,19 +156,16 @@ export const actions: Actions = {
updateFooter: async ({ request, fetch, cookies, params }) => { updateFooter: async ({ request, fetch, cookies, params }) => {
const data = await request.formData(); const data = await request.formData();
const res = await fetch( const res = await fetch(`/api/footer?website_id=eq.${params.websiteId}`, {
`http://localhost:${process.env.ARCHTIKA_API_PORT}/footer?website_id=eq.${params.websiteId}`, method: "PATCH",
{ headers: {
method: "PATCH", "Content-Type": "application/json",
headers: { Authorization: `Bearer ${cookies.get("session_token")}`
"Content-Type": "application/json", },
Authorization: `Bearer ${cookies.get("session_token")}` body: JSON.stringify({
}, additional_text: data.get("additional-text")
body: JSON.stringify({ })
additional_text: data.get("additional-text") });
})
}
);
if (!res.ok) { if (!res.ok) {
const response = await res.json(); const response = await res.json();

View File

@@ -55,10 +55,7 @@
</label> </label>
{#if data.globalSettings.favicon_image} {#if data.globalSettings.favicon_image}
<Modal id="preview-favicon-global-{data.globalSettings.website_id}" text="Preview"> <Modal id="preview-favicon-global-{data.globalSettings.website_id}" text="Preview">
<img <img src={`/api/rpc/retrieve_file?id=${data.globalSettings.favicon_image}`} alt="" />
src={`http://localhost:${process.env.ARCHTIKA_API_PORT}/rpc/retrieve_file?id=${data.globalSettings.favicon_image}`}
alt=""
/>
</Modal> </Modal>
{/if} {/if}
</div> </div>
@@ -109,10 +106,7 @@
</label> </label>
{#if data.header.logo_image} {#if data.header.logo_image}
<Modal id="preview-logo-header-{data.header.website_id}" text="Preview"> <Modal id="preview-logo-header-{data.header.website_id}" text="Preview">
<img <img src={`/api/rpc/retrieve_file?id=${data.header.logo_image}`} alt="" />
src={`http://localhost:${process.env.ARCHTIKA_API_PORT}/rpc/retrieve_file?id=${data.header.logo_image}`}
alt=""
/>
</Modal> </Modal>
{/if} {/if}
</div> </div>

View File

@@ -6,7 +6,7 @@ export const load: PageServerLoad = async ({ params, fetch, cookies, url, parent
const parameters = new URLSearchParams(); const parameters = new URLSearchParams();
const baseFetchUrl = `http://localhost:${process.env.ARCHTIKA_API_PORT}/article?website_id=eq.${params.websiteId}&select=id,title`; const baseFetchUrl = `/api/article?website_id=eq.${params.websiteId}&select=id,title`;
if (searchQuery) { if (searchQuery) {
parameters.append("title", `ilike.*${searchQuery}*`); parameters.append("title", `ilike.*${searchQuery}*`);
@@ -66,7 +66,7 @@ export const actions: Actions = {
createArticle: async ({ request, fetch, cookies, params, locals }) => { createArticle: async ({ request, fetch, cookies, params, locals }) => {
const data = await request.formData(); const data = await request.formData();
const res = await fetch(`http://localhost:${process.env.ARCHTIKA_API_PORT}/article`, { const res = await fetch(`/api/article`, {
method: "POST", method: "POST",
headers: { headers: {
"Content-Type": "application/json", "Content-Type": "application/json",
@@ -89,16 +89,13 @@ export const actions: Actions = {
deleteArticle: async ({ request, fetch, cookies }) => { deleteArticle: async ({ request, fetch, cookies }) => {
const data = await request.formData(); const data = await request.formData();
const res = await fetch( const res = await fetch(`/api/article?id=eq.${data.get("id")}`, {
`http://localhost:${process.env.ARCHTIKA_API_PORT}/article?id=eq.${data.get("id")}`, method: "DELETE",
{ headers: {
method: "DELETE", "Content-Type": "application/json",
headers: { Authorization: `Bearer ${cookies.get("session_token")}`
"Content-Type": "application/json",
Authorization: `Bearer ${cookies.get("session_token")}`
}
} }
); });
if (!res.ok) { if (!res.ok) {
const response = await res.json(); const response = await res.json();

View File

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

View File

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

View File

@@ -4,7 +4,7 @@ export const load: PageServerLoad = async ({ parent, params, fetch, cookies }) =
const { website, home } = await parent(); const { website, home } = await parent();
const collabData = await fetch( const collabData = await fetch(
`http://localhost:${process.env.ARCHTIKA_API_PORT}/collab?website_id=eq.${params.websiteId}&select=*,user!user_id(*)`, `/api/collab?website_id=eq.${params.websiteId}&select=*,user!user_id(*)`,
{ {
method: "GET", method: "GET",
headers: { headers: {
@@ -27,7 +27,7 @@ export const actions: Actions = {
addCollaborator: async ({ request, fetch, cookies, params }) => { addCollaborator: async ({ request, fetch, cookies, params }) => {
const data = await request.formData(); const data = await request.formData();
const res = await fetch(`http://localhost:${process.env.ARCHTIKA_API_PORT}/collab`, { const res = await fetch(`/api/collab`, {
method: "POST", method: "POST",
headers: { headers: {
"Content-Type": "application/json", "Content-Type": "application/json",
@@ -51,7 +51,7 @@ export const actions: Actions = {
const data = await request.formData(); const data = await request.formData();
const res = await fetch( const res = await fetch(
`http://localhost:${process.env.ARCHTIKA_API_PORT}/collab?website_id=eq.${params.websiteId}&user_id=eq.${data.get("user-id")}`, `/api/collab?website_id=eq.${params.websiteId}&user_id=eq.${data.get("user-id")}`,
{ {
method: "PATCH", method: "PATCH",
headers: { headers: {
@@ -75,7 +75,7 @@ export const actions: Actions = {
const data = await request.formData(); const data = await request.formData();
const res = await fetch( const res = await fetch(
`http://localhost:${process.env.ARCHTIKA_API_PORT}/collab?website_id=eq.${params.websiteId}&user_id=eq.${data.get("user-id")}`, `/api/collab?website_id=eq.${params.websiteId}&user_id=eq.${data.get("user-id")}`,
{ {
method: "DELETE", method: "DELETE",
headers: { headers: {

View File

@@ -4,23 +4,20 @@ import { md } from "$lib/utils";
import type { Actions, PageServerLoad } from "./$types"; import type { Actions, PageServerLoad } from "./$types";
export const load: PageServerLoad = async ({ params, fetch, cookies, locals }) => { export const load: PageServerLoad = async ({ params, fetch, cookies, locals }) => {
const websiteOverviewData = await fetch( const websiteOverviewData = await fetch(`/api/website_overview?id=eq.${params.websiteId}`, {
`http://localhost:${process.env.ARCHTIKA_API_PORT}/website_overview?id=eq.${params.websiteId}`, method: "GET",
{ headers: {
method: "GET", "Content-Type": "application/json",
headers: { Authorization: `Bearer ${cookies.get("session_token")}`,
"Content-Type": "application/json", Accept: "application/vnd.pgrst.object+json"
Authorization: `Bearer ${cookies.get("session_token")}`,
Accept: "application/vnd.pgrst.object+json"
}
} }
); });
const websiteOverview = await websiteOverviewData.json(); const websiteOverview = await websiteOverviewData.json();
generateStaticFiles(websiteOverview); generateStaticFiles(websiteOverview);
const websitePreviewUrl = `http://localhost:${process.env.ARCHTIKA_NGINX_PORT}/previews/${websiteOverview.user_id}/${websiteOverview.id}/index.html`; const websitePreviewUrl = `/user-websites/previews/${websiteOverview.user_id}/${websiteOverview.id}/index.html`;
return { return {
websiteOverview, websiteOverview,
@@ -125,7 +122,7 @@ const generateStaticFiles = async (websiteData: any, isPreview: boolean = true)
) )
.replace( .replace(
"{{cover_image}}", "{{cover_image}}",
`<img src="${article.cover_image ? `http://localhost:${process.env.ARCHTIKA_API_PORT}/rpc/retrieve_file?id=${article.cover_image}` : "https://picsum.photos/600/200"}" />` `<img src="${article.cover_image ? `/api/rpc/retrieve_file?id=${article.cover_image}` : "https://picsum.photos/600/200"}" />`
) )
.replace("{{title}}", `<h1>${article.title}</h1>`) .replace("{{title}}", `<h1>${article.title}</h1>`)
.replace("{{publication_date}}", `<p>${article.publication_date}</p>`) .replace("{{publication_date}}", `<p>${article.publication_date}</p>`)