Skip to content

Commit

Permalink
better cors
Browse files Browse the repository at this point in the history
  • Loading branch information
JEMeyer committed Jul 5, 2024
1 parent da8ce52 commit 9667576
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 3 deletions.
2 changes: 1 addition & 1 deletion .env.template
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
EDGE_SERVER_PORT=4000 # default in ai-maestro-edge

# Falls back to []. Will always allow in localhost or 192.168.x.x
CORS_ORIGIN=
CORS_ORIGINS=http://domain1.com,https://domain2.io

# MariaDB/MySQL
SQL_HOST=
Expand Down
22 changes: 20 additions & 2 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,26 @@ const server = createServer(app);
// Middleware to parse JSON bodies
app.use(express.json());

// Enable CORS
app.use(cors());
// CORS configuration
const corsOrigins = process.env.ALLOWED_CORS_ORIGINS?.split(',') ?? [];
const corsOptions = {
origin: function (
origin: string | undefined,
callback: (error: Error | null, allow?: boolean) => void
) {
const allowedOrigins = ['http://localhost:5173', ...corsOrigins];
if (!origin || allowedOrigins.indexOf(origin) !== -1) {
callback(null, true);
} else {
callback(new Error('Not allowed by CORS'));
}
},
credentials: true,
optionsSuccessStatus: 200,
};

// Enable CORS with the specified options
app.use(cors(corsOptions));

app.use('/api/computers', computersRouter);
app.use('/api/gpus', gpusRouter);
Expand Down

0 comments on commit 9667576

Please sign in to comment.