Skip to content

Instantly share code, notes, and snippets.

Ben Chartrand bcnzer

Block or report user

Report or block bcnzer

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
@bcnzer
bcnzer / worker.js
Last active Apr 18, 2019
Example of a Cloudflare Worker and Worker KV storage used to transfer/migrate traffic to alternatively infrastructure
View worker.js
addEventListener('fetch', event => {
event.passThroughOnException()
event.respondWith(handleRequest(event.request))
})
async function handleRequest(request) {
const maxBizId = await MIGRATION_SETTINGS.get('maxbizid')
const currentBusinessId = getBusinessIdFromCookie(request)
if (!maxBizId || maxBizId == 0 || !currentBusinessId) {
@bcnzer
bcnzer / workerPart4.js
Last active Nov 6, 2018
Example of a Cloudflare Worker handling Google reCAPTCHA in an "edged out" POST request
View workerPart4.js
// NOTE: all auth code has been removed for the sake of brevity. Please see part 2 of blog series
// for more info: https://liftcodeplay.com/2018/10/16/pushing-my-api-to-the-edge-part-2-authentication-and-authorization/
addEventListener('fetch', event => {
event.respondWith(handleRequest(event))
})
const genderFemale = 'Female'
const genderMale = 'Male'
/**
@bcnzer
bcnzer / workerOptions.js
Last active Nov 3, 2018
Cloudflare Worker example of how you can handle the OPTIONS verb and set some CORS details in the response header
View workerOptions.js
addEventListener('fetch', event => {
event.respondWith(handleRequest(event))
})
/**
* Entry point of the worker
*/
async function handleRequest(event) {
// Generate the CORS headers I'll have to return with requests
const corsHeaders = setCorsHeaders(new Headers())
@bcnzer
bcnzer / workerEdgedGets.js
Last active Nov 5, 2018
Sample Worker and KV code to demonstrate GETs that are purely handled on the edge - both the request AND data
View workerEdgedGets.js
// NOTE: all auth code has been removed for the sake of brevity. Please see part 2 of blog series
// for more info: https://liftcodeplay.com/2018/10/16/pushing-my-api-to-the-edge-part-2-authentication-and-authorization/
addEventListener('fetch', event => {
event.respondWith(handleRequest(event))
})
const genderFemale = 'Female'
const genderMale = 'Male'
@bcnzer
bcnzer / ipf.json
Created Nov 2, 2018
IPF (International Powerlifting Federation) divisions and weight classes, for both men and women
View ipf.json
{
"divisions": [
{
"gender": "male",
"startAge": 14,
"endAge": 18,
"dateNumber": 1,
"dateMonth": 1,
"name": "Sub-Junior",
"abbrev": "SJ"
@bcnzer
bcnzer / worker.js
Created Oct 15, 2018
Cloudflare Worker that uses Workers KV to get Authorization data. All authentication and authorization is done on the edge
View worker.js
addEventListener('fetch', event => {
event.respondWith(handleRequest(event))
})
/**
* Entry point of the worker
*/
async function handleRequest(event) {
try {
// Get the JWT
@bcnzer
bcnzer / sampleFunctions.cs
Created Oct 15, 2018
Cut down version of a Azure Functions HTTP trigger
View sampleFunctions.cs
namespace EventMgr.Functions.Events
{
[DependencyInjectionConfig(typeof(DIConfig))]
public static class Attendee
{
[FunctionName("Attendee")]
public static async Task<IActionResult> Run(
[HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", "put", "delete", Route = "attendee")]
HttpRequest request, ILogger log, ExecutionContext context,
[Inject] IAttendeeRepository attendeeRepository,
@bcnzer
bcnzer / samplePermissions.json
Created Oct 14, 2018
Sample authorization data from Auth0. This was stored in the user's app_metadata
View samplePermissions.json
{
"EventPermissions": [
{
"EventId": 6002,
"Role": "Admin",
"AllowedPermissions": [
"All"
]
},
{
@bcnzer
bcnzer / cloudflareWorkerExample.js
Created Oct 10, 2018
Example of a worker adding a header value and returning a response in a specific condition
View cloudflareWorkerExample.js
addEventListener('fetch', event => {
event.respondWith(handleRequest(event.request))
})
/**
* Fetch and log a request
* @param {Request} request
*/
async function handleRequest(request) {
var currentDateTime = new Date()
@bcnzer
bcnzer / cloudflareworker-verifyjwt.js
Last active Sep 9, 2019
Sample Cloudflare worker that gets the JWT, ensures it hasn't expired, decrypts it and returns a result
View cloudflareworker-verifyjwt.js
addEventListener('fetch', event => {
event.respondWith(handleRequest(event.request))
})
// Following code is a modified version of that found at https://blog.cloudflare.com/dronedeploy-and-cloudflare-workers/
/**
* Fetch and log a request
* @param {Request} request
*/
You can’t perform that action at this time.