mirror of
https://github.com/danielmiessler/Fabric.git
synced 2026-01-09 22:38:10 -05:00
Merge pull request #1485 from ksylvan/0523-generalize-web-ui-connect-to-fabric-api
Web UI: Centralize Environment Configuration and Make Fabric Base URL Configurable
This commit is contained in:
62
web/src/lib/config/environment.ts
Normal file
62
web/src/lib/config/environment.ts
Normal file
@@ -0,0 +1,62 @@
|
|||||||
|
/**
|
||||||
|
* Environment configuration for the Fabric web app
|
||||||
|
* Centralizes all environment variable handling
|
||||||
|
*/
|
||||||
|
|
||||||
|
// Default values
|
||||||
|
const DEFAULT_FABRIC_BASE_URL = 'http://localhost:8080';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the Fabric base URL from environment variable or default
|
||||||
|
* This function works in both server and client contexts
|
||||||
|
*/
|
||||||
|
export function getFabricBaseUrl(): string {
|
||||||
|
// In server context (Node.js), use process.env
|
||||||
|
if (typeof process !== 'undefined' && process.env) {
|
||||||
|
return process.env.FABRIC_BASE_URL || DEFAULT_FABRIC_BASE_URL;
|
||||||
|
}
|
||||||
|
|
||||||
|
// In client context, check if the environment was injected via Vite
|
||||||
|
if (typeof window !== 'undefined' && (window as any).__FABRIC_CONFIG__) {
|
||||||
|
return (window as any).__FABRIC_CONFIG__.FABRIC_BASE_URL || DEFAULT_FABRIC_BASE_URL;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Fallback to default
|
||||||
|
return DEFAULT_FABRIC_BASE_URL;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the Fabric API base URL (adds /api if not present)
|
||||||
|
*/
|
||||||
|
export function getFabricApiUrl(): string {
|
||||||
|
const baseUrl = getFabricBaseUrl();
|
||||||
|
|
||||||
|
// Remove trailing slash if present
|
||||||
|
const cleanBaseUrl = baseUrl.replace(/\/$/, '');
|
||||||
|
|
||||||
|
// Check if it already ends with /api
|
||||||
|
if (cleanBaseUrl.endsWith('/api')) {
|
||||||
|
return cleanBaseUrl;
|
||||||
|
}
|
||||||
|
|
||||||
|
return `${cleanBaseUrl}/api`;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Configuration object for easy access to all environment settings
|
||||||
|
*/
|
||||||
|
export const config = {
|
||||||
|
fabricBaseUrl: getFabricBaseUrl(),
|
||||||
|
fabricApiUrl: getFabricApiUrl(),
|
||||||
|
} as const;
|
||||||
|
|
||||||
|
// Type definitions
|
||||||
|
export interface FabricConfig {
|
||||||
|
FABRIC_BASE_URL: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
declare global {
|
||||||
|
interface Window {
|
||||||
|
__FABRIC_CONFIG__?: FabricConfig;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -15,11 +15,11 @@ export const POST: RequestHandler = async ({ request }) => {
|
|||||||
language: body.language,
|
language: body.language,
|
||||||
hasLanguageParam: true
|
hasLanguageParam: true
|
||||||
});
|
});
|
||||||
|
|
||||||
// Extract video ID
|
// Extract video ID
|
||||||
const match = body.url.match(/(?:youtube\.com\/(?:[^\/]+\/.+\/|(?:v|e(?:mbed)?)\/|.*[?&]v=)|youtu\.be\/)([^"&?\/\s]{11})/);
|
const match = body.url.match(/(?:youtube\.com\/(?:[^\/]+\/.+\/|(?:v|e(?:mbed)?)\/|.*[?&]v=)|youtu\.be\/)([^"&?\/\s]{11})/);
|
||||||
const videoId = match ? match[1] : null;
|
const videoId = match ? match[1] : null;
|
||||||
|
|
||||||
if (!videoId) {
|
if (!videoId) {
|
||||||
return json({ error: 'Invalid YouTube URL' }, { status: 400 });
|
return json({ error: 'Invalid YouTube URL' }, { status: 400 });
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,6 +2,9 @@ import { purgeCss } from 'vite-plugin-tailwind-purgecss';
|
|||||||
import { sveltekit } from '@sveltejs/kit/vite';
|
import { sveltekit } from '@sveltejs/kit/vite';
|
||||||
import { defineConfig } from 'vite';
|
import { defineConfig } from 'vite';
|
||||||
|
|
||||||
|
// Get the Fabric base URL from environment variable with fallback
|
||||||
|
const FABRIC_BASE_URL = process.env.FABRIC_BASE_URL || 'http://localhost:8080';
|
||||||
|
|
||||||
export default defineConfig({
|
export default defineConfig({
|
||||||
plugins: [sveltekit(), purgeCss()],
|
plugins: [sveltekit(), purgeCss()],
|
||||||
build: {
|
build: {
|
||||||
@@ -18,6 +21,10 @@ export default defineConfig({
|
|||||||
'process.browser': true,
|
'process.browser': true,
|
||||||
'process': {
|
'process': {
|
||||||
cwd: () => ('/')
|
cwd: () => ('/')
|
||||||
|
},
|
||||||
|
// Inject Fabric configuration for client-side access
|
||||||
|
'__FABRIC_CONFIG__': {
|
||||||
|
FABRIC_BASE_URL: JSON.stringify(FABRIC_BASE_URL)
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
resolve: {
|
resolve: {
|
||||||
@@ -31,11 +38,11 @@ export default defineConfig({
|
|||||||
},
|
},
|
||||||
proxy: {
|
proxy: {
|
||||||
'/api': {
|
'/api': {
|
||||||
target: 'http://localhost:8080',
|
target: FABRIC_BASE_URL,
|
||||||
changeOrigin: true,
|
changeOrigin: true,
|
||||||
timeout: 30000,
|
timeout: 30000,
|
||||||
rewrite: (path) => path.replace(/^\/api/, ''),
|
rewrite: (path) => path.replace(/^\/api/, ''),
|
||||||
configure: (proxy, options) => {
|
configure: (proxy, _options) => {
|
||||||
proxy.on('error', (err, req, res) => {
|
proxy.on('error', (err, req, res) => {
|
||||||
console.log('proxy error', err);
|
console.log('proxy error', err);
|
||||||
res.writeHead(500, {
|
res.writeHead(500, {
|
||||||
@@ -46,10 +53,10 @@ export default defineConfig({
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
'^/(patterns|models|sessions)/names': {
|
'^/(patterns|models|sessions)/names': {
|
||||||
target: 'http://localhost:8080',
|
target: FABRIC_BASE_URL,
|
||||||
changeOrigin: true,
|
changeOrigin: true,
|
||||||
timeout: 30000,
|
timeout: 30000,
|
||||||
configure: (proxy, options) => {
|
configure: (proxy, _options) => {
|
||||||
proxy.on('error', (err, req, res) => {
|
proxy.on('error', (err, req, res) => {
|
||||||
console.log('proxy error', err);
|
console.log('proxy error', err);
|
||||||
res.writeHead(500, {
|
res.writeHead(500, {
|
||||||
|
|||||||
Reference in New Issue
Block a user