-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
173 lines (123 loc) · 3.82 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
import os
import sys
import random
import logging
from typing import Text
from typing import Tuple
from abc import ABC, abstractmethod
from urllib.parse import urlparse
from string import ascii_letters, digits
import redis
from flask import Flask, request, redirect
ROOT_URL = os.getenv('APP_URL') or 'http://127.0.0.1:5005/'
ENV = os.getenv('APP_ENV') or 'local'
app = Flask(__name__)
log = logging.getLogger(__name__)
log.setLevel(logging.INFO)
handler = logging.StreamHandler(sys.stdout)
log.addHandler(handler)
class URLInvalidError(Exception):
pass
class DataStore:
def __init__(self, backend):
self.backend = backend
def set(self, key, value) -> None:
self.backend.set(key, value)
def get(self, key) -> Text:
return self.backend.get(key)
def inc(self) -> int:
return self.backend.inc()
def __str__(self) -> Text:
return str(self.backend)
class Backend(ABC):
@abstractmethod
def set(self, key, value) -> None:
pass
@abstractmethod
def get(self, key) -> Text:
pass
@abstractmethod
def inc(self) -> int:
pass
class RedisBackend(Backend):
def __init__(self, url=None):
if url is None:
url = os.getenv('REDIS_URL')
self.db = redis.Redis.from_url(url)
# A key that will be used as a counter
self.counter = 'COUNTER'
def set(self, key, value) -> None:
self.db.set(key, value)
def get(self, key) -> Text:
return self.db.get(key)
def inc(self) -> int:
return self.db.incr(self.counter)
class InMemoryBackend(Backend):
def __init__(self):
self.db = {}
self.counter = 0
def set(self, key, value) -> None:
self.db[key] = value
def get(self, key) -> Text:
return self.db.get(key)
def inc(self) -> int:
self.counter += 1
return self.counter
def __str__(self) -> Text:
return str({'store': self.db, 'counter': self.counter})
class Encoder:
CHARSET = [char for char in digits + ascii_letters]
BASE = len(CHARSET)
@classmethod
def encode(cls, number) -> Text:
"""
Encode number in base62
"""
string = ''
while(number > 0):
string = cls.CHARSET[number % cls.BASE] + string
number //= cls.BASE
return string
@classmethod
def decode(cls, shortcode) -> int:
"""
Decode base62 number to base 10 number
"""
number = 0
for index, char in enumerate(shortcode[::-1]):
number += cls.CHARSET.index(char) * cls.BASE ** index
return number
def _get_store() -> DataStore:
if ENV == 'local':
return DataStore(InMemoryBackend())
elif ENV == 'prod':
return DataStore(RedisBackend())
raise RuntimeError("enviroment {ENV} is not supported")
STORE = _get_store()
@app.route('/shorten', methods=['POST'])
def create() -> Tuple[Text, int]:
try:
# TODO escape JS in url
url = request.json.get('url')
result = urlparse(url)
if not all([result.scheme, result.netloc]):
raise URLInvalidError
except (AttributeError, URLInvalidError):
message = "Could not parse URL"
log.exception(message)
return message, 400
incremented_counter = STORE.inc()
encoded_counter = Encoder.encode(incremented_counter)
encoded_randomized = f"{encoded_counter}{random.randint(1,100)}"
STORE.set(encoded_randomized, url)
if ENV == 'local':
log.info(str(STORE))
return ROOT_URL + encoded_randomized, 201
@app.route('/<shortcode>')
def get(shortcode) -> Tuple[Text, int]:
long_url = STORE.get(shortcode)
if long_url is None:
return "Not found", 404
return redirect(long_url)
if __name__ == '__main__':
app.run(debug=True, port=5005)