Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(objectstorage): web host add history router support #5076

Merged
merged 6 commits into from
Sep 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion frontend/providers/objectstorage/.vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
57 changes: 21 additions & 36 deletions frontend/providers/objectstorage/src/pages/api/site/openHost.ts
Original file line number Diff line number Diff line change
@@ -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<ApiResp>) {
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',
Expand Down Expand Up @@ -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: {
Expand Down Expand Up @@ -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;
}
}
45 changes: 45 additions & 0 deletions frontend/providers/objectstorage/src/templates/nginx/site-host
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
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;
error_page 404 = @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;
}
Loading