simplify cors middleware remove .well-knwon endpoint and stick with redirect

This commit is contained in:
austinkelsay 2024-11-15 13:51:11 -06:00
parent 6a8634f2a4
commit 1d54f3d123
No known key found for this signature in database
GPG Key ID: 44CB4EC6D9F2FA02
2 changed files with 10 additions and 59 deletions

View File

@ -1,46 +0,0 @@
import appConfig from "@/config/appConfig"
import { runMiddleware, corsMiddleware } from "@/utils/corsMiddleware";
import { getLightningAddressByName } from "@/db/models/lightningAddressModels";
const BACKEND_URL = process.env.BACKEND_URL
const ZAP_PUBKEY = process.env.ZAP_PUBKEY
export default async function handler(req, res) {
// Run CORS middleware first
await runMiddleware(req, res, corsMiddleware);
const { slug } = req.query;
if (!slug || slug === 'undefined') {
res.status(404).json({ error: 'Not found' })
return
}
let foundAddress = null;
const customAddress = appConfig.customLightningAddresses.find(addr => addr.name === slug);
if (customAddress) {
foundAddress = customAddress;
} else {
foundAddress = await getLightningAddressByName(slug);
}
if (!foundAddress) {
res.status(404).json({ error: 'Lightning address not found' })
return
}
const metadata = [
["text/plain", `${foundAddress.description}`]
];
res.status(200).json({
callback: `${BACKEND_URL}/api/lightning-address/callback/${foundAddress.name}`,
maxSendable: foundAddress.maxSendable || 10000000000,
minSendable: foundAddress.minSendable || 1000,
metadata: JSON.stringify(metadata),
tag: 'payRequest',
allowsNostr: true,
nostrPubkey: ZAP_PUBKEY
});
}

View File

@ -2,23 +2,20 @@ import Cors from 'cors';
// Initialize the cors middleware
export const corsMiddleware = Cors({
methods: ['GET', 'HEAD', 'POST', 'OPTIONS'],
origin: '*',
credentials: true,
optionsSuccessStatus: 200,
allowedHeaders: ['Content-Type', 'Authorization'],
methods: ['GET', 'HEAD', 'POST', 'OPTIONS'],
origin: '*',
});
// Helper method to wait for a middleware to execute before continuing
// And to throw an error when an error happens in a middleware
export async function runMiddleware(req, res, middleware) {
return new Promise((resolve, reject) => {
middleware(req, res, (result) => {
if (result instanceof Error) {
return reject(result);
}
return resolve(result);
});
});
return new Promise((resolve, reject) => {
middleware(req, res, (result) => {
if (result instanceof Error) {
return reject(result);
}
return resolve(result);
});
});
}