From 996126a9efc5e60e356e344b4461f0a9d5b6f2a0 Mon Sep 17 00:00:00 2001 From: HUAHUAI23 Date: Thu, 12 Sep 2024 08:43:11 +0000 Subject: [PATCH 1/4] feat(objectstorage): web host add history router support --- .../objectstorage/.vscode/settings.json | 8 ++- .../src/pages/api/site/openHost.ts | 57 +++++++------------ .../src/templates/nginx/site-host | 49 ++++++++++++++++ 3 files changed, 77 insertions(+), 37 deletions(-) create mode 100644 frontend/providers/objectstorage/src/templates/nginx/site-host diff --git a/frontend/providers/objectstorage/.vscode/settings.json b/frontend/providers/objectstorage/.vscode/settings.json index f0a94803de5..f2c26a2c262 100644 --- a/frontend/providers/objectstorage/.vscode/settings.json +++ b/frontend/providers/objectstorage/.vscode/settings.json @@ -3,5 +3,11 @@ "i18n-ally.localesPaths": [ "public/locales" ], - "i18n-ally.keystyle": "nested" + "i18n-ally.keystyle": "nested", + "[typescriptreact]": { + "editor.defaultFormatter": "esbenp.prettier-vscode" + }, + "[typescript]": { + "editor.defaultFormatter": "esbenp.prettier-vscode" + } } \ No newline at end of file diff --git a/frontend/providers/objectstorage/src/pages/api/site/openHost.ts b/frontend/providers/objectstorage/src/pages/api/site/openHost.ts index 27c0e7f3e3f..6b2bf6c036c 100644 --- a/frontend/providers/objectstorage/src/pages/api/site/openHost.ts +++ b/frontend/providers/objectstorage/src/pages/api/site/openHost.ts @@ -1,14 +1,17 @@ import type { NextApiRequest, NextApiResponse } from 'next'; -import { initK8s } from 'sealos-desktop-sdk/service'; import { ApiResp } from '@/services/kubernet'; import { jsonRes } from '@/services/backend/response'; import { appLanuchPadClient } from '@/services/request'; +import fs from 'fs/promises'; +import _ from 'lodash'; +import path from 'path'; export default async function handler(req: NextApiRequest, res: NextApiResponse) { try { - const client = await initK8s({ req }); const { bucket } = req.body as { bucket?: string }; + if (!bucket) return jsonRes(res, { code: 400, data: { error: 'bucketName is invaild' } }); + const appName = `static-host-${bucket}`; const result = await appLanuchPadClient.post( '/createApp', @@ -44,40 +47,7 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse< { mountPath: '/etc/nginx/nginx.conf', subPath: 'nginx.conf', - value: `user nginx; - worker_processes auto; - - error_log /var/log/nginx/error.log notice; - pid /var/run/nginx.pid; - - - events { - worker_connections 1024; - } - - - http { - - proxy_intercept_errors on; - - server { - listen 80; - - error_page 404 = /404.html; - - location / { - rewrite ^/404\\.html$ /${bucket}/404.html break; - rewrite ^/$ /${bucket}/index.html break; - rewrite ^/(.+)/$ /${bucket}/$1/index.html break; - rewrite ^/(.*\\..*)$ /${bucket}/$1 break; - rewrite ^/(.+)$ /${bucket}/$1/index.html break; - - proxy_pass http://object-storage.objectstorage-system.svc.cluster.local; - } - } - - sendfile on; - }` + value: await generateNginxConfig(bucket) } ], secret: { @@ -107,3 +77,18 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse< }); } } + +async function generateNginxConfig(bucketName: string) { + try { + const templatePath = path.join(process.cwd(), 'src', 'templates', 'nginx', 'site-host'); + const template = await fs.readFile(templatePath, 'utf-8'); + + const compiledTemplate = _.template(template); + + const nginxConfig = compiledTemplate({ bucket: bucketName }); + return nginxConfig; + } catch (error) { + console.error('Error generating nginx conf', error); + throw error; + } +} diff --git a/frontend/providers/objectstorage/src/templates/nginx/site-host b/frontend/providers/objectstorage/src/templates/nginx/site-host new file mode 100644 index 00000000000..f976ec5ffcf --- /dev/null +++ b/frontend/providers/objectstorage/src/templates/nginx/site-host @@ -0,0 +1,49 @@ +user nginx; +worker_processes auto; + +error_log /var/log/nginx/error.log notice; +pid /var/run/nginx.pid; + +events { + worker_connections 1024; +} + +http { + proxy_intercept_errors on; + + server { + listen 80; + # 主位置块 + location / { + rewrite ^/404\.html$ /<%= bucket %>/404.html break; + rewrite ^/$ /<%= bucket %>/index.html break; + rewrite ^/(.+)/$ /<%= bucket %>/$1/index.html break; + rewrite ^/(.+)$ /<%= bucket %>/$1 break; + proxy_pass http://object-storage.objectstorage-system.svc.cluster.local; + proxy_set_header Host $host; + proxy_set_header X-Real-IP $remote_addr; + proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; + proxy_set_header X-Forwarded-Proto $scheme; + # 处理 404 错误,支持 History 模式路由 + error_page 404 = @spa; + } + + # 专门处理 SPA 路由 + location @spa { + rewrite ^(.*)$ /<%= bucket %>/index.html break; + proxy_pass http://object-storage.objectstorage-system.svc.cluster.local; + } + + # 处理静态资源 + location ~* \.(js|css|png|jpg|jpeg|gif|ico|svg|woff|woff2|ttf|eot|json)$ { + rewrite ^/(.+)$ /<%= bucket %>/$1 break; + proxy_pass http://object-storage.objectstorage-system.svc.cluster.local; + proxy_set_header Host $host; + proxy_set_header X-Real-IP $remote_addr; + expires max; + log_not_found off; + } + } + + sendfile on; +} \ No newline at end of file From 9a12fdeae56e4bf23cbf233a1bebb25cbe4bd89f Mon Sep 17 00:00:00 2001 From: HUAHUAI23 Date: Thu, 12 Sep 2024 08:55:33 +0000 Subject: [PATCH 2/4] ok --- .../providers/objectstorage/src/pages/api/site/openHost.ts | 3 +++ frontend/providers/objectstorage/src/templates/nginx/site-host | 3 --- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/frontend/providers/objectstorage/src/pages/api/site/openHost.ts b/frontend/providers/objectstorage/src/pages/api/site/openHost.ts index 6b2bf6c036c..d10a0be101a 100644 --- a/frontend/providers/objectstorage/src/pages/api/site/openHost.ts +++ b/frontend/providers/objectstorage/src/pages/api/site/openHost.ts @@ -1,4 +1,5 @@ import type { NextApiRequest, NextApiResponse } from 'next'; +import { initK8s } from 'sealos-desktop-sdk/service'; import { ApiResp } from '@/services/kubernet'; import { jsonRes } from '@/services/backend/response'; import { appLanuchPadClient } from '@/services/request'; @@ -8,6 +9,8 @@ import path from 'path'; export default async function handler(req: NextApiRequest, res: NextApiResponse) { try { + // auth + await initK8s({ req }); const { bucket } = req.body as { bucket?: string }; if (!bucket) return jsonRes(res, { code: 400, data: { error: 'bucketName is invaild' } }); diff --git a/frontend/providers/objectstorage/src/templates/nginx/site-host b/frontend/providers/objectstorage/src/templates/nginx/site-host index f976ec5ffcf..ca9cc130272 100644 --- a/frontend/providers/objectstorage/src/templates/nginx/site-host +++ b/frontend/providers/objectstorage/src/templates/nginx/site-host @@ -13,7 +13,6 @@ http { server { listen 80; - # 主位置块 location / { rewrite ^/404\.html$ /<%= bucket %>/404.html break; rewrite ^/$ /<%= bucket %>/index.html break; @@ -28,13 +27,11 @@ http { error_page 404 = @spa; } - # 专门处理 SPA 路由 location @spa { rewrite ^(.*)$ /<%= bucket %>/index.html break; proxy_pass http://object-storage.objectstorage-system.svc.cluster.local; } - # 处理静态资源 location ~* \.(js|css|png|jpg|jpeg|gif|ico|svg|woff|woff2|ttf|eot|json)$ { rewrite ^/(.+)$ /<%= bucket %>/$1 break; proxy_pass http://object-storage.objectstorage-system.svc.cluster.local; From ab11743162bca8376056ecc68a84eb79c22a1a35 Mon Sep 17 00:00:00 2001 From: HUAHUAI23 Date: Thu, 12 Sep 2024 09:02:02 +0000 Subject: [PATCH 3/4] ok --- .../providers/objectstorage/src/pages/api/site/openHost.ts | 3 --- 1 file changed, 3 deletions(-) diff --git a/frontend/providers/objectstorage/src/pages/api/site/openHost.ts b/frontend/providers/objectstorage/src/pages/api/site/openHost.ts index d10a0be101a..6b2bf6c036c 100644 --- a/frontend/providers/objectstorage/src/pages/api/site/openHost.ts +++ b/frontend/providers/objectstorage/src/pages/api/site/openHost.ts @@ -1,5 +1,4 @@ import type { NextApiRequest, NextApiResponse } from 'next'; -import { initK8s } from 'sealos-desktop-sdk/service'; import { ApiResp } from '@/services/kubernet'; import { jsonRes } from '@/services/backend/response'; import { appLanuchPadClient } from '@/services/request'; @@ -9,8 +8,6 @@ import path from 'path'; export default async function handler(req: NextApiRequest, res: NextApiResponse) { try { - // auth - await initK8s({ req }); const { bucket } = req.body as { bucket?: string }; if (!bucket) return jsonRes(res, { code: 400, data: { error: 'bucketName is invaild' } }); From fab4235f1fdd9091a50a325fed81a646ff861ce7 Mon Sep 17 00:00:00 2001 From: HUAHUAI23 Date: Thu, 12 Sep 2024 09:09:12 +0000 Subject: [PATCH 4/4] ok --- frontend/providers/objectstorage/src/templates/nginx/site-host | 1 - 1 file changed, 1 deletion(-) diff --git a/frontend/providers/objectstorage/src/templates/nginx/site-host b/frontend/providers/objectstorage/src/templates/nginx/site-host index ca9cc130272..c215fa7440f 100644 --- a/frontend/providers/objectstorage/src/templates/nginx/site-host +++ b/frontend/providers/objectstorage/src/templates/nginx/site-host @@ -23,7 +23,6 @@ http { proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Forwarded-Proto $scheme; - # 处理 404 错误,支持 History 模式路由 error_page 404 = @spa; }