From ca2f50d1b9ed373ad87c4c8fab37c87e48246384 Mon Sep 17 00:00:00 2001 From: vishal-1408 Date: Mon, 16 Aug 2021 17:33:31 +0530 Subject: [PATCH 1/3] feat: access medium articles functionality --- workers-site/data.js | 20 ++++++++++++++++++++ workers-site/handler.js | 13 ++++++++++++- 2 files changed, 32 insertions(+), 1 deletion(-) diff --git a/workers-site/data.js b/workers-site/data.js index fcbd0a9..99f6be9 100644 --- a/workers-site/data.js +++ b/workers-site/data.js @@ -43,3 +43,23 @@ export const redirect = { backendblog:'https://medium.com/gdg-vit/which-backend-stack-should-you-learn-e4935dfe81bd?source=friends_link&sk=166633a73e76a8e7328420d594f1c798', tensorflowblog:'https://medium.com/gdg-vit/my-journey-of-becoming-a-tensorflow-certified-developer-9ba6134807b5?source=friends_link&sk=8dfe2284188ae7ab62cc5142eaf2a631', } + +export const mediumRedirect = { + dscid: + 'https://medium.com/gdg-vit/how-to-handle-imbalanced-datasets-d33d10678c8a?source=friends_link&sk=45f724429b0dc51eb2846bf05c18af9f', + minimalism: 'https://medium.com/@aroranamya5/dial-down-the-minimalism-77f8dc0d9641', + blogonlinecollege: + 'https://medium.com/gdg-vit/pandemic-online-classes-everything-else-5a483d1423e5?source=friends_link&sk=877ec26afabaab4dde97444f9bec6f5c', + mlrandomnessblog: + 'https://medium.com/gdg-vit/understanding-the-role-of-randomness-in-machine-learning-4a19632f41c4?source=friends_link&sk=4714e51d2e4c1b060d8e593c9b4c2a8f', + getstartedblog: + 'https://medium.com/gdg-vit/5-tips-to-get-you-started-developeredition-bccfc2adf9ff?source=friends_link&sk=7cb75db17e668baaced0c1815b643bdd', + notebooksmaster: + 'https://medium.com/gdg-vit/my-kaggle-journey-from-novice-to-notebooks-masters-92bb4dc51a89', + songifiedandcyanite: + 'https://medium.com/gdg-vit/songified-and-cyanite-1cdb187b4168?source=friends_link&sk=f8ea2324bdf0cde8f1194f3388ea4ce6', + searchengine:'https://medium.com/gdg-vit/a-guide-to-understanding-search-engines-b83f12132722', + notifierblog:'https://medium.com/gdg-vit/notifier-bot-be892baaf445?source=friends_link&sk=57c01e86a29f3d79c29d3ffaeb883c44', + backendblog:'https://medium.com/gdg-vit/which-backend-stack-should-you-learn-e4935dfe81bd?source=friends_link&sk=166633a73e76a8e7328420d594f1c798', + tensorflowblog:'https://medium.com/gdg-vit/my-journey-of-becoming-a-tensorflow-certified-developer-9ba6134807b5?source=friends_link&sk=8dfe2284188ae7ab62cc5142eaf2a631' +} diff --git a/workers-site/handler.js b/workers-site/handler.js index 9b4ebbd..816dfc1 100644 --- a/workers-site/handler.js +++ b/workers-site/handler.js @@ -1,6 +1,7 @@ import { getAssetFromKV } from '@cloudflare/kv-asset-handler' -import { data, redirect } from './data.js' +import { data, mediumRedirect, redirect } from './data.js' +const MEDIUM_URL = 'https://medium.com/gdg-vit/' const GITHUB_USERNAME = 'GDGVIT' const GITHUB_URL = `https://github.com/${GITHUB_USERNAME}` const PERMISSIONS_POLICY = @@ -63,6 +64,16 @@ async function redirectGitHub(event) { return Response.redirect(`${GITHUB_URL}/${urlParts[1]}/issues/${urlParts[3]}`, 301) } } + + if(urlParts[0] == 'm' && mediumRedirect[urlParts[1]]){ + return Response.redirect(mediumRedirect[urlParts[1]],301) + } + + // if the provided article name isn't valid then redirected to dscvit medium homepage + if(urlParts[0] == 'm'){ + return Response.redirect(`${MEDIUM_URL}`,301) + } + // only works for android apps. // cannot handle ios apps right now. if (urlParts[0] == 'app') { From 2898ec08707e0f0cacf8bcd136b459af20acf5f7 Mon Sep 17 00:00:00 2001 From: vishal-1408 Date: Wed, 18 Aug 2021 12:48:43 +0530 Subject: [PATCH 2/3] chore: remove blog from shortlinks of mediumRedirect --- workers-site/data.js | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/workers-site/data.js b/workers-site/data.js index 99f6be9..1601b45 100644 --- a/workers-site/data.js +++ b/workers-site/data.js @@ -48,18 +48,18 @@ export const mediumRedirect = { dscid: 'https://medium.com/gdg-vit/how-to-handle-imbalanced-datasets-d33d10678c8a?source=friends_link&sk=45f724429b0dc51eb2846bf05c18af9f', minimalism: 'https://medium.com/@aroranamya5/dial-down-the-minimalism-77f8dc0d9641', - blogonlinecollege: + onlinecollege: 'https://medium.com/gdg-vit/pandemic-online-classes-everything-else-5a483d1423e5?source=friends_link&sk=877ec26afabaab4dde97444f9bec6f5c', - mlrandomnessblog: + mlrandomness: 'https://medium.com/gdg-vit/understanding-the-role-of-randomness-in-machine-learning-4a19632f41c4?source=friends_link&sk=4714e51d2e4c1b060d8e593c9b4c2a8f', - getstartedblog: + getstarted: 'https://medium.com/gdg-vit/5-tips-to-get-you-started-developeredition-bccfc2adf9ff?source=friends_link&sk=7cb75db17e668baaced0c1815b643bdd', notebooksmaster: 'https://medium.com/gdg-vit/my-kaggle-journey-from-novice-to-notebooks-masters-92bb4dc51a89', songifiedandcyanite: 'https://medium.com/gdg-vit/songified-and-cyanite-1cdb187b4168?source=friends_link&sk=f8ea2324bdf0cde8f1194f3388ea4ce6', searchengine:'https://medium.com/gdg-vit/a-guide-to-understanding-search-engines-b83f12132722', - notifierblog:'https://medium.com/gdg-vit/notifier-bot-be892baaf445?source=friends_link&sk=57c01e86a29f3d79c29d3ffaeb883c44', - backendblog:'https://medium.com/gdg-vit/which-backend-stack-should-you-learn-e4935dfe81bd?source=friends_link&sk=166633a73e76a8e7328420d594f1c798', - tensorflowblog:'https://medium.com/gdg-vit/my-journey-of-becoming-a-tensorflow-certified-developer-9ba6134807b5?source=friends_link&sk=8dfe2284188ae7ab62cc5142eaf2a631' + notifier:'https://medium.com/gdg-vit/notifier-bot-be892baaf445?source=friends_link&sk=57c01e86a29f3d79c29d3ffaeb883c44', + backend:'https://medium.com/gdg-vit/which-backend-stack-should-you-learn-e4935dfe81bd?source=friends_link&sk=166633a73e76a8e7328420d594f1c798', + tensorflow:'https://medium.com/gdg-vit/my-journey-of-becoming-a-tensorflow-certified-developer-9ba6134807b5?source=friends_link&sk=8dfe2284188ae7ab62cc5142eaf2a631' } From 289cc1d2c5d9c64c874f6bc60327fe3e1423ef24 Mon Sep 17 00:00:00 2001 From: vishal-1408 Date: Thu, 19 Aug 2021 23:45:25 +0530 Subject: [PATCH 3/3] feature: add analytics logging, analytics access --- workers-site/handler.js | 51 ++++++++++++++++++++++++++++++++--------- 1 file changed, 40 insertions(+), 11 deletions(-) diff --git a/workers-site/handler.js b/workers-site/handler.js index 816dfc1..533a650 100644 --- a/workers-site/handler.js +++ b/workers-site/handler.js @@ -6,13 +6,14 @@ const GITHUB_USERNAME = 'GDGVIT' const GITHUB_URL = `https://github.com/${GITHUB_USERNAME}` const PERMISSIONS_POLICY = 'accelerometer=(), autoplay=(), camera=(), encrypted-media=(), geolocation=(), gyroscope=(), magnetometer=(), microphone=(), midi=(), payment=(), picture-in-picture=(), sync-xhr=(), usb=()' +const analyticsUrl = ANALYTICS_URL + 'analytics/data' -function playstoreLink(name) { +function playstoreLink (name) { return `https://play.google.com/store/apps/details?id=com.dscvit.${name}` } export async function handleRequest(event) { - return redirectGitHub(event) + return redirectRequest(event) } async function getPageFromKV(event) { @@ -32,26 +33,41 @@ async function getPageFromKV(event) { } catch (e) { try { const notFoundResponse = await getAssetFromKV(event, { - mapRequestToAsset: (req) => new Request(`${new URL(req.url).origin}/404.html`, req), + mapRequestToAsset: (req) => new Request(`${new URL(req.url).origin}/404.html`, req) }) return new Response(notFoundResponse.body, { ...notFoundResponse, - status: 404, + status: 404 }) } catch (e) {} return new Response(e.message || e.toString(), { status: 500 }) } } -async function redirectGitHub(event) { +async function redirectRequest(event) { const urlParts = event.request.url .split('?')[0] .replace(BASE_URL, '') .split('/') .map((s) => s.toLowerCase()) - if (redirect[urlParts[0]]) { - return Response.redirect(redirect[urlParts[0]], 301) + if ( + redirect[urlParts[0]] || + urlParts[0] == 'g' || + (urlParts[0] == 'm' && mediumRedirect[urlParts[1]]) || + urlParts[0] == 'app' || + urlParts[0] == 'm' + ) { + // event.waitUntil(Promise) takes in promise and executes it asynchronously even if response is sent! + event.waitUntil(makePostRequest({ shortLink: event.request.url })) } + + // redirect to analytics + if (urlParts[0] === 'analytics') return Response.redirect(analyticsUrl, 301) + + // redirect to shortlink + if (redirect[urlParts[0]]) return Response.redirect(redirect[urlParts[0]], 301) + + // redirect to github GDGVIT repos if (urlParts[0] == 'g') { switch (urlParts.length) { case 1: @@ -65,13 +81,14 @@ async function redirectGitHub(event) { } } - if(urlParts[0] == 'm' && mediumRedirect[urlParts[1]]){ - return Response.redirect(mediumRedirect[urlParts[1]],301) + // redirects to blogs + if (urlParts[0] == 'm' && mediumRedirect[urlParts[1]]) { + return Response.redirect(mediumRedirect[urlParts[1]], 301) } // if the provided article name isn't valid then redirected to dscvit medium homepage - if(urlParts[0] == 'm'){ - return Response.redirect(`${MEDIUM_URL}`,301) + if (urlParts[0] == 'm') { + return Response.redirect(`${MEDIUM_URL}`, 301) } // only works for android apps. @@ -81,3 +98,15 @@ async function redirectGitHub(event) { } return getPageFromKV(event) } + +// function posts the analytics +function makePostRequest(body) { + const init = { + body: JSON.stringify(body), + method: 'POST', + headers: { + 'content-type': 'application/json;charset=UTF-8' + } + } + return fetch(analyticsUrl, init) +}