Skip to content

Commit

Permalink
use ip endpoint to fetch from non-https endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
muliswilliam committed Sep 20, 2023
1 parent cdfacef commit e3d116a
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 11 deletions.
3 changes: 2 additions & 1 deletion src/middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ export default authMiddleware({
'/api/files/upload',
'/api/msg/new',
'api/msg/destroy',
'/api/msg/message-viewed'
'/api/msg/message-viewed',
'api/ip'
]
})

Expand Down
22 changes: 22 additions & 0 deletions src/pages/api/ip.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { NextApiRequest, NextApiResponse } from "next";
import { IpAddressInfo } from "../../shared/types";

export default async function handler(req: NextApiRequest, res: NextApiResponse) {
if (req.method !== 'GET') {
res.setHeader('Allow', ['GET'])
res.status(405).json({
error: { message: `Method ${req.method} Not Allowed` }
})
}

const response = await fetch('http://ip-api.com/json/')
const data = await response.json()
const { status, query, ...rest } = data

if (status !== 'success') {
res.status(500).json({ error: 'Failed to get IP address info' })
}
const ipInfo = { ...rest, ipAddress: query } as IpAddressInfo

res.status(200).json({ ...ipInfo })
}
3 changes: 0 additions & 3 deletions src/pages/messages/[publicId].tsx
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,6 @@ export const getServerSideProps: GetServerSideProps = async ({ params, req }) =>
}
})

const ipAddressInfo = await getIpAddressInfo()
console.log(ipAddressInfo)

// log message_viewed
const session = getAuth(req)
const messageViewedEvent = {
Expand Down
13 changes: 6 additions & 7 deletions src/shared/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -176,12 +176,11 @@ export function getLiveKitURL(region?: string | string[]): string {
// add return type

export async function getIpAddressInfo(): Promise<IpAddressInfo> {
const response = await fetch('http://ip-api.com/json/')
const data = await response.json()
const { status, query, ...rest } = data

if (status !== 'success') {
throw new Error('Failed to get IP address info')
try {
const response = await fetch('/api/ip')
return await response.json() as IpAddressInfo
} catch (error) {
console.log(error)
throw new Error('Failed to get IP address info')
}
return { ...rest, ipAddress: query } as IpAddressInfo
}

0 comments on commit e3d116a

Please sign in to comment.