Skip to content

Commit

Permalink
experiment with edge functions
Browse files Browse the repository at this point in the history
  • Loading branch information
muliswilliam committed Sep 20, 2023
1 parent e919355 commit 83e6ee2
Show file tree
Hide file tree
Showing 11 changed files with 39 additions and 14 deletions.
4 changes: 4 additions & 0 deletions src/pages/api/chat/livekit_token.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,3 +68,7 @@ export default async function handler(
res.status(500).end()
}
}

export const config = {
runtime: 'edge' // this is a pre-requisite
}
4 changes: 4 additions & 0 deletions src/pages/api/chat/url.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,7 @@ export default async function handleServerUrl(
res.status(500).end()
}
}

export const config = {
runtime: 'edge' // this is a pre-requisite
}
2 changes: 2 additions & 0 deletions src/pages/api/files/upload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { once } from 'events'
const bucketName = process.env.SUPABASE_BUCKET_NAME as string

export const config = {
runtime: 'edge',
api: {
bodyParser: false
}
Expand Down Expand Up @@ -58,3 +59,4 @@ export default async function handler(
res.status(400).json({ message: 'Error uploading file', error })
}
}

23 changes: 13 additions & 10 deletions src/pages/api/ip.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import { NextApiRequest, NextApiResponse } from "next";
import { IpAddressInfo } from "../../shared/types";
import { NextApiRequest, NextApiResponse } from 'next'
import { IpAddressInfo } from '../../shared/types'

export default async function handler(req: NextApiRequest, res: NextApiResponse) {
export default async function handler(
req: NextApiRequest,
res: NextApiResponse
) {
if (req.method !== 'GET') {
res.setHeader('Allow', ['GET'])
res.status(405).json({
Expand All @@ -10,13 +13,13 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
}

// TODO: Use these values instead of making a request to ip-api.com
// console.log('x-vercel-forwarded-for' ,req.headers['x-vercel-forwarded-for'])
// console.log('x-vercel-ip-country' ,req.headers['x-vercel-ip-country'])
// console.log('x-vercel-ip-country-region' ,req.headers['x-vercel-ip-country-region'])
// console.log('x-vercel-ip-city' ,req.headers['x-vercel-ip-city'])
// console.log('x-vercel-ip-longitude' ,req.headers['x-vercel-ip-longitude'])
// console.log('x-vercel-ip-latitude' ,req.headers['x-vercel-ip-latitude'])
// console.log('x-vercel-ip-timezone' ,req.headers['x-vercel-ip-timezone'])
// console.log('x-vercel-forwarded-for', req.headers['x-vercel-forwarded-for'])
// console.log('x-vercel-ip-country', req.headers['x-vercel-ip-country'])
// console.log('x-vercel-ip-country-region', req.headers['x-vercel-ip-country-region'])
// console.log('x-vercel-ip-city', req.headers['x-vercel-ip-city'])
// console.log('x-vercel-ip-longitude', req.headers['x-vercel-ip-longitude'])
// console.log('x-vercel-ip-latitude', req.headers['x-vercel-ip-latitude'])
// console.log('x-vercel-ip-timezone', req.headers['x-vercel-ip-timezone'])

const ipAddress = req.headers['x-vercel-forwarded-for'] as string
const response = await fetch(`http://ip-api.com/json/${ipAddress}`)
Expand Down
4 changes: 4 additions & 0 deletions src/pages/api/msg/[messageId].ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,3 +56,7 @@ export default async function handler(
res.status(500).json({ error })
}
}

export const config = {
runtime: 'edge' // this is a pre-requisite
}
4 changes: 4 additions & 0 deletions src/pages/api/msg/destroy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,3 +56,7 @@ async function handler(req: NextApiRequest, res: NextApiResponse) {
}

export default verifySignature(handler)

export const config = {
runtime: 'edge' // this is a pre-requisite
}
4 changes: 4 additions & 0 deletions src/pages/api/msg/message-viewed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,7 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
return res.status(500).send({ error })
}
}

export const config = {
runtime: 'edge' // this is a pre-requisite
}
4 changes: 4 additions & 0 deletions src/pages/api/msg/new.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,3 +73,7 @@ export default async function handler(
return res.status(500).send({ error })
}
}

export const config = {
runtime: 'edge' // this is a pre-requisite
}
2 changes: 0 additions & 2 deletions src/pages/dashboard/messages.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,6 @@ export default function Messages({
events
}: InferGetServerSidePropsType<typeof getServerSideProps>) {

console.log(events.map(event => event.ipAddressInfo))

return (
<>
<AppHead />
Expand Down
1 change: 0 additions & 1 deletion src/pages/messages/[publicId].tsx
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,6 @@ export default function MessagePage({ message, messageViewedEventId }: MessagePr
setMessageType('text')
const text = await decryptText(encryptionDetails.ct, encryptionKey)
setDecryptedMessage(text)
console.log(messageViewedEventId)
if (messageViewedEventId) {
const ipAddressInfo = await getIpAddressInfo()
await fetch('/api/msg/message-viewed', {
Expand Down
1 change: 0 additions & 1 deletion src/shared/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ export function useServerUrl(region?: string) {
fetch(endpoint).then(async (res) => {
if (res.ok) {
const body = await res.json();
console.log(body);
setServerUrl(body.url);
} else {
throw Error('Error fetching server url, check server logs');
Expand Down

0 comments on commit 83e6ee2

Please sign in to comment.