temp: add content-type
This commit is contained in:
parent
2b9f238e41
commit
569446b99a
21
.env.example
21
.env.example
@ -1,12 +1,13 @@
|
|||||||
# 代理配置
|
# 代理配置
|
||||||
PROXY_PORT=3005
|
PROXY_PORT=3005
|
||||||
PROXY_DOMAIN=localhost
|
PROXY_DOMAIN=localhost
|
||||||
PROXY_RESOURCES=http://localhost:9000/resources
|
PROXY_ALLOWED_ORIGINS=localhost,xiongxiao.me
|
||||||
PROXY_ALLOWED_ORIGINS=localhost,xiongxiao.me,zxj.im
|
|
||||||
|
|
||||||
# 后台代理
|
# 后台代理
|
||||||
API_HOST=http://localhost:4005
|
API_HOST=http://localhost:4005
|
||||||
API_PATH=/api/router
|
API_PATH=/api/router
|
||||||
|
# API assistant 客户端地址
|
||||||
|
API_CLIENT_HOST=https://localhost:51015
|
||||||
|
|
||||||
# Minio 配置
|
# Minio 配置
|
||||||
MINIO_ENDPOINT=localhost
|
MINIO_ENDPOINT=localhost
|
||||||
@ -15,3 +16,19 @@ MINIO_BUCKET_NAME=resources
|
|||||||
MINIO_USE_SSL=false
|
MINIO_USE_SSL=false
|
||||||
MINIO_ACCESS_KEY=username
|
MINIO_ACCESS_KEY=username
|
||||||
MINIO_SECRET_KEY=****
|
MINIO_SECRET_KEY=****
|
||||||
|
|
||||||
|
|
||||||
|
# 本地postgres
|
||||||
|
POSTGRES_HOST=localhost
|
||||||
|
POSTGRES_PORT=5432
|
||||||
|
POSTGRES_USER=root
|
||||||
|
POSTGRES_PASSWORD=needchange
|
||||||
|
POSTGRES_DB=postgres
|
||||||
|
|
||||||
|
## Redis
|
||||||
|
REDIS_HOST=localhost
|
||||||
|
REDIS_PORT=6379
|
||||||
|
REDIS_PASSWORD=needchange
|
||||||
|
|
||||||
|
|
||||||
|
DATA_WEBSITE_ID=
|
@ -12,14 +12,13 @@
|
|||||||
"runtime": [
|
"runtime": [
|
||||||
"client",
|
"client",
|
||||||
"server"
|
"server"
|
||||||
],
|
|
||||||
"files": [
|
|
||||||
"dist"
|
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
"files": [
|
"files": [
|
||||||
"dist"
|
"dist",
|
||||||
|
".env.example"
|
||||||
],
|
],
|
||||||
|
"home": "",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"dev": "bun run --watch --hot --inspect src/index.ts",
|
"dev": "bun run --watch --hot --inspect src/index.ts",
|
||||||
"cmd": "bun run src/run.ts ",
|
"cmd": "bun run src/run.ts ",
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
import { useConfig } from '@kevisual/use-config/env';
|
import { useConfig } from '@kevisual/use-config/env';
|
||||||
import { useFileStore } from '@kevisual/use-config/file-store';
|
import { useFileStore } from '@kevisual/use-config/file-store';
|
||||||
|
import { minioResources } from './minio.ts';
|
||||||
export const fileStore = useFileStore('pages');
|
export const fileStore = useFileStore('pages');
|
||||||
|
|
||||||
type ConfigType = {
|
type ConfigType = {
|
||||||
@ -73,7 +74,7 @@ export const config: ConfigType = {
|
|||||||
proxy: {
|
proxy: {
|
||||||
port: envConfig.PROXY_PORT,
|
port: envConfig.PROXY_PORT,
|
||||||
domain: envConfig.PROXY_DOMAIN,
|
domain: envConfig.PROXY_DOMAIN,
|
||||||
resources: envConfig.PROXY_RESOURCES,
|
resources: minioResources,
|
||||||
allowedOrigin: (envConfig.PROXY_ALLOWED_ORIGINS as string)?.split(',') || [],
|
allowedOrigin: (envConfig.PROXY_ALLOWED_ORIGINS as string)?.split(',') || [],
|
||||||
},
|
},
|
||||||
redis: {
|
redis: {
|
||||||
|
@ -1,7 +1,30 @@
|
|||||||
import path from 'path';
|
import path from 'path';
|
||||||
|
export const getTextContentType = (ext: string) => {
|
||||||
|
const textContentTypes = [
|
||||||
|
'.tsx',
|
||||||
|
'.jsx', //
|
||||||
|
'.conf',
|
||||||
|
'.env',
|
||||||
|
'.example',
|
||||||
|
'.log',
|
||||||
|
'.mjs',
|
||||||
|
'.map',
|
||||||
|
'.json5',
|
||||||
|
];
|
||||||
|
const include = textContentTypes.includes(ext);
|
||||||
|
if (!include) {
|
||||||
|
return {};
|
||||||
|
}
|
||||||
|
const contentType = getContentTypeCore(ext);
|
||||||
|
if (!contentType) {
|
||||||
|
return {};
|
||||||
|
}
|
||||||
|
return {
|
||||||
|
'Content-Type': contentType,
|
||||||
|
};
|
||||||
|
};
|
||||||
// 获取文件的 content-type
|
// 获取文件的 content-type
|
||||||
export const getContentType = (filePath: string) => {
|
export const getContentTypeCore = (extname: string) => {
|
||||||
const extname = path.extname(filePath);
|
|
||||||
const contentType = {
|
const contentType = {
|
||||||
'.html': 'text/html; charset=utf-8',
|
'.html': 'text/html; charset=utf-8',
|
||||||
'.js': 'text/javascript; charset=utf-8',
|
'.js': 'text/javascript; charset=utf-8',
|
||||||
@ -11,9 +34,12 @@ export const getContentType = (filePath: string) => {
|
|||||||
'.ts': 'text/typescript; charset=utf-8',
|
'.ts': 'text/typescript; charset=utf-8',
|
||||||
'.jsx': 'text/javascript; charset=utf-8',
|
'.jsx': 'text/javascript; charset=utf-8',
|
||||||
'.conf': 'text/plain; charset=utf-8',
|
'.conf': 'text/plain; charset=utf-8',
|
||||||
|
'.env': 'text/plain; charset=utf-8',
|
||||||
|
'.example': 'text/plain; charset=utf-8',
|
||||||
'.log': 'text/plain; charset=utf-8',
|
'.log': 'text/plain; charset=utf-8',
|
||||||
'.mjs': 'text/javascript; charset=utf-8',
|
'.mjs': 'text/javascript; charset=utf-8',
|
||||||
'.map': 'application/json; charset=utf-8',
|
'.map': 'application/json; charset=utf-8',
|
||||||
|
|
||||||
'.json5': 'application/json5; charset=utf-8',
|
'.json5': 'application/json5; charset=utf-8',
|
||||||
'.json': 'application/json; charset=utf-8',
|
'.json': 'application/json; charset=utf-8',
|
||||||
'.png': 'image/png',
|
'.png': 'image/png',
|
||||||
@ -52,5 +78,11 @@ export const getContentType = (filePath: string) => {
|
|||||||
'.yml': 'application/x-yaml; charset=utf-8', // YAML 文件(别名)
|
'.yml': 'application/x-yaml; charset=utf-8', // YAML 文件(别名)
|
||||||
'.zip': 'application/octet-stream',
|
'.zip': 'application/octet-stream',
|
||||||
};
|
};
|
||||||
return contentType[extname] || 'application/octet-stream';
|
return contentType[extname];
|
||||||
|
};
|
||||||
|
|
||||||
|
export const getContentType = (filePath: string) => {
|
||||||
|
const extname = path.extname(filePath).toLowerCase();
|
||||||
|
const contentType = getContentTypeCore(extname);
|
||||||
|
return contentType || 'application/octet-stream';
|
||||||
};
|
};
|
||||||
|
@ -14,7 +14,7 @@ import { logger } from './logger.ts';
|
|||||||
|
|
||||||
const pipelineAsync = promisify(pipeline);
|
const pipelineAsync = promisify(pipeline);
|
||||||
|
|
||||||
const { resources } = config?.proxy || { resources: 'https://minio.xiongxiao.me/resources' };
|
const { resources } = config?.proxy || { resources: minioResources };
|
||||||
const wrapperResources = (resources: string, urlpath: string) => {
|
const wrapperResources = (resources: string, urlpath: string) => {
|
||||||
if (urlpath.startsWith('http')) {
|
if (urlpath.startsWith('http')) {
|
||||||
return urlpath;
|
return urlpath;
|
||||||
|
@ -284,7 +284,10 @@ export const handleRequest = async (req: http.IncomingMessage, res: http.ServerR
|
|||||||
appFileUrl = (url + '').replace(`/${user}/${app}/`, '');
|
appFileUrl = (url + '').replace(`/${user}/${app}/`, '');
|
||||||
}
|
}
|
||||||
appFileUrl = decodeURIComponent(appFileUrl); // Decode URL components
|
appFileUrl = decodeURIComponent(appFileUrl); // Decode URL components
|
||||||
const appFile = await userApp.getFile(appFileUrl);
|
let appFile = await userApp.getFile(appFileUrl);
|
||||||
|
if (!appFile && url.endsWith('/')) {
|
||||||
|
appFile = await userApp.getFile(appFileUrl + 'index.html');
|
||||||
|
}
|
||||||
if (isExist.proxy) {
|
if (isExist.proxy) {
|
||||||
let proxyUrl = appFile || isExist.indexFilePath;
|
let proxyUrl = appFile || isExist.indexFilePath;
|
||||||
if (!proxyUrl.startsWith('http')) {
|
if (!proxyUrl.startsWith('http')) {
|
||||||
|
@ -7,6 +7,9 @@ import http from 'http';
|
|||||||
import https from 'https';
|
import https from 'https';
|
||||||
import { UserApp } from '../get-user-app.ts';
|
import { UserApp } from '../get-user-app.ts';
|
||||||
import { addStat } from '../html/stat/index.ts';
|
import { addStat } from '../html/stat/index.ts';
|
||||||
|
import path from 'path';
|
||||||
|
import { getTextContentType } from '../get-content-type.ts';
|
||||||
|
import { logger } from '../logger.ts';
|
||||||
|
|
||||||
const pipelineAsync = promisify(pipeline);
|
const pipelineAsync = promisify(pipeline);
|
||||||
|
|
||||||
@ -50,7 +53,7 @@ export async function minioProxy(
|
|||||||
const etag = stat.etag;
|
const etag = stat.etag;
|
||||||
const lastModified = stat.lastModified.toISOString();
|
const lastModified = stat.lastModified.toISOString();
|
||||||
const fileName = objectName.split('/').pop();
|
const fileName = objectName.split('/').pop();
|
||||||
|
const ext = path.extname(fileName || '');
|
||||||
const objectStream = await minioClient.getObject(bucketName, objectName);
|
const objectStream = await minioClient.getObject(bucketName, objectName);
|
||||||
const headers = {
|
const headers = {
|
||||||
'Content-Length': contentLength,
|
'Content-Length': contentLength,
|
||||||
@ -58,6 +61,7 @@ export async function minioProxy(
|
|||||||
'last-modified': lastModified,
|
'last-modified': lastModified,
|
||||||
'file-name': fileName,
|
'file-name': fileName,
|
||||||
...filterMetaData,
|
...filterMetaData,
|
||||||
|
...getTextContentType(ext),
|
||||||
};
|
};
|
||||||
if (objectName.endsWith('.html') && !isDownload) {
|
if (objectName.endsWith('.html') && !isDownload) {
|
||||||
const { html, contentLength } = await getTextFromStreamAndAddStat(objectStream);
|
const { html, contentLength } = await getTextFromStreamAndAddStat(objectStream);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user