feat:add domain config

This commit is contained in:
2024-10-07 22:52:51 +08:00
parent 9588217de4
commit 7c81814e17
8 changed files with 177 additions and 47 deletions

View File

@@ -7,11 +7,12 @@ import fs from 'fs';
import { useConfig } from '@abearxiong/use-config';
import { redis } from './redis/redis.ts';
import { getContentType } from './get-content-type.ts';
const { api, domain } = useConfig<{
const { api, domain, allowedOrigins } = useConfig<{
api: {
host: string;
};
domain: string;
allowedOrigins: string[];
}>();
const fileStore = useFileStore('upload');
@@ -19,6 +20,18 @@ console.log('filePath', fileStore);
const noProxyUrl = ['/', '/favicon.ico'];
export const handleRequest = async (req: http.IncomingMessage, res: http.ServerResponse) => {
const dns = getDNS(req);
// 配置可以跨域
// 配置可以访问的域名 localhost, xiongxiao.me
const _orings = allowedOrigins || [];
const host = dns.hostName;
if (
_orings.some((item) => {
return host.includes(item);
})
) {
res.setHeader('Access-Control-Allow-Origin', '*');
}
res.setHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS');
let user, app;
let domainApp = false;
@@ -33,21 +46,19 @@ export const handleRequest = async (req: http.IncomingMessage, res: http.ServerR
if (dns.hostName !== domain) {
// redis获取域名对应的用户和应用
domainApp = true;
const key = 'domain:' + dns.hostName;
const value = await redis.get(key);
if (!value) {
const data = await UserApp.getDomainApp(dns.hostName);
if (!data) {
res.writeHead(404, { 'Content-Type': 'text/plain' });
res.write('Invalid domain\n');
return res.end();
}
const [_user, _app] = value.split(':');
if (!_user || !_app) {
if (!data.user || !data.app) {
res.writeHead(404, { 'Content-Type': 'text/plain' });
res.write('Invalid domain, Config error\n');
res.write('Invalid domain config\n');
return res.end();
}
user = _user;
app = _app;
user = data.user;
app = data.app;
}
}
const url = req.url;
@@ -145,8 +156,10 @@ export const handleRequest = async (req: http.IncomingMessage, res: http.ServerR
const appFile = await userApp.getFile(appFileUrl);
if (!appFile) {
const [indexFilePath, etag] = indexFile.split('||');
const contentType = getContentType(indexFilePath);
const isHTML = contentType.includes('html');
// 不存在的文件,返回indexFile的文件
res.writeHead(200, { 'Content-Type': 'text/html', 'Cache-Control': 'no-cache' });
res.writeHead(200, { 'Content-Type': contentType, 'Cache-Control': isHTML ? 'no-cache' : 'public, max-age=3600' });
const filePath = path.join(fileStore, indexFilePath);
const readStream = fs.createReadStream(filePath);
readStream.pipe(res);
@@ -161,9 +174,10 @@ export const handleRequest = async (req: http.IncomingMessage, res: http.ServerR
}
const filePath = path.join(fileStore, appFilePath);
let contentType = getContentType(filePath);
const isHTML = contentType.includes('html');
res.writeHead(200, {
'Content-Type': contentType,
'Cache-Control': 'public, max-age=3600', // 设置缓存时间为 1 小时
'Cache-Control': isHTML ? 'no-cache' : 'public, max-age=3600', // 设置缓存时间为 1 小时
ETag: eTag,
});
const readStream = fs.createReadStream(filePath);