117 lines
4.6 KiB
TypeScript
117 lines
4.6 KiB
TypeScript
|
|
// 온프레미스 Supabase URL과 anon key 설정
|
|
export const getSupabaseUrl = () => {
|
|
// 로컬 스토리지에서 설정된 URL을 우선 사용
|
|
const storedUrl = localStorage.getItem('supabase_url');
|
|
if (storedUrl) {
|
|
// CORS 프록시 설정 확인
|
|
const useProxy = localStorage.getItem('use_cors_proxy') === 'true';
|
|
const proxyType = localStorage.getItem('proxy_type') || 'cloudflare';
|
|
|
|
if (useProxy) {
|
|
// URL에 이미 프로토콜이 포함되어 있는지 확인
|
|
const cleanUrl = storedUrl.trim();
|
|
const hasProtocol = cleanUrl.startsWith('http://') || cleanUrl.startsWith('https://');
|
|
const urlForProxy = hasProtocol ? cleanUrl : `http://${cleanUrl}`;
|
|
|
|
// 선택된 프록시 타입에 따라 URL 생성
|
|
let proxyUrl = '';
|
|
|
|
switch (proxyType) {
|
|
case 'corsproxy.io':
|
|
// 주의: 쿼리 파라미터가 포함된 URL에서 발생하는 문제를 해결하기 위해
|
|
// 전체 URL을 인코딩하고 끝에 슬래시 추가
|
|
proxyUrl = `https://corsproxy.io/?${encodeURIComponent(urlForProxy)}`;
|
|
break;
|
|
case 'thingproxy':
|
|
proxyUrl = `https://thingproxy.freeboard.io/fetch/${urlForProxy}`;
|
|
break;
|
|
case 'allorigins':
|
|
proxyUrl = `https://api.allorigins.win/raw?url=${encodeURIComponent(urlForProxy)}`;
|
|
break;
|
|
case 'cors-anywhere':
|
|
proxyUrl = `https://cors-anywhere.herokuapp.com/${urlForProxy}`;
|
|
break;
|
|
case 'cloudflare':
|
|
// Cloudflare Workers CORS 프록시
|
|
proxyUrl = `https://cors-proxy.azurewebsites.net/api/cors-proxy?url=${encodeURIComponent(urlForProxy)}`;
|
|
break;
|
|
case 'local-proxy':
|
|
// 사용자 지정 로컬 프록시 (개발 환경용)
|
|
proxyUrl = `http://localhost:8080/proxy?url=${encodeURIComponent(urlForProxy)}`;
|
|
break;
|
|
default:
|
|
proxyUrl = `https://cors-proxy.azurewebsites.net/api/cors-proxy?url=${encodeURIComponent(urlForProxy)}`;
|
|
}
|
|
|
|
console.log('CORS 프록시 URL 생성:', proxyUrl);
|
|
return proxyUrl;
|
|
}
|
|
return storedUrl;
|
|
}
|
|
|
|
// 기본값 사용 (환경 변수 대신)
|
|
return 'https://a11.ism.kr';
|
|
};
|
|
|
|
export const getSupabaseKey = () => {
|
|
// 로컬 스토리지에서 설정된 키를 우선 사용
|
|
const storedKey = localStorage.getItem('supabase_key');
|
|
if (storedKey) return storedKey;
|
|
|
|
// 기본값 사용 (환경 변수 대신)
|
|
return 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJyb2xlIjoiYW5vbiIsImlzcyI6InN1cGFiYXNlIiwiaWF0IjoxNzQyMDQ4NTEwLCJleHAiOjQ4OTU2NDg1MTB9.BZx5MF-HldDju9r4eFwOVp9_qj6GOdkjaG6VrJAYAIg';
|
|
};
|
|
|
|
// CORS 프록시 사용 여부 설정
|
|
export const useCorsProxy = (enabled: boolean) => {
|
|
localStorage.setItem('use_cors_proxy', enabled.toString());
|
|
};
|
|
|
|
// CORS 프록시 유형 설정
|
|
export const setProxyType = (proxyType: string) => {
|
|
localStorage.setItem('proxy_type', proxyType);
|
|
};
|
|
|
|
// 현재 사용 중인 프록시 유형 가져오기
|
|
export const getProxyType = () => {
|
|
return localStorage.getItem('proxy_type') || 'cloudflare';
|
|
};
|
|
|
|
// CORS 프록시 사용 여부 확인
|
|
export const isCorsProxyEnabled = () => {
|
|
return localStorage.getItem('use_cors_proxy') === 'true';
|
|
};
|
|
|
|
// 온프레미스 연결을 위한 설정 도우미 함수
|
|
export const configureSupabase = (url: string, key: string, useProxy: boolean = false, proxyType: string = 'corsproxy.io') => {
|
|
// URL 정리 (앞뒤 공백 제거)
|
|
const cleanUrl = url.trim();
|
|
|
|
// URL에 프로토콜이 없는 경우 http:// 추가
|
|
const normalizedUrl = (cleanUrl.startsWith('http://') || cleanUrl.startsWith('https://'))
|
|
? cleanUrl
|
|
: `http://${cleanUrl}`;
|
|
|
|
// HTTP URL을 사용하고 프록시가 활성화되지 않은 경우 경고
|
|
const isHttpUrl = normalizedUrl.startsWith('http:') && !normalizedUrl.startsWith('http://localhost');
|
|
if (isHttpUrl && !useProxy) {
|
|
console.warn('경고: HTTP URL을 사용하면서 CORS 프록시가 비활성화되어 있습니다. 브라우저에서 접근 문제가 발생할 수 있습니다.');
|
|
}
|
|
|
|
// 로컬 스토리지에 설정 저장
|
|
localStorage.setItem('supabase_url', normalizedUrl);
|
|
localStorage.setItem('supabase_key', key);
|
|
localStorage.setItem('use_cors_proxy', useProxy.toString());
|
|
localStorage.setItem('proxy_type', proxyType);
|
|
|
|
// 페이지 새로고침 - 새로운 설정으로 Supabase 클라이언트 초기화
|
|
window.location.reload();
|
|
};
|
|
|
|
// 원본 URL 반환 (프록시 없는 URL)
|
|
export const getOriginalSupabaseUrl = () => {
|
|
const storedUrl = localStorage.getItem('supabase_url');
|
|
return storedUrl || 'https://a11.ism.kr';
|
|
};
|