Skip to content

Commit

Permalink
Merge pull request #40 from desci-labs/handle-cors
Browse files Browse the repository at this point in the history
Added CORS handling
  • Loading branch information
kadamidev authored Oct 17, 2024
2 parents bec3927 + 37e44f6 commit 69eddb8
Showing 1 changed file with 37 additions and 0 deletions.
37 changes: 37 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,49 @@ import {
type ResolveGenericQueryParams,
} from "./api/v2/resolvers/generic.js";

const allowlist = [
"http://localhost:3000",
"http://localhost:3001",
"http://localhost:61440",
"http://localhost:3002",
"http://host.docker.internal:3000",
"http://host.docker.internal:3002",
"http://127.0.0.1:3000",
"https://nodes.desci.com",
"https://nodes-dev.desci.com",
"https://nodes-demo.desci.com",
"d2195goqok3wlx.amplifyapp.com",
"d3ge8gcb3rt5iw.amplifyapp.com",
"desci.com",
"gitpod.io",
"loca.lt" /** NOT SECURE */,
"vercel.app" /** NOT SECURE */,
];

export const app: Express = express();
const port = process.env.PORT || 5460;

app.use(pinoHttp({ logger }));
app.use(express.json());

app.use(function (req, res, next) {
// Handle CORS
const origin = req.headers.origin;
if (
(origin && allowlist.indexOf(origin) !== -1) ||
(origin && allowlist.filter((a) => a.indexOf("http") != 0 && origin && origin.endsWith(a)).length)
) {
res.setHeader("Access-Control-Allow-Origin", origin);
res.setHeader(
"Access-Control-Allow-Headers",
"X-Requested-With,Content-Type,Authorization,sentry-trace,baggage",
);
res.setHeader("Access-Control-Allow-Methods", "GET, POST, PATCH, OPTIONS, PUT, DELETE");
res.setHeader("Access-Control-Allow-Credentials", "true");
}
next();
});

app.use("/api", api);

// Should probably check connectivity with ceramic/blockchain RPC/IPFS node
Expand Down

0 comments on commit 69eddb8

Please sign in to comment.