forked from MattIPv4/tweets-to-discord
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Talking to the twitter API in a worker
- Loading branch information
Showing
8 changed files
with
3,704 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
.idea/ | ||
node_modules/ | ||
.DS_Store | ||
worker/ | ||
dist/ | ||
*.env |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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= |
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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= |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
})); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
), | ||
], | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" } | ||
] |