unknownfriend00007 commited on
Commit
9f98007
·
verified ·
1 Parent(s): fc05a6d

Update server.js

Browse files
Files changed (1) hide show
  1. server.js +24 -11
server.js CHANGED
@@ -14,7 +14,7 @@ app.set('trust proxy', 1);
14
  // Limit each IP to 100 requests per 15 minutes
15
  const limiter = rateLimit({
16
  windowMs: 15 * 60 * 1000,
17
- max: 150, // number of max requests
18
  standardHeaders: true,
19
  legacyHeaders: false,
20
  message: { error: "Too many requests, please try again later." }
@@ -26,7 +26,7 @@ const allowedOrigins = process.env.ALLOWED_ORIGINS ? process.env.ALLOWED_ORIGINS
26
 
27
  app.use(cors({
28
  origin: function (origin, callback) {
29
- // Allow requests with no origin (like mobile apps/curl) or if list is empty
30
  if (!origin || allowedOrigins.length === 0) return callback(null, true);
31
 
32
  if (allowedOrigins.indexOf(origin) !== -1) {
@@ -42,6 +42,7 @@ app.use(express.json());
42
  // --- 4. MULTI-INSTANCE CONFIGURATION ---
43
  let INSTANCES = [];
44
  try {
 
45
  INSTANCES = JSON.parse(process.env.FLOWISE_INSTANCES || '[]');
46
  } catch (e) {
47
  console.error("CRITICAL ERROR: Could not parse FLOWISE_INSTANCES JSON", e);
@@ -62,9 +63,14 @@ async function refreshFlowDirectory() {
62
  // Run all fetches in parallel
63
  const promises = INSTANCES.map(async (inst) => {
64
  try {
65
- const res = await fetch(`${inst.url}/api/v1/chatflows`, {
66
- headers: { 'Authorization': `Bearer ${inst.key}` }
67
- });
 
 
 
 
 
68
  if(!res.ok) throw new Error(`Status ${res.status}`);
69
  const flows = await res.json();
70
 
@@ -107,14 +113,21 @@ app.post('/api/v1/prediction/:botName', async (req, res) => {
107
  const finalTarget = flowDirectory[botName];
108
 
109
  try {
 
 
 
 
 
 
 
 
 
 
 
 
110
  const flowiseResponse = await fetch(`${finalTarget.host}/api/v1/prediction/${finalTarget.id}`, {
111
  method: 'POST',
112
- headers: {
113
- 'Content-Type': 'application/json',
114
- 'Authorization': `Bearer ${finalTarget.key}`,
115
- 'HTTP-Referer': req.headers.origin || 'https://huggingface.co',
116
- 'X-Title': 'FederatedProxy'
117
- },
118
  body: JSON.stringify(req.body)
119
  });
120
 
 
14
  // Limit each IP to 100 requests per 15 minutes
15
  const limiter = rateLimit({
16
  windowMs: 15 * 60 * 1000,
17
+ max: 100,
18
  standardHeaders: true,
19
  legacyHeaders: false,
20
  message: { error: "Too many requests, please try again later." }
 
26
 
27
  app.use(cors({
28
  origin: function (origin, callback) {
29
+ // Open Mode: If list is empty or origin is null (tools/mobile), allow it.
30
  if (!origin || allowedOrigins.length === 0) return callback(null, true);
31
 
32
  if (allowedOrigins.indexOf(origin) !== -1) {
 
42
  // --- 4. MULTI-INSTANCE CONFIGURATION ---
43
  let INSTANCES = [];
44
  try {
45
+ // Example Secret: [{"url":"https://my-flowise.com","key":"tr-123"}, {"url":"https://open-flowise.com","key":""}]
46
  INSTANCES = JSON.parse(process.env.FLOWISE_INSTANCES || '[]');
47
  } catch (e) {
48
  console.error("CRITICAL ERROR: Could not parse FLOWISE_INSTANCES JSON", e);
 
63
  // Run all fetches in parallel
64
  const promises = INSTANCES.map(async (inst) => {
65
  try {
66
+ // Smart Auth: Only add header if key exists
67
+ const headers = {};
68
+ if (inst.key && inst.key.length > 0) {
69
+ headers['Authorization'] = `Bearer ${inst.key}`;
70
+ }
71
+
72
+ const res = await fetch(`${inst.url}/api/v1/chatflows`, { headers });
73
+
74
  if(!res.ok) throw new Error(`Status ${res.status}`);
75
  const flows = await res.json();
76
 
 
113
  const finalTarget = flowDirectory[botName];
114
 
115
  try {
116
+ // Construct Headers for forwarding
117
+ const forwardHeaders = {
118
+ 'Content-Type': 'application/json',
119
+ 'HTTP-Referer': req.headers.origin || 'https://huggingface.co',
120
+ 'X-Title': 'FederatedProxy'
121
+ };
122
+
123
+ // Smart Auth: Only add Bearer if key exists
124
+ if (finalTarget.key && finalTarget.key.length > 0) {
125
+ forwardHeaders['Authorization'] = `Bearer ${finalTarget.key}`;
126
+ }
127
+
128
  const flowiseResponse = await fetch(`${finalTarget.host}/api/v1/prediction/${finalTarget.id}`, {
129
  method: 'POST',
130
+ headers: forwardHeaders,
 
 
 
 
 
131
  body: JSON.stringify(req.body)
132
  });
133