Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: access medium articles functionality #9

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions workers-site/data.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,23 @@ export const redirect = {
camp6: 'https://gdsc.community.dev/events/details/developer-student-clubs-vellore-institute-of-technology-presents-workshop-on-advanced-react/',
hackjudgeblog:'https://medium.com/gdg-vit/hackjudge-from-inception-to-application-af92c0221f04?source=friends_link&sk=df3f0f46ce03ecc9e00be7239aeabd85',
}

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',
onlinecollege:
'https://medium.com/gdg-vit/pandemic-online-classes-everything-else-5a483d1423e5?source=friends_link&sk=877ec26afabaab4dde97444f9bec6f5c',
mlrandomness:
'https://medium.com/gdg-vit/understanding-the-role-of-randomness-in-machine-learning-4a19632f41c4?source=friends_link&sk=4714e51d2e4c1b060d8e593c9b4c2a8f',
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',
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'
}
56 changes: 48 additions & 8 deletions workers-site/handler.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
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 =
'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) {
Expand All @@ -31,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:
Expand All @@ -63,10 +80,33 @@ async function redirectGitHub(event) {
return Response.redirect(`${GITHUB_URL}/${urlParts[1]}/issues/${urlParts[3]}`, 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)
}

// only works for android apps.
// cannot handle ios apps right now.
if (urlParts[0] == 'app') {
return Response.redirect(playstoreLink(urlParts[1]), 301)
}
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)
}