-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
137 lines (110 loc) · 3.69 KB
/
main.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
import os
import json
from flask import Flask, Response, request
from flask_restful import Resource, Api
from flask_cors import CORS
from logger import LOGGER
from dbhandler import DBHandler, createDB
from tokengenerator import generate
TOKEN_LEN = os.environ.get("TOKEN_LEN", 5)
# TOKEN_LEN is pre-computed during system design phase
# 6 letters can accommodate around 15 billions URLs
# - 6 letters can produce 56 billions (56,800,235,584) requirements
# - 5 letters can produce 900 millions (916,132,832) requirements
class ShortenURL(Resource):
def __init__(self, db):
self.db = db
def post(self):
try:
req = request.get_json()
url = req.get("url")
if not url or not isinstance(url, str):
return Response(status=400)
token = generate(url, TOKEN_LEN)
if not token:
# invalid URL
return Response(status=400)
ret = self.db.insertEntry(url=url, token=token)
if ret != self.db.INSERT_OK:
LOGGER.warning((
"entry collision, "
"but return the token for accessing is OK"))
return Response(
response=json.dumps({
"token": token
}),
status=200,
mimetype="application/json"
)
except Exception as e:
LOGGER.error("Unexpected error: {}".format(e), exc_info=True)
return Response(status=500)
class GetURL(Resource):
def __init__(self, db):
self.db = db
def post(self):
try:
req = request.get_json()
token = req.get("token")
if not token or not isinstance(token, str):
return Response(status=400)
url = self.db.getURL(token)
if not url:
return Response(status=404)
return Response(
response=json.dumps({
"url": url
}),
status=200,
mimetype="application/json"
)
except Exception as e:
LOGGER.error("Unexpected error: {}".format(e), exc_info=True)
return Response(status=500)
class Redirects(Resource):
def __init__(self, db):
self.db = db
def get(self, token):
url = self.db.getURL(token)
if not url:
return Response(status=404)
headers = {
"location": url
}
return Response(
status=302,
headers=headers
)
def set_resources(app, db):
app.add_resource(ShortenURL, "/shortenURL",
resource_class_kwargs={'db': db})
app.add_resource(GetURL, "/getURL",
resource_class_kwargs={'db': db})
app.add_resource(Redirects, "/<token>",
resource_class_kwargs={'db': db})
return app
def create_app(db):
app = Flask(__name__)
api = Api(app)
CORS(app)
set_resources(api, db)
return app
def create_db():
db = DBHandler(
createDB(
mode=os.environ.get("DBMODE", "memory"),
db_host=os.environ.get("DBHOST", "127.0.0.1"),
db_port=os.environ.get("DBPORT", 27017),
cache_host=os.environ.get("CACHEHOST", "127.0.0.1"),
cache_port=os.environ.get("CACHEPORT", 6379),
)
)
# DB is a DB handler, can be an actual DB server's connection pool
# by passing some configurations.
return db
if __name__ == "__main__":
LOGGER.info("Server start...")
db = create_db()
app = create_app(db)
app.run("0.0.0.0", port=12345, debug=True)
LOGGER.info("Server shutdown...")