Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add ipinfo worker #2800

Merged
merged 4 commits into from
May 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { GrowthBook, GrowthBookProvider } from '@growthbook/growthbook-react';
import { useQuery } from '@tanstack/react-query';
import { GROWTHBOOK_KEY, IS_MAINNET, mainnetStaffs, testnetStaffs } from 'data';
import getIpInfo from 'lib/getIpInfo';
import isGardener from 'lib/isGardener';
import type { FC, ReactNode } from 'react';
import { useEffect } from 'react';
Expand All @@ -17,6 +19,12 @@ interface FeatureFlagsProviderProps {
const FeatureFlagsProvider: FC<FeatureFlagsProviderProps> = ({ children }) => {
const currentProfile = useAppStore((state) => state.currentProfile);

const { data: ipInfoData } = useQuery(
['ipInfoData'],
() => getIpInfo().then((res) => res),
{ enabled: Boolean(currentProfile?.id) }
);

useEffect(() => {
if (currentProfile?.id) {
growthbook.loadFeatures();
Expand All @@ -26,11 +34,12 @@ const FeatureFlagsProvider: FC<FeatureFlagsProviderProps> = ({ children }) => {
isStaff: IS_MAINNET
? mainnetStaffs.includes(currentProfile.id)
: testnetStaffs.includes(currentProfile.id),
browser: window.navigator.userAgent
browser: window.navigator.userAgent,
country: ipInfoData?.country ?? 'Unknown'
});
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [currentProfile]);
}, [currentProfile, ipInfoData]);

return (
<GrowthBookProvider growthbook={growthbook}>{children}</GrowthBookProvider>
Expand Down
18 changes: 9 additions & 9 deletions apps/web/src/components/Common/Providers/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,11 @@ const Providers = ({ children }: { children: ReactNode }) => {
return (
<I18nProvider i18n={i18n}>
<ErrorBoundary>
<FeatureFlagsProvider>
<TelemetryProvider />
<WagmiConfig client={wagmiClient}>
<ApolloProvider client={apolloClient}>
<QueryClientProvider client={queryClient}>
<TelemetryProvider />
<WagmiConfig client={wagmiClient}>
<ApolloProvider client={apolloClient}>
<QueryClientProvider client={queryClient}>
<FeatureFlagsProvider>
<LivepeerConfig
client={livepeerClient}
theme={getLivepeerTheme}
Expand All @@ -77,10 +77,10 @@ const Providers = ({ children }: { children: ReactNode }) => {
<Layout>{children}</Layout>
</ThemeProvider>
</LivepeerConfig>
</QueryClientProvider>
</ApolloProvider>
</WagmiConfig>
</FeatureFlagsProvider>
</FeatureFlagsProvider>
</QueryClientProvider>
</ApolloProvider>
</WagmiConfig>
</ErrorBoundary>
</I18nProvider>
);
Expand Down
21 changes: 21 additions & 0 deletions packages/lib/getIpInfo.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import axios from 'axios';

/**
* Get the IP info from the IP info API.
* @returns The IP info.
*/
const getIpInfo = async (): Promise<{
success: boolean;
ip: string;
country: string;
ray: string;
}> => {
try {
const response = await axios('https://ipinfo.lenster.xyz');
return response.data;
} catch {
throw new Error('Failed to get IP info.');
}
};

export default getIpInfo;
1 change: 1 addition & 0 deletions packages/lib/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"prettier:fix": "prettier --write \"**/*.{js,ts,tsx,md}\" --cache"
},
"dependencies": {
"axios": "^1.4.0",
"data": "workspace:*",
"lens": "workspace:*",
"viem": "^0.3.17"
Expand Down
7 changes: 7 additions & 0 deletions packages/workers/ipinfo/.eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module.exports = {
root: true,
extends: ['weblint'],
rules: {
'import/no-anonymous-default-export': 'off'
}
};
1 change: 1 addition & 0 deletions packages/workers/ipinfo/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# IPInfo worker
22 changes: 22 additions & 0 deletions packages/workers/ipinfo/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"name": "@workers/ipinfo",
"version": "0.0.0",
"private": true,
"license": "GPL-3.0",
"scripts": {
"dev": "wrangler dev --port 8086 --local",
"worker:deploy": "wrangler publish",
"typecheck": "tsc --pretty",
"lint": "eslint . --ext .ts",
"lint:fix": "eslint . --fix --ext .ts",
"prettier": "prettier --check \"**/*.{js,ts,tsx,md}\" --cache",
"prettier:fix": "prettier --write \"**/*.{js,ts,tsx,md}\" --cache"
},
"devDependencies": {
"@cloudflare/workers-types": "^4.20230419.0",
"eslint-config-weblint": "workspace:*",
"tsconfig": "workspace:*",
"typescript": "^5.0.4",
"wrangler": "^2.19.0"
}
}
25 changes: 25 additions & 0 deletions packages/workers/ipinfo/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
const headers = {
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Headers': 'Content-Type',
'Content-Type': 'application/json'
};

const handleRequest = async (request: Request) => {
try {
const ip = request.headers.get('cf-connecting-ip');
const country = request.headers.get('cf-ipcountry');
const ray = request.headers.get('cf-ray');

return new Response(JSON.stringify({ success: true, ip, country, ray }), {
headers
});
} catch {
return new Response(JSON.stringify({ success: false }), { headers });
}
};

export default {
async fetch(request: Request) {
return await handleRequest(request);
}
};
6 changes: 6 additions & 0 deletions packages/workers/ipinfo/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"extends": "tsconfig/base.json",
"compilerOptions": {
"types": ["@cloudflare/workers-types"]
}
}
7 changes: 7 additions & 0 deletions packages/workers/ipinfo/wrangler.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
name = "ipinfo"
main = "src/index.ts"
compatibility_date = "2023-01-25"

routes = [
{ pattern = "ipinfo.lenster.xyz", custom_domain = true }
]
21 changes: 21 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading