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 = {
type = "app";
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
}/web-app
''}/bin/web-wrapper";

View File

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

View File

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

View File

@@ -50,12 +50,6 @@ in
default = 10000;
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 {
@@ -110,7 +104,7 @@ in
};
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;
recommendedTlsSettings = true;
virtualHosts."archtika" = {
listen = [
{
addr = "0.0.0.0";
port = cfg.nginxPort;
}
];
virtualHosts = {
"demo.archtika.com" = {
enableACME = true;
forceSSL = true;
locations = {
"/" = {
root = "/var/www/archtika-websites";
proxyPass = "http://localhost:${toString cfg.webAppPort}";
};
"/user-websites/" = {
alias = "/var/www/archtika-websites/";
index = "index.html";
tryFiles = "$uri $uri/ $uri/index.html =404";
extraConfig = ''
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",
"private": true,
"scripts": {
"dev": "ARCHTIKA_API_PORT=3000 ARCHTIKA_NGINX_PORT=18000 vite dev",
"dev": "vite dev",
"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:watch": "svelte-kit sync && svelte-check --tsconfig ./tsconfig.json --watch",
"lint": "prettier --check .",

View File

@@ -1,7 +1,8 @@
import { redirect } from "@sveltejs/kit";
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/")) {
const userData = await event.fetch(`/api/account`, {
method: "GET",
headers: {
"Content-Type": "application/json",
@@ -23,6 +24,7 @@ export const handle = async ({ event, resolve }) => {
event.locals.user = user;
}
}
return await resolve(event);
};

View File

@@ -4,7 +4,7 @@ export const actions: Actions = {
default: async ({ request, cookies, fetch }) => {
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",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({

View File

@@ -4,7 +4,7 @@ export const actions: Actions = {
default: async ({ request, fetch }) => {
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",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

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}`,
{
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,9 +21,7 @@ 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`,
{
const uploadedImageData = await fetch(`/api/rpc/upload_file`, {
method: "POST",
headers: {
"Content-Type": "application/octet-stream",
@@ -37,8 +32,7 @@ export const actions: Actions = {
"X-Original-Filename": coverFile.name
},
body: await coverFile.arrayBuffer()
}
);
});
const uploadedImage = await uploadedImageData.json();
@@ -46,9 +40,7 @@ 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}`,
{
const res = await fetch(`/api/article?id=eq.${params.articleId}`, {
method: "PATCH",
headers: {
"Content-Type": "application/json",
@@ -62,8 +54,7 @@ export const actions: Actions = {
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>

View File

@@ -4,7 +4,7 @@ export const load: PageServerLoad = async ({ parent, params, fetch, cookies }) =
const { website, home } = await parent();
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",
headers: {
@@ -27,7 +27,7 @@ export const actions: Actions = {
addCollaborator: async ({ request, fetch, cookies, params }) => {
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",
headers: {
"Content-Type": "application/json",
@@ -51,7 +51,7 @@ export const actions: Actions = {
const data = await request.formData();
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",
headers: {
@@ -75,7 +75,7 @@ export const actions: Actions = {
const data = await request.formData();
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",
headers: {

View File

@@ -4,23 +4,20 @@ import { md } from "$lib/utils";
import type { Actions, PageServerLoad } from "./$types";
export const load: PageServerLoad = async ({ params, fetch, cookies, locals }) => {
const websiteOverviewData = await fetch(
`http://localhost:${process.env.ARCHTIKA_API_PORT}/website_overview?id=eq.${params.websiteId}`,
{
const websiteOverviewData = await fetch(`/api/website_overview?id=eq.${params.websiteId}`, {
method: "GET",
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${cookies.get("session_token")}`,
Accept: "application/vnd.pgrst.object+json"
}
}
);
});
const websiteOverview = await websiteOverviewData.json();
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 {
websiteOverview,
@@ -125,7 +122,7 @@ const generateStaticFiles = async (websiteData: any, isPreview: boolean = true)
)
.replace(
"{{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("{{publication_date}}", `<p>${article.publication_date}</p>`)