Skip to content

Commit

Permalink
Talking to the twitter API in a worker
Browse files Browse the repository at this point in the history
  • Loading branch information
MattIPv4 committed Apr 11, 2021
1 parent 2aac385 commit 80c876f
Show file tree
Hide file tree
Showing 8 changed files with 3,704 additions and 0 deletions.
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
.idea/
node_modules/
.DS_Store
worker/
dist/
*.env
5 changes: 5 additions & 0 deletions development.env.sample
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
SENTRY_AUTH_TOKEN=
SENTRY_ORG=
SENTRY_PROJECT=
SENTRY_DSN=
TWITTER_BEARER_AUTH=
3,544 changes: 3,544 additions & 0 deletions package-lock.json

Large diffs are not rendered by default.

28 changes: 28 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{
"name": "tweets-to-discord",
"version": "1.0.0",
"description": "",
"private": true,
"main": "src/index.js",
"scripts": {
"dev": "NODE_ENV=development wrangler dev",
"publish:production": "NODE_ENV=production wrangler publish -e production"
},
"repository": {
"type": "git",
"url": "git+https://github.com/MattIPv4/tweets-to-discord.git"
},
"author": "Matt (IPv4) Cowley",
"license": "Apache-2.0",
"bugs": {
"url": "https://github.com/MattIPv4/tweets-to-discord/issues"
},
"homepage": "https://github.com/MattIPv4/tweets-to-discord#readme",
"dependencies": {
"workers-sentry": "0.0.5"
},
"devDependencies": {
"dotenv": "^8.2.0",
"webpack": "^4.46.0"
}
}
5 changes: 5 additions & 0 deletions production.env.sample
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
SENTRY_AUTH_TOKEN=
SENTRY_ORG=
SENTRY_PROJECT=
SENTRY_DSN=
TWITTER_BEARER_AUTH=
78 changes: 78 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
const WorkersSentry = require('workers-sentry/worker');

// Util to send a text response
const textResponse = content => new Response(content, {
headers: {
'Content-Type': 'text/plain',
'Cache-Control': 'no-store, no-cache, must-revalidate, proxy-revalidate',
'Expires': '0',
'Surrogate-Control': 'no-store',
},
});

// Util to send a JSON response
const jsonResponse = obj => new Response(JSON.stringify(obj), {
headers: {
'Content-Type': 'application/json',
},
});

const fetchRecentTweets = lastId => fetch(
`https://api.twitter.com/2/users/:id/tweets?limit=100${lastId ? `&since=${lastId}` : ''}`,
{ headers: { Authorization: `Bearer ${process.env.TWITTER_BEARER_AUTH}` } },
).then(req => req.json());

const mirrorLatestTweets = async () => {
const data = await fetchRecentTweets();
console.log(data);
};

// Process all requests to the worker
const handleRequest = async ({ request, wait, sentry }) => {
const url = new URL(request.url);

// Health check route
if (url.pathname === '/health') return textResponse('OK');

// Execute triggers route
if (url.pathname === '/execute') {
// Trigger each workflow in the background after
wait(mirrorLatestTweets().catch(err => sentry.captureException(err)));
return jsonResponse({});
}

// Not found
return new Response(null, { status: 404 });
};

// Register the worker listener
addEventListener('fetch', event => {
// Start Sentry
const sentry = new WorkersSentry(event, process.env.SENTRY_DSN);

// Process the event
return event.respondWith(handleRequest({
request: event.request,
wait: event.waitUntil.bind(event),
sentry,
}).catch(err => {
// Log & re-throw any errors
console.error(err);
sentry.captureException(err);
throw err;
}));
});

// Also listen for a cron trigger
addEventListener('scheduled', event => {
// Start Sentry
const sentry = new WorkersSentry(event, process.env.SENTRY_DSN);

// Process the event
return event.waitUntil(mirrorLatestTweets().catch(err => {
// Log & re-throw any errors
console.error(err);
sentry.captureException(err);
throw err;
}));
});
22 changes: 22 additions & 0 deletions webpack.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
const path = require('path');
const env = require('dotenv').config({ path: path.join(__dirname, `${process.env.NODE_ENV}.env`) });
const { DefinePlugin } = require('webpack');
const WorkersSentryWebpackPlugin = require('workers-sentry/webpack');

module.exports = {
entry: './src/index.js',
plugins: [
// Expose our environment in the worker
new DefinePlugin(Object.entries(env.parsed).reduce((obj, [ key, val ]) => {
obj[`process.env.${key}`] = JSON.stringify(val);
return obj;
}, { 'process.env.NODE_ENV': JSON.stringify(process.env.NODE_ENV) })),

// Publish source maps to Sentry on each build
new WorkersSentryWebpackPlugin(
process.env.SENTRY_AUTH_TOKEN,
process.env.SENTRY_ORG,
process.env.SENTRY_PROJECT,
),
],
};
16 changes: 16 additions & 0 deletions wrangler.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
name = "tweets-to-discord"
type = "webpack"
account_id = "fb1f542488f2441acf88ca15f3a8390d"
workers_dev = true
webpack_config = "webpack.config.js"
kv_namespaces = [
{ binding = "TWEETS_TO_DISCORD_LAST_TWEET", id = "4fc9f04e2f29425c878cf74200359002", preview_id = "4fc9f04e2f29425c878cf74200359002" }
]

[triggers]
crons = ["* * * * *"]

[env.production]
kv_namespaces = [
{ binding = "TWEETS_TO_DISCORD_LAST_TWEET", id = "e8a41596dbff45ab84f7833da8834bbb" }
]

0 comments on commit 80c876f

Please sign in to comment.