Skip to content

Commit

Permalink
fix: Frontend server request ip updates (#4066)
Browse files Browse the repository at this point in the history
* fix: testing how throttling works on server side requests

* fix: updates to make testing easier

* fix: possible fix

* fix: some small tweaks

* fix: updates

* fix: forwards requester IP to API for throttling

* fix: missed an update

* fix: test update
  • Loading branch information
YazeedLoonat authored May 7, 2024
1 parent fa41751 commit 4ed3179
Show file tree
Hide file tree
Showing 8 changed files with 102 additions and 55 deletions.
9 changes: 7 additions & 2 deletions sites/partners/src/pages/listings/[id]/edit.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,17 @@ const EditListing = (props: { listing: Listing }) => {
)
}

export async function getServerSideProps(context: { params: Record<string, string> }) {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export async function getServerSideProps(context: { params: Record<string, string>; req: any }) {
let response

try {
response = await axios.get(`${process.env.backendApiBase}/listings/${context.params.id}`, {
headers: { passkey: process.env.API_PASS_KEY },
headers: {
passkey: process.env.API_PASS_KEY,
"x-forwarded-for":
context.req.headers["x-forwarded-for"] ?? context.req.socket.remoteAddress,
},
})
} catch (e) {
console.log("e = ", e)
Expand Down
9 changes: 7 additions & 2 deletions sites/partners/src/pages/listings/[id]/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -125,12 +125,17 @@ export default function ListingDetail(props: ListingProps) {
)
}

export async function getServerSideProps(context: { params: Record<string, string> }) {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export async function getServerSideProps(context: { params: Record<string, string>; req: any }) {
let response

try {
response = await axios.get(`${process.env.backendApiBase}/listings/${context.params.id}`, {
headers: { passkey: process.env.API_PASS_KEY },
headers: {
passkey: process.env.API_PASS_KEY,
"x-forwarded-for":
context.req.headers["x-forwarded-for"] ?? context.req.socket.remoteAddress,
},
})
} catch (e) {
return { notFound: true }
Expand Down
95 changes: 57 additions & 38 deletions sites/public/src/lib/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,20 +59,24 @@ export const useGetApplicationStatusProps = (listing: Listing): ApplicationStatu
return props
}

export async function fetchBaseListingData({
additionalFilters,
orderBy,
orderDir,
limit,
}: {
additionalFilters?: ListingFilterParams[]
orderBy?: ListingOrderByKeys[]
orderDir?: OrderByEnum[]
limit?: string
}) {
export async function fetchBaseListingData(
{
additionalFilters,
orderBy,
orderDir,
limit,
}: {
additionalFilters?: ListingFilterParams[]
orderBy?: ListingOrderByKeys[]
orderDir?: OrderByEnum[]
limit?: string
},
// eslint-disable-next-line @typescript-eslint/no-explicit-any
req: any
) {
let listings = []
try {
const { id: jurisdictionId } = await fetchJurisdictionByName()
const { id: jurisdictionId } = await fetchJurisdictionByName(req)

if (!jurisdictionId) {
return listings
Expand Down Expand Up @@ -109,7 +113,10 @@ export async function fetchBaseListingData({
paramsSerializer: (params) => {
return qs.stringify(params)
},
headers: { passkey: process.env.API_PASS_KEY },
headers: {
passkey: process.env.API_PASS_KEY,
"x-forwarded-for": req.headers["x-forwarded-for"] ?? req.socket.remoteAddress,
},
})

listings = response.data?.items
Expand All @@ -120,36 +127,45 @@ export async function fetchBaseListingData({
return listings
}

export async function fetchOpenListings() {
return await fetchBaseListingData({
additionalFilters: [
{
$comparison: EnumListingFilterParamsComparison["="],
status: ListingsStatusEnum.active,
},
],
orderBy: [ListingOrderByKeys.mostRecentlyPublished],
orderDir: [OrderByEnum.desc],
})
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export async function fetchOpenListings(req: any) {
return await fetchBaseListingData(
{
additionalFilters: [
{
$comparison: EnumListingFilterParamsComparison["="],
status: ListingsStatusEnum.active,
},
],
orderBy: [ListingOrderByKeys.mostRecentlyPublished],
orderDir: [OrderByEnum.desc],
},
req
)
}

export async function fetchClosedListings() {
return await fetchBaseListingData({
additionalFilters: [
{
$comparison: EnumListingFilterParamsComparison["="],
status: ListingsStatusEnum.closed,
},
],
orderBy: [ListingOrderByKeys.mostRecentlyClosed],
orderDir: [OrderByEnum.desc],
limit: "10",
})
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export async function fetchClosedListings(req: any) {
return await fetchBaseListingData(
{
additionalFilters: [
{
$comparison: EnumListingFilterParamsComparison["="],
status: ListingsStatusEnum.closed,
},
],
orderBy: [ListingOrderByKeys.mostRecentlyClosed],
orderDir: [OrderByEnum.desc],
limit: "10",
},
req
)
}

let jurisdiction: Jurisdiction | null = null

export async function fetchJurisdictionByName() {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export async function fetchJurisdictionByName(req: any) {
try {
if (jurisdiction) {
return jurisdiction
Expand All @@ -159,7 +175,10 @@ export async function fetchJurisdictionByName() {
const jurisdictionRes = await axios.get(
`${process.env.backendApiBase}/jurisdictions/byName/${jurisdictionName}`,
{
headers: { passkey: process.env.API_PASS_KEY },
headers: {
passkey: process.env.API_PASS_KEY,
"x-forwarded-for": req.headers["x-forwarded-for"] ?? req.socket.remoteAddress,
},
}
)
jurisdiction = jurisdictionRes?.data
Expand Down
5 changes: 3 additions & 2 deletions sites/public/src/pages/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -114,8 +114,9 @@ export default function Home(props: IndexProps) {
)
}

export async function getStaticProps() {
const jurisdiction = await fetchJurisdictionByName()
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export async function getStaticProps(context: { req: any }) {
const jurisdiction = await fetchJurisdictionByName(context.req)

return {
props: { jurisdiction },
Expand Down
9 changes: 7 additions & 2 deletions sites/public/src/pages/listing/[id].tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,17 @@ export default function ListingRedirect(props: Record<string, string>) {
)
}

export async function getServerSideProps(context: { params: Record<string, string> }) {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export async function getServerSideProps(context: { params: Record<string, string>; req: any }) {
let response

try {
response = await axios.get(`${process.env.backendApiBase}/listings/${context.params.id}`, {
headers: { passkey: process.env.API_PASS_KEY },
headers: {
passkey: process.env.API_PASS_KEY,
"x-forwarded-for":
context.req.headers["x-forwarded-for"] ?? context.req.socket.remoteAddress,
},
})
} catch (e) {
return { notFound: true }
Expand Down
12 changes: 9 additions & 3 deletions sites/public/src/pages/listing/[id]/[slug].tsx
Original file line number Diff line number Diff line change
Expand Up @@ -119,17 +119,23 @@ export default function ListingPage(props: ListingProps) {
export async function getServerSideProps(context: {
params: Record<string, string>
locale: string
// eslint-disable-next-line @typescript-eslint/no-explicit-any
req: any
}) {
let response

try {
response = await axios.get(`${process.env.backendApiBase}/listings/${context.params.id}`, {
headers: { language: context.locale, passkey: process.env.API_PASS_KEY },
headers: {
language: context.locale,
passkey: process.env.API_PASS_KEY,
"x-forwarded-for":
context.req.headers["x-forwarded-for"] ?? context.req.socket.remoteAddress,
},
})
} catch (e) {
return { notFound: true }
}
const jurisdiction = fetchJurisdictionByName()
const jurisdiction = fetchJurisdictionByName(context.req)

return { props: { listing: response.data, jurisdiction: await jurisdiction } }
}
7 changes: 4 additions & 3 deletions sites/public/src/pages/listings.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,10 @@ export default function ListingsPage(props: ListingsProps) {
)
}

export async function getServerSideProps() {
const openListings = fetchOpenListings()
const closedListings = fetchClosedListings()
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export async function getServerSideProps(context: { req: any }) {
const openListings = fetchOpenListings(context.req)
const closedListings = fetchClosedListings(context.req)

return {
props: { openListings: await openListings, closedListings: await closedListings },
Expand Down
11 changes: 8 additions & 3 deletions sites/public/src/pages/preview/listings/[id].tsx
Original file line number Diff line number Diff line change
Expand Up @@ -44,18 +44,23 @@ export default function ListingPage(props: ListingProps) {
)
}

export async function getServerSideProps(context: { params: Record<string, string> }) {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export async function getServerSideProps(context: { params: Record<string, string>; req: any }) {
let response

try {
response = await axios.get(`${process.env.backendApiBase}/listings/${context.params.id}`, {
headers: { passkey: process.env.API_PASS_KEY },
headers: {
passkey: process.env.API_PASS_KEY,
"x-forwarded-for":
context.req.headers["x-forwarded-for"] ?? context.req.socket.remoteAddress,
},
})
} catch (e) {
return { notFound: true }
}

const jurisdiction = fetchJurisdictionByName()
const jurisdiction = fetchJurisdictionByName(context.req)

return { props: { listing: response.data, jurisdiction: await jurisdiction } }
}

0 comments on commit 4ed3179

Please sign in to comment.