Skip to content

Commit

Permalink
pass x-real-ip to 2a5-api
Browse files Browse the repository at this point in the history
  • Loading branch information
reckseba committed Jun 23, 2024
1 parent dd73223 commit b09a38d
Showing 1 changed file with 15 additions and 2 deletions.
17 changes: 15 additions & 2 deletions pages/api/newUrlLong.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { NextApiRequest, NextApiResponse } from "next";
import { IncomingHttpHeaders } from "http";
import psl from "psl";

import { getHostname } from "../../lib/hostnames";
Expand All @@ -14,14 +15,15 @@ type SuccessType = {
urlShortFull: string;
}

async function doAPIRequest(urlLong: string) {
async function doAPIRequest(urlLong: string, ipAddress: string) {
const endpoint = "http://" + ( process.env.DOCKER_API_HOSTNAME || "localhost" ) + ":" + ( process.env.DOCKER_API_PORT || "3000" ) + "/api/urlLong/new";
const payload = {
urlLong: urlLong
};
const options = {
headers: {
"Content-Type": "application/json",
"x-real-ip": ipAddress
},
method: "PUT",
body: JSON.stringify(payload)
Expand Down Expand Up @@ -85,8 +87,19 @@ export default async function handler(
return;
}

const getClientIpAddress = (headers: IncomingHttpHeaders): string => {

if (headers.hasOwnProperty("x-real-ip")) {
return headers["x-real-ip"] as string;
}

return "127.0.0.1";
};

const ipAddress = getClientIpAddress(req.headers);

// finally check if the requested urlLong is already in database
const response = await doAPIRequest(req.body.urlLong);
const response = await doAPIRequest(req.body.urlLong, ipAddress);

if (response == false) {
res.status(400).json({
Expand Down

0 comments on commit b09a38d

Please sign in to comment.