Skip to content

Commit

Permalink
feat: add leafwatch worker (#1765)
Browse files Browse the repository at this point in the history
  • Loading branch information
bigint committed Feb 8, 2023
1 parent 875938a commit fd38a30
Show file tree
Hide file tree
Showing 9 changed files with 123 additions and 15 deletions.
15 changes: 4 additions & 11 deletions apps/web/src/lib/leafwatch.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import axios from 'axios';
import { DATADOG_TOKEN, IS_PRODUCTION, LEAFWATCH_HOST } from 'data/constants';
import { v4 as uuid } from 'uuid';
import { LEAFWATCH_WORKER_URL } from 'data/constants';

const enabled = DATADOG_TOKEN && IS_PRODUCTION;
const isBrowser = typeof window !== 'undefined';

/**
Expand All @@ -14,21 +12,16 @@ export const Leafwatch = {
localStorage.getItem('lenster.store') || JSON.stringify({ state: { profileId: null } })
);

if (isBrowser && enabled) {
axios(LEAFWATCH_HOST, {
if (isBrowser) {
axios(LEAFWATCH_WORKER_URL, {
method: 'POST',
params: {
'dd-api-key': DATADOG_TOKEN,
'dd-request-id': uuid()
},
data: {
event: name,
metadata,
profile: state.profileId,
url: location.href,
referrer: document.referrer,
userAgent: navigator.userAgent,
ddsource: 'leafwatch'
userAgent: navigator.userAgent
}
}).catch(() => {
console.error('Error while sending analytics event to Leafwatch');
Expand Down
5 changes: 1 addition & 4 deletions packages/data/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,6 @@ export const LIT_PROTOCOL_ENVIRONMENT = getEnvConfig().litProtocolEnvironment;

export const IS_MAINNET = API_URL === MAINNET_API_URL;

// Leafwatch
export const DATADOG_TOKEN = process.env.NEXT_PUBLIC_DATADOG_TOKEN ?? '';
export const LEAFWATCH_HOST = 'https://http-intake.logs.datadoghq.com/api/v2/logs';

// XMTP
export const XMTP_ENV = IS_MAINNET ? 'production' : 'dev';
export const XMTP_PREFIX = 'lens.dev/dm';
Expand Down Expand Up @@ -67,6 +63,7 @@ export const DEFAULT_OG = `${STATIC_IMAGES_URL}/og/logo.jpeg`;
export const USER_CONTENT_URL = 'https://user-content.lenster.xyz';
export const STS_TOKEN_URL = IS_PRODUCTION ? 'https://sts.lenster.xyz' : 'http://localhost:8082';
export const METADATA_WORKER_URL = IS_PRODUCTION ? 'https://metadata.lenster.xyz' : 'http://localhost:8083';
export const LEAFWATCH_WORKER_URL = IS_PRODUCTION ? 'https://leafwatch.lenster.xyz' : 'http://localhost:8084';

// Web3
export const ALCHEMY_KEY = 'HHfOFn8jsYguteTVvL0cz4g9aydrbjTV';
Expand Down
1 change: 1 addition & 0 deletions packages/leafwatch-worker/.dev.vars.example
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
DATADOG_TOKEN=""
9 changes: 9 additions & 0 deletions packages/leafwatch-worker/.eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
module.exports = {
root: true,
extends: ['weblint'],
rules: {
'import/no-anonymous-default-export': 'off',
'import/no-anonymous-default-export': 'off',
'no-use-before-define': 'off'
}
};
21 changes: 21 additions & 0 deletions packages/leafwatch-worker/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"name": "leafwatch-worker",
"version": "0.0.0",
"private": true,
"license": "GPL-3.0",
"scripts": {
"dev": "wrangler dev --port 8084 --local",
"worker:deploy": "wrangler publish",
"lint": "eslint . --ext .ts"
},
"devDependencies": {
"@cloudflare/workers-types": "^4.20230115.0",
"eslint-config-weblint": "*",
"tsconfig": "*",
"typescript": "^4.9.5",
"wrangler": "2.9.1"
},
"dependencies": {
"@cfworker/uuid": "^1.12.4"
}
}
64 changes: 64 additions & 0 deletions packages/leafwatch-worker/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import { uuid } from '@cfworker/uuid';

type EnvType = {
DATADOG_TOKEN: string;
};

export default {
async fetch(request: Request, env: EnvType) {
return await handleRequest(request, env);
}
};

const headers = {
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Headers': 'Content-Type',
'Content-Type': 'application/json'
};

const handleRequest = async (request: Request, env: EnvType) => {
if (request.method !== 'POST') {
return new Response(JSON.stringify({ success: false, message: 'Only POST requests are supported' }), {
headers
});
}

if (request.headers.get('origin') !== 'https://lenster.xyz') {
return new Response(JSON.stringify({ success: false, message: 'Origin not allowed' }), { headers });
}

const payload = await request.json();

if (!payload) {
return new Response(JSON.stringify({ success: false, message: 'No body provided' }), { headers });
}

try {
const appenedPayload = {
...payload,
ddsource: 'leafwatch',
ip: request.headers.get('cf-connecting-ip') || 'unknown'
};
const datadogRes = await fetch(
`https://http-intake.logs.datadoghq.com/api/v2/logs?dd-api-key=${
env.DATADOG_TOKEN
}&dd-request-id=${uuid()}`,
{
method: 'POST',
headers: { 'content-type': 'application/json' },
body: appenedPayload
}
);

return new Response(
JSON.stringify({
success: true,
payload: appenedPayload,
ddResponse: await datadogRes.json()
}),
{ headers }
);
} catch {
return new Response(JSON.stringify({ success: false, message: 'Something went wrong!' }), { headers });
}
};
7 changes: 7 additions & 0 deletions packages/leafwatch-worker/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"extends": "tsconfig/base.json",
"compilerOptions": {
"baseUrl": ".",
"noEmit": true
}
}
11 changes: 11 additions & 0 deletions packages/leafwatch-worker/wrangler.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
name = "leafwatch-worker"
main = "src/index.ts"
compatibility_date = "2023-01-25"
logpush = true
routes = [
{ pattern = "leafwatch.lenster.xyz", custom_domain = true }
]
keep_vars = true

[env.production.vars]
DATADOG_TOKEN = ""
5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1552,6 +1552,11 @@
"@babel/helper-validator-identifier" "^7.19.1"
to-fast-properties "^2.0.0"

"@cfworker/uuid@^1.12.4":
version "1.12.4"
resolved "https://registry.yarnpkg.com/@cfworker/uuid/-/uuid-1.12.4.tgz#178a2126ce91bfd2fb58c1a14c991502bb69e8ef"
integrity sha512-aw4lxCT4NLbGqlTScWPtPrZrg4TO6HCZCnNUAeC4yTOvkXjJGdJP6S5NCY9vtP2FruLIC+vYCvUP98fHXtF7kg==

"@cloudflare/kv-asset-handler@^0.2.0":
version "0.2.0"
resolved "https://registry.yarnpkg.com/@cloudflare/kv-asset-handler/-/kv-asset-handler-0.2.0.tgz#c9959bbd7a1c40bd7c674adae98aa8c8d0e5ca68"
Expand Down

3 comments on commit fd38a30

@vercel
Copy link

@vercel vercel bot commented on fd38a30 Feb 8, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

prerender – ./apps/prerender

prerender-git-main-lenster.vercel.app
prerender-lenster.vercel.app
prerender.lenster.xyz

@vercel
Copy link

@vercel vercel bot commented on fd38a30 Feb 8, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

web – ./apps/web

web-lenster.vercel.app
lenster.xyz
lenster.vercel.app
web-git-main-lenster.vercel.app

@hop-deploy
Copy link

@hop-deploy hop-deploy bot commented on fd38a30 Feb 8, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Deployment Status Build Logs Updated At
lenster ✅ Deployed View Logs 2023-02-08T16:56:46.649Z

Please sign in to comment.