mirror of
https://github.com/linkedin/school-of-sre
synced 2026-01-08 17:48:02 +00:00
1047 lines
25 KiB
HTML
1047 lines
25 KiB
HTML
|
|
<!doctype html>
|
|
<html lang="en" class="no-js">
|
|
<head>
|
|
|
|
<meta charset="utf-8">
|
|
<meta name="viewport" content="width=device-width,initial-scale=1">
|
|
|
|
|
|
|
|
|
|
<link rel="shortcut icon" href="../../assets/images/favicon.png">
|
|
<meta name="generator" content="mkdocs-1.1.2, mkdocs-material-6.1.5">
|
|
|
|
|
|
|
|
<title>The URL Shortening App - SchoolOfSRE</title>
|
|
|
|
|
|
|
|
<link rel="stylesheet" href="../../assets/stylesheets/main.21aed14c.min.css">
|
|
|
|
|
|
<link rel="stylesheet" href="../../assets/stylesheets/palette.196e0c26.min.css">
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
<link href="https://fonts.gstatic.com" rel="preconnect" crossorigin>
|
|
<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Roboto:300,400,400i,700%7CRoboto+Mono&display=fallback">
|
|
<style>body,input{font-family:"Roboto",-apple-system,BlinkMacSystemFont,Helvetica,Arial,sans-serif}code,kbd,pre{font-family:"Roboto Mono",SFMono-Regular,Consolas,Menlo,monospace}</style>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
</head>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
<body dir="ltr" data-md-color-scheme="" data-md-color-primary="none" data-md-color-accent="none">
|
|
|
|
|
|
<input class="md-toggle" data-md-toggle="drawer" type="checkbox" id="__drawer" autocomplete="off">
|
|
<input class="md-toggle" data-md-toggle="search" type="checkbox" id="__search" autocomplete="off">
|
|
<label class="md-overlay" for="__drawer"></label>
|
|
<div data-md-component="skip">
|
|
|
|
|
|
<a href="#the-url-shortening-app" class="md-skip">
|
|
Skip to content
|
|
</a>
|
|
|
|
</div>
|
|
<div data-md-component="announce">
|
|
|
|
</div>
|
|
|
|
|
|
|
|
<header class="md-header" data-md-component="header">
|
|
<nav class="md-header-nav md-grid" aria-label="Header">
|
|
<a href="../.." title="SchoolOfSRE" class="md-header-nav__button md-logo" aria-label="SchoolOfSRE">
|
|
|
|
<img src="../../img/sos.png" alt="logo">
|
|
|
|
</a>
|
|
<label class="md-header-nav__button md-icon" for="__drawer">
|
|
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M3 6h18v2H3V6m0 5h18v2H3v-2m0 5h18v2H3v-2z"/></svg>
|
|
</label>
|
|
<div class="md-header-nav__title" data-md-component="header-title">
|
|
|
|
<div class="md-header-nav__ellipsis">
|
|
<span class="md-header-nav__topic md-ellipsis">
|
|
SchoolOfSRE
|
|
</span>
|
|
<span class="md-header-nav__topic md-ellipsis">
|
|
|
|
The URL Shortening App
|
|
|
|
</span>
|
|
</div>
|
|
|
|
</div>
|
|
|
|
|
|
</nav>
|
|
</header>
|
|
|
|
<div class="md-container" data-md-component="container">
|
|
|
|
|
|
|
|
|
|
<main class="md-main" data-md-component="main">
|
|
<div class="md-main__inner md-grid">
|
|
|
|
|
|
<div class="md-sidebar md-sidebar--primary" data-md-component="navigation">
|
|
<div class="md-sidebar__scrollwrap">
|
|
<div class="md-sidebar__inner">
|
|
|
|
|
|
<nav class="md-nav md-nav--primary" aria-label="Navigation" data-md-level="0">
|
|
<label class="md-nav__title" for="__drawer">
|
|
<a href="../.." title="SchoolOfSRE" class="md-nav__button md-logo" aria-label="SchoolOfSRE">
|
|
|
|
<img src="../../img/sos.png" alt="logo">
|
|
|
|
</a>
|
|
SchoolOfSRE
|
|
</label>
|
|
|
|
<ul class="md-nav__list" data-md-scrollfix>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
<li class="md-nav__item">
|
|
<a href="../.." class="md-nav__link">
|
|
Home
|
|
</a>
|
|
</li>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
<li class="md-nav__item md-nav__item--nested">
|
|
|
|
<input class="md-nav__toggle md-toggle" data-md-toggle="nav-2" type="checkbox" id="nav-2" >
|
|
<label class="md-nav__link" for="nav-2">
|
|
Fundamentals Series
|
|
<span class="md-nav__icon md-icon"></span>
|
|
</label>
|
|
<nav class="md-nav" aria-label="Fundamentals Series" data-md-level="1">
|
|
<label class="md-nav__title" for="nav-2">
|
|
<span class="md-nav__icon md-icon"></span>
|
|
Fundamentals Series
|
|
</label>
|
|
<ul class="md-nav__list" data-md-scrollfix>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
<li class="md-nav__item md-nav__item--nested">
|
|
|
|
<input class="md-nav__toggle md-toggle" data-md-toggle="nav-2-1" type="checkbox" id="nav-2-1" >
|
|
<label class="md-nav__link" for="nav-2-1">
|
|
Git
|
|
<span class="md-nav__icon md-icon"></span>
|
|
</label>
|
|
<nav class="md-nav" aria-label="Git" data-md-level="2">
|
|
<label class="md-nav__title" for="nav-2-1">
|
|
<span class="md-nav__icon md-icon"></span>
|
|
Git
|
|
</label>
|
|
<ul class="md-nav__list" data-md-scrollfix>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
<li class="md-nav__item">
|
|
<a href="../../git/git-basics/" class="md-nav__link">
|
|
Git Basics
|
|
</a>
|
|
</li>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
<li class="md-nav__item">
|
|
<a href="../../git/branches/" class="md-nav__link">
|
|
Working With Branches
|
|
</a>
|
|
</li>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
<li class="md-nav__item">
|
|
<a href="../../git/github-hooks/" class="md-nav__link">
|
|
Github and Hooks
|
|
</a>
|
|
</li>
|
|
|
|
|
|
</ul>
|
|
</nav>
|
|
</li>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
<li class="md-nav__item md-nav__item--nested">
|
|
|
|
<input class="md-nav__toggle md-toggle" data-md-toggle="nav-2-2" type="checkbox" id="nav-2-2" >
|
|
<label class="md-nav__link" for="nav-2-2">
|
|
Linux Networking
|
|
<span class="md-nav__icon md-icon"></span>
|
|
</label>
|
|
<nav class="md-nav" aria-label="Linux Networking" data-md-level="2">
|
|
<label class="md-nav__title" for="nav-2-2">
|
|
<span class="md-nav__icon md-icon"></span>
|
|
Linux Networking
|
|
</label>
|
|
<ul class="md-nav__list" data-md-scrollfix>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
<li class="md-nav__item">
|
|
<a href="../../linux_networking/intro/" class="md-nav__link">
|
|
Introduction
|
|
</a>
|
|
</li>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
<li class="md-nav__item">
|
|
<a href="../../linux_networking/dns/" class="md-nav__link">
|
|
DNS
|
|
</a>
|
|
</li>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
<li class="md-nav__item">
|
|
<a href="../../linux_networking/udp/" class="md-nav__link">
|
|
UDP
|
|
</a>
|
|
</li>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
<li class="md-nav__item">
|
|
<a href="../../linux_networking/http/" class="md-nav__link">
|
|
HTTP
|
|
</a>
|
|
</li>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
<li class="md-nav__item">
|
|
<a href="../../linux_networking/tcp/" class="md-nav__link">
|
|
TCP
|
|
</a>
|
|
</li>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
<li class="md-nav__item">
|
|
<a href="../../linux_networking/ipr/" class="md-nav__link">
|
|
Routing and Conclusion
|
|
</a>
|
|
</li>
|
|
|
|
|
|
</ul>
|
|
</nav>
|
|
</li>
|
|
|
|
|
|
</ul>
|
|
</nav>
|
|
</li>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
<li class="md-nav__item md-nav__item--active md-nav__item--nested">
|
|
|
|
<input class="md-nav__toggle md-toggle" data-md-toggle="nav-3" type="checkbox" id="nav-3" checked>
|
|
<label class="md-nav__link" for="nav-3">
|
|
Python and Web
|
|
<span class="md-nav__icon md-icon"></span>
|
|
</label>
|
|
<nav class="md-nav" aria-label="Python and Web" data-md-level="1">
|
|
<label class="md-nav__title" for="nav-3">
|
|
<span class="md-nav__icon md-icon"></span>
|
|
Python and Web
|
|
</label>
|
|
<ul class="md-nav__list" data-md-scrollfix>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
<li class="md-nav__item">
|
|
<a href="../intro/" class="md-nav__link">
|
|
Introduction
|
|
</a>
|
|
</li>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
<li class="md-nav__item">
|
|
<a href="../python-concepts/" class="md-nav__link">
|
|
Some Python Concepts
|
|
</a>
|
|
</li>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
<li class="md-nav__item">
|
|
<a href="../python-web-flask/" class="md-nav__link">
|
|
Python, Web and Flask
|
|
</a>
|
|
</li>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
<li class="md-nav__item md-nav__item--active">
|
|
|
|
<input class="md-nav__toggle md-toggle" data-md-toggle="toc" type="checkbox" id="__toc">
|
|
|
|
|
|
|
|
|
|
<label class="md-nav__link md-nav__link--active" for="__toc">
|
|
The URL Shortening App
|
|
<span class="md-nav__icon md-icon"></span>
|
|
</label>
|
|
|
|
<a href="./" class="md-nav__link md-nav__link--active">
|
|
The URL Shortening App
|
|
</a>
|
|
|
|
|
|
<nav class="md-nav md-nav--secondary" aria-label="Table of contents">
|
|
|
|
|
|
|
|
|
|
|
|
<label class="md-nav__title" for="__toc">
|
|
<span class="md-nav__icon md-icon"></span>
|
|
Table of contents
|
|
</label>
|
|
<ul class="md-nav__list" data-md-scrollfix>
|
|
|
|
<li class="md-nav__item">
|
|
<a href="#design" class="md-nav__link">
|
|
Design
|
|
</a>
|
|
|
|
<nav class="md-nav" aria-label="Design">
|
|
<ul class="md-nav__list">
|
|
|
|
<li class="md-nav__item">
|
|
<a href="#1-high-level-operations-and-api-endpoints" class="md-nav__link">
|
|
1. High Level Operations and API Endpoints
|
|
</a>
|
|
|
|
</li>
|
|
|
|
<li class="md-nav__item">
|
|
<a href="#2-how-to-shorten" class="md-nav__link">
|
|
2. How to shorten?
|
|
</a>
|
|
|
|
</li>
|
|
|
|
<li class="md-nav__item">
|
|
<a href="#3-is-url-valid" class="md-nav__link">
|
|
3. Is URL Valid?
|
|
</a>
|
|
|
|
</li>
|
|
|
|
<li class="md-nav__item">
|
|
<a href="#4-storage" class="md-nav__link">
|
|
4. Storage
|
|
</a>
|
|
|
|
</li>
|
|
|
|
<li class="md-nav__item">
|
|
<a href="#5-other" class="md-nav__link">
|
|
5. Other
|
|
</a>
|
|
|
|
</li>
|
|
|
|
</ul>
|
|
</nav>
|
|
|
|
</li>
|
|
|
|
</ul>
|
|
|
|
</nav>
|
|
|
|
</li>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
<li class="md-nav__item">
|
|
<a href="../sre-conclusion/" class="md-nav__link">
|
|
SRE Aspects of The App and Conclusion
|
|
</a>
|
|
</li>
|
|
|
|
|
|
</ul>
|
|
</nav>
|
|
</li>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
<li class="md-nav__item md-nav__item--nested">
|
|
|
|
<input class="md-nav__toggle md-toggle" data-md-toggle="nav-4" type="checkbox" id="nav-4" >
|
|
<label class="md-nav__link" for="nav-4">
|
|
Systems Design
|
|
<span class="md-nav__icon md-icon"></span>
|
|
</label>
|
|
<nav class="md-nav" aria-label="Systems Design" data-md-level="1">
|
|
<label class="md-nav__title" for="nav-4">
|
|
<span class="md-nav__icon md-icon"></span>
|
|
Systems Design
|
|
</label>
|
|
<ul class="md-nav__list" data-md-scrollfix>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
<li class="md-nav__item">
|
|
<a href="../../systems_design/intro/" class="md-nav__link">
|
|
Introduction
|
|
</a>
|
|
</li>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
<li class="md-nav__item">
|
|
<a href="../../systems_design/scalability/" class="md-nav__link">
|
|
Scalability
|
|
</a>
|
|
</li>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
<li class="md-nav__item">
|
|
<a href="../../systems_design/availability/" class="md-nav__link">
|
|
Availability
|
|
</a>
|
|
</li>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
<li class="md-nav__item">
|
|
<a href="../../systems_design/fault-tolerance/" class="md-nav__link">
|
|
Fault Tolerance
|
|
</a>
|
|
</li>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
<li class="md-nav__item">
|
|
<a href="../../systems_design/conclusion/" class="md-nav__link">
|
|
Conclusion
|
|
</a>
|
|
</li>
|
|
|
|
|
|
</ul>
|
|
</nav>
|
|
</li>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
<li class="md-nav__item md-nav__item--nested">
|
|
|
|
<input class="md-nav__toggle md-toggle" data-md-toggle="nav-5" type="checkbox" id="nav-5" >
|
|
<label class="md-nav__link" for="nav-5">
|
|
Data
|
|
<span class="md-nav__icon md-icon"></span>
|
|
</label>
|
|
<nav class="md-nav" aria-label="Data" data-md-level="1">
|
|
<label class="md-nav__title" for="nav-5">
|
|
<span class="md-nav__icon md-icon"></span>
|
|
Data
|
|
</label>
|
|
<ul class="md-nav__list" data-md-scrollfix>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
<li class="md-nav__item md-nav__item--nested">
|
|
|
|
<input class="md-nav__toggle md-toggle" data-md-toggle="nav-5-1" type="checkbox" id="nav-5-1" >
|
|
<label class="md-nav__link" for="nav-5-1">
|
|
Big Data
|
|
<span class="md-nav__icon md-icon"></span>
|
|
</label>
|
|
<nav class="md-nav" aria-label="Big Data" data-md-level="2">
|
|
<label class="md-nav__title" for="nav-5-1">
|
|
<span class="md-nav__icon md-icon"></span>
|
|
Big Data
|
|
</label>
|
|
<ul class="md-nav__list" data-md-scrollfix>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
<li class="md-nav__item">
|
|
<a href="../../big_data/intro/" class="md-nav__link">
|
|
Introduction
|
|
</a>
|
|
</li>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
<li class="md-nav__item">
|
|
<a href="../../big_data/overview/" class="md-nav__link">
|
|
Overview of Big Data
|
|
</a>
|
|
</li>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
<li class="md-nav__item">
|
|
<a href="../../big_data/usage/" class="md-nav__link">
|
|
Usage of Big Data techniques
|
|
</a>
|
|
</li>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
<li class="md-nav__item">
|
|
<a href="../../big_data/evolution/" class="md-nav__link">
|
|
Evolution of Hadoop
|
|
</a>
|
|
</li>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
<li class="md-nav__item">
|
|
<a href="../../big_data/architecture/" class="md-nav__link">
|
|
Architecture of Hadoop
|
|
</a>
|
|
</li>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
<li class="md-nav__item">
|
|
<a href="../../big_data/tasks/" class="md-nav__link">
|
|
Tasks and conclusion
|
|
</a>
|
|
</li>
|
|
|
|
|
|
</ul>
|
|
</nav>
|
|
</li>
|
|
|
|
|
|
</ul>
|
|
</nav>
|
|
</li>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
<li class="md-nav__item md-nav__item--nested">
|
|
|
|
<input class="md-nav__toggle md-toggle" data-md-toggle="nav-6" type="checkbox" id="nav-6" >
|
|
<label class="md-nav__link" for="nav-6">
|
|
Security
|
|
<span class="md-nav__icon md-icon"></span>
|
|
</label>
|
|
<nav class="md-nav" aria-label="Security" data-md-level="1">
|
|
<label class="md-nav__title" for="nav-6">
|
|
<span class="md-nav__icon md-icon"></span>
|
|
Security
|
|
</label>
|
|
<ul class="md-nav__list" data-md-scrollfix>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
<li class="md-nav__item">
|
|
<a href="../../security/intro/" class="md-nav__link">
|
|
Introduction
|
|
</a>
|
|
</li>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
<li class="md-nav__item">
|
|
<a href="../../security/fundamentals/" class="md-nav__link">
|
|
Fundamentals of Security
|
|
</a>
|
|
</li>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
<li class="md-nav__item">
|
|
<a href="../../security/network_security/" class="md-nav__link">
|
|
Network Security
|
|
</a>
|
|
</li>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
<li class="md-nav__item">
|
|
<a href="../../security/threats_attacks_defences/" class="md-nav__link">
|
|
Threat, Attacks & Defences
|
|
</a>
|
|
</li>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
<li class="md-nav__item">
|
|
<a href="../../security/writing_secure_code/" class="md-nav__link">
|
|
Writing Secure code
|
|
</a>
|
|
</li>
|
|
|
|
|
|
</ul>
|
|
</nav>
|
|
</li>
|
|
|
|
|
|
</ul>
|
|
</nav>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
|
|
<div class="md-sidebar md-sidebar--secondary" data-md-component="toc">
|
|
<div class="md-sidebar__scrollwrap">
|
|
<div class="md-sidebar__inner">
|
|
|
|
<nav class="md-nav md-nav--secondary" aria-label="Table of contents">
|
|
|
|
|
|
|
|
|
|
|
|
<label class="md-nav__title" for="__toc">
|
|
<span class="md-nav__icon md-icon"></span>
|
|
Table of contents
|
|
</label>
|
|
<ul class="md-nav__list" data-md-scrollfix>
|
|
|
|
<li class="md-nav__item">
|
|
<a href="#design" class="md-nav__link">
|
|
Design
|
|
</a>
|
|
|
|
<nav class="md-nav" aria-label="Design">
|
|
<ul class="md-nav__list">
|
|
|
|
<li class="md-nav__item">
|
|
<a href="#1-high-level-operations-and-api-endpoints" class="md-nav__link">
|
|
1. High Level Operations and API Endpoints
|
|
</a>
|
|
|
|
</li>
|
|
|
|
<li class="md-nav__item">
|
|
<a href="#2-how-to-shorten" class="md-nav__link">
|
|
2. How to shorten?
|
|
</a>
|
|
|
|
</li>
|
|
|
|
<li class="md-nav__item">
|
|
<a href="#3-is-url-valid" class="md-nav__link">
|
|
3. Is URL Valid?
|
|
</a>
|
|
|
|
</li>
|
|
|
|
<li class="md-nav__item">
|
|
<a href="#4-storage" class="md-nav__link">
|
|
4. Storage
|
|
</a>
|
|
|
|
</li>
|
|
|
|
<li class="md-nav__item">
|
|
<a href="#5-other" class="md-nav__link">
|
|
5. Other
|
|
</a>
|
|
|
|
</li>
|
|
|
|
</ul>
|
|
</nav>
|
|
|
|
</li>
|
|
|
|
</ul>
|
|
|
|
</nav>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
|
|
<div class="md-content">
|
|
<article class="md-content__inner md-typeset">
|
|
|
|
|
|
|
|
<h1 id="the-url-shortening-app">The URL Shortening App</h1>
|
|
<p>Let's build a very simple URL shortening app using flask and try to incorporate all aspects of the development process including the reliability aspects. We will not be building the UI and we will come up with a minimal set of API that will be enough for the app to function well.</p>
|
|
<h2 id="design">Design</h2>
|
|
<p>We don't jump directly to coding. First thing we do is gather requirements. Come up with an approach. Have the approach/design reviewed by peers. Evolve, iterate, document the decisions and tradeoffs. And then finally implement. While we will not do the full blown design document here, we will raise certain questions here that are important to the design.</p>
|
|
<h3 id="1-high-level-operations-and-api-endpoints">1. High Level Operations and API Endpoints</h3>
|
|
<p>Since it's a URL shortening app, we will need an API for generating the shorten link given an original link. And an API/Endpoint which will accept the shorten link and redirect to original URL. We are not including the user aspect of the app to keep things minimal. These two API should make app functional and usable by anyone.</p>
|
|
<h3 id="2-how-to-shorten">2. How to shorten?</h3>
|
|
<p>Given a url, we will need to generate a shortened version of it. One approach could be using random characters for each link. Another thing that can be done is to use some sort of hashing algorithm. The benefit here is we will reuse the same hash for the same link. ie: if lot of people are shortening <code>https://www.linkedin.com</code> they all will have the same value, compared to multiple entries in DB if chosen random characters.</p>
|
|
<p>What about hash collisions? Even in random characters approach, though there is a less probability, hash collisions can happen. And we need to be mindful of them. In that case we might want to prepend/append the string with some random value to avoid conflict.</p>
|
|
<p>Also, choice of hash algorithm matters. We will need to analyze algorithms. Their CPU requirements and their characteristics. Choose one that suits the most.</p>
|
|
<h3 id="3-is-url-valid">3. Is URL Valid?</h3>
|
|
<p>Given a URL to shorten, how do we verify if the URL is valid? Do we even verify or validate? One basic check that can be done is see if the URL matches a regex of a URL. To go even further we can try opening/visiting the URL. But there are certain gotchas here.</p>
|
|
<ol>
|
|
<li>We need to define success criteria. ie: HTTP 200 means it is valid.</li>
|
|
<li>What is the URL is in private network?</li>
|
|
<li>What if URL is temporarily down?</li>
|
|
</ol>
|
|
<h3 id="4-storage">4. Storage</h3>
|
|
<p>Finally, storage. Where will we store the data that we will generate over time? There are multiple database solutions available and we will need to choose the one that suits this app the most. Relational database like MySQL would be a fair choice but <strong>be sure to checkout School of SRE's database section for deeper insights into making a more informed decision.</strong></p>
|
|
<h3 id="5-other">5. Other</h3>
|
|
<p>We are not accounting for users into our app and other possible features like rate limiting, customized links etc but it will eventually come up with time. Depending on the requirements, they too might need to get incorporated.</p>
|
|
<p>The minimal working code is given below for reference but I'd encourage you to come up with your own.</p>
|
|
<pre><code class="language-python">from flask import Flask, redirect, request
|
|
|
|
from hashlib import md5
|
|
|
|
app = Flask("url_shortener")
|
|
|
|
mapping = {}
|
|
|
|
@app.route("/shorten", methods=["POST"])
|
|
def shorten():
|
|
global mapping
|
|
payload = request.json
|
|
|
|
if "url" not in payload:
|
|
return "Missing URL Parameter", 400
|
|
|
|
# TODO: check if URL is valid
|
|
|
|
hash_ = md5()
|
|
hash_.update(payload["url"].encode())
|
|
digest = hash_.hexdigest()[:5] # limiting to 5 chars. Less the limit more the chances of collission
|
|
|
|
if digest not in mapping:
|
|
mapping[digest] = payload["url"]
|
|
return f"Shortened: r/{digest}\n"
|
|
else:
|
|
# TODO: check for hash collission
|
|
return f"Already exists: r/{digest}\n"
|
|
|
|
|
|
@app.route("/r/<hash_>")
|
|
def redirect_(hash_):
|
|
if hash_ not in mapping:
|
|
return "URL Not Found", 404
|
|
return redirect(mapping[hash_])
|
|
|
|
|
|
if __name__ == "__main__":
|
|
app.run(debug=True)
|
|
|
|
"""
|
|
OUTPUT:
|
|
|
|
|
|
===> SHORTENING
|
|
|
|
spatel1-mn1:tmp spatel1$ curl localhost:5000/shorten -H "content-type: application/json" --data '{"url":"https://linkedin.com"}'
|
|
Shortened: r/a62a4
|
|
|
|
|
|
===> REDIRECTING, notice the response code 302 and the location header
|
|
|
|
spatel1-mn1:tmp spatel1$ curl localhost:5000/r/a62a4 -v
|
|
* Uses proxy env variable NO_PROXY == '127.0.0.1'
|
|
* Trying ::1...
|
|
* TCP_NODELAY set
|
|
* Connection failed
|
|
* connect to ::1 port 5000 failed: Connection refused
|
|
* Trying 127.0.0.1...
|
|
* TCP_NODELAY set
|
|
* Connected to localhost (127.0.0.1) port 5000 (#0)
|
|
> GET /r/a62a4 HTTP/1.1
|
|
> Host: localhost:5000
|
|
> User-Agent: curl/7.64.1
|
|
> Accept: */*
|
|
>
|
|
* HTTP 1.0, assume close after body
|
|
< HTTP/1.0 302 FOUND
|
|
< Content-Type: text/html; charset=utf-8
|
|
< Content-Length: 247
|
|
< Location: https://linkedin.com
|
|
< Server: Werkzeug/0.15.4 Python/3.7.7
|
|
< Date: Tue, 27 Oct 2020 09:37:12 GMT
|
|
<
|
|
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
|
|
<title>Redirecting...</title>
|
|
<h1>Redirecting...</h1>
|
|
* Closing connection 0
|
|
<p>You should be redirected automatically to target URL: <a href="https://linkedin.com">https://linkedin.com</a>. If not click the link.
|
|
"""
|
|
</code></pre>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
</article>
|
|
</div>
|
|
</div>
|
|
</main>
|
|
|
|
|
|
<footer class="md-footer">
|
|
|
|
<div class="md-footer-nav">
|
|
<nav class="md-footer-nav__inner md-grid" aria-label="Footer">
|
|
|
|
<a href="../python-web-flask/" class="md-footer-nav__link md-footer-nav__link--prev" rel="prev">
|
|
<div class="md-footer-nav__button md-icon">
|
|
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M20 11v2H8l5.5 5.5-1.42 1.42L4.16 12l7.92-7.92L13.5 5.5 8 11h12z"/></svg>
|
|
</div>
|
|
<div class="md-footer-nav__title">
|
|
<div class="md-ellipsis">
|
|
<span class="md-footer-nav__direction">
|
|
Previous
|
|
</span>
|
|
Python, Web and Flask
|
|
</div>
|
|
</div>
|
|
</a>
|
|
|
|
|
|
<a href="../sre-conclusion/" class="md-footer-nav__link md-footer-nav__link--next" rel="next">
|
|
<div class="md-footer-nav__title">
|
|
<div class="md-ellipsis">
|
|
<span class="md-footer-nav__direction">
|
|
Next
|
|
</span>
|
|
SRE Aspects of The App and Conclusion
|
|
</div>
|
|
</div>
|
|
<div class="md-footer-nav__button md-icon">
|
|
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M4 11v2h12l-5.5 5.5 1.42 1.42L19.84 12l-7.92-7.92L10.5 5.5 16 11H4z"/></svg>
|
|
</div>
|
|
</a>
|
|
|
|
</nav>
|
|
</div>
|
|
|
|
<div class="md-footer-meta md-typeset">
|
|
<div class="md-footer-meta__inner md-grid">
|
|
<div class="md-footer-copyright">
|
|
|
|
Made with
|
|
<a href="https://squidfunk.github.io/mkdocs-material/" target="_blank" rel="noopener">
|
|
Material for MkDocs
|
|
</a>
|
|
</div>
|
|
|
|
</div>
|
|
</div>
|
|
</footer>
|
|
|
|
</div>
|
|
|
|
<script src="../../assets/javascripts/vendor.7e0ee788.min.js"></script>
|
|
<script src="../../assets/javascripts/bundle.b3a72adc.min.js"></script><script id="__lang" type="application/json">{"clipboard.copy": "Copy to clipboard", "clipboard.copied": "Copied to clipboard", "search.config.lang": "en", "search.config.pipeline": "trimmer, stopWordFilter", "search.config.separator": "[\\s\\-]+", "search.placeholder": "Search", "search.result.placeholder": "Type to start searching", "search.result.none": "No matching documents", "search.result.one": "1 matching document", "search.result.other": "# matching documents", "search.result.more.one": "1 more on this page", "search.result.more.other": "# more on this page", "search.result.term.missing": "Missing"}</script>
|
|
|
|
<script>
|
|
app = initialize({
|
|
base: "../..",
|
|
features: [],
|
|
search: Object.assign({
|
|
worker: "../../assets/javascripts/worker/search.4ac00218.min.js"
|
|
}, typeof search !== "undefined" && search)
|
|
})
|
|
</script>
|
|
|
|
|
|
</body>
|
|
</html> |