Skip to content

Commit

Permalink
[feat] add CORS support
Browse files Browse the repository at this point in the history
  • Loading branch information
AndyBoWu committed Dec 16, 2024
1 parent 0d25b01 commit 8bf376c
Show file tree
Hide file tree
Showing 5 changed files with 20,178 additions and 15,639 deletions.
3 changes: 2 additions & 1 deletion agent/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@
!api.ts
!apiServer.ts
!maincharacter.ts
!corsOptions.ts
.env
*.env
.env*
/data
/generatedImages
/generatedImages
18 changes: 10 additions & 8 deletions agent/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,25 +35,27 @@
"@ai16z/plugin-conflux": "workspace:*",
"@ai16z/plugin-evm": "workspace:*",
"@ai16z/plugin-flow": "workspace:*",
"@ai16z/plugin-story": "workspace:*",
"@ai16z/plugin-goat": "workspace:*",
"@ai16z/plugin-icp": "workspace:*",
"@ai16z/plugin-image-generation": "workspace:*",
"@ai16z/plugin-node": "workspace:*",
"@ai16z/plugin-solana": "workspace:*",
"@ai16z/plugin-starknet": "workspace:*",
"@ai16z/plugin-story": "workspace:*",
"@ai16z/plugin-tee": "workspace:*",
"cors": "^2.8.5",
"dotenv": "16.4.5",
"express": "^4.18.2",
"readline": "1.3.0",
"ws": "8.18.0",
"yargs": "17.7.2",
"express": "^4.18.2",
"cors": "^2.8.5"
"yargs": "17.7.2"
},
"devDependencies": {
"ts-node": "10.9.2",
"tsup": "8.3.5",
"@types/express": "^4.17.21",
"@types/cors": "^2.8.17",
"cross-env": "^7.0.3"
"@types/dotenv": "^8.2.3",
"@types/express": "^4.17.21",
"cross-env": "^7.0.3",
"ts-node": "10.9.2",
"tsup": "8.3.5"
}
}
8 changes: 8 additions & 0 deletions agent/src/apiServer.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
// apiServer.ts
import dotenv from "dotenv";
import path from "path";

import express from "express";
import cors from "cors";
import corsOptions from "./corsOptions.ts"; // Import the CORS options
import { fileURLToPath, pathToFileURL } from "url";
import { dirname } from "path";
import { elizaLogger } from "@ai16z/eliza";
Expand All @@ -8,6 +13,9 @@ import apiRouter from "./api.js";
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);

// Load environment variables from .env file
dotenv.config({ path: path.resolve(__dirname, '../../.env') });

export function startApiServer(port?: number) {
const app = express();

Expand Down
28 changes: 28 additions & 0 deletions agent/src/corsOptions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// corsOptions.ts

import { CorsOptions } from 'cors';

// Parse allowed origins from environment variable, split by comma
const allowedOrigins = process.env.ALLOWED_ORIGINS
? process.env.ALLOWED_ORIGINS.split(",").map((origin) => origin.trim())
: [];

// Define CORS options
const corsOptions: CorsOptions = {
origin: (origin, callback) => {
// Allow requests with no origin (e.g., mobile apps, curl requests)
if (!origin) return callback(null, true);

if (allowedOrigins.includes(origin)) {
return callback(null, true);
} else {
const msg = 'The CORS policy for this site does not allow access from the specified Origin.';
return callback(new Error(msg), false);
}
},
methods: ['GET', 'POST', 'PUT', 'DELETE', 'OPTIONS'],
allowedHeaders: ['Content-Type', 'Authorization'],
credentials: true, // Adjust based on your needs
};

export default corsOptions;
Loading

0 comments on commit 8bf376c

Please sign in to comment.