-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
122 lines (103 loc) · 2.98 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
import os
from flask import Flask, redirect, jsonify, request, abort
from flask_cors import CORS
from redis import Redis
import datetime as dt
import string
import random
import json
from urllib.parse import urlparse
# pratomrerk
# Update 21/02/2023
app = Flask(__name__)
CORS(app)
redis_host = os.environ.get('REDIS_HOST', 'localhost')
redis_port = os.environ.get('REDIS_PORT', 6379)
redis_db = os.environ.get('REDIS_DB', 8)
redis_password = os.environ.get('REDIS_PASSWORD', '')
BASE_URL = os.environ.get('BASE_URL', 'http://localhost:7788')
def random_string(length):
all_chars = string.digits + string.ascii_letters + string.ascii_uppercase
return ''.join(random.choice(all_chars) for m in range(length))
def connect_redis():
return Redis(host=redis_host, port=redis_port, db=redis_db, password=redis_password)
@app.route('/', methods=['GET'])
def index():
return abort(404)
@app.route('/<url_key>', methods=['GET'])
def get(url_key):
r = connect_redis()
v = r.get(url_key)
if v is not None:
v = json.loads(v)
url = v['url']
v['count'] += 1
r.set(url_key, json.dumps(v))
r.close()
return redirect(url)
else:
return jsonify({
'error': 'Not found or expired'
}), 404
@app.route('/new-url', methods=['GET'])
def new_url():
url = request.args.get('url', None)
expire = request.args.get('expire', 60*60*24*30) # 30 days by default
if url is None:
return jsonify({
'error': 'URL is required'
}), 400
# check url is valid
try:
result = urlparse(url)
if not all([result.scheme, result.netloc]):
return jsonify({
'error': 'Invalid URL'
}), 400
except ValueError:
return jsonify({
'error': 'Invalid URL'
}), 400
if expire is not None:
try:
expire = int(expire)
except ValueError:
return jsonify({
'error': 'Expire must be integer'
}), 400
r = connect_redis()
while True:
url_key = random_string(8)
if not r.get(url_key):
break
expired = int(dt.datetime.now().timestamp()) + expire
data = {
'short_url': f'{BASE_URL}/{url_key}',
'url': url,
'count': 0,
'expire': expired
}
#print(data)
r.set(url_key, json.dumps(data), ex=expire)
r.close()
data['check'] = f'{BASE_URL}/check?url-key={url_key}'
return jsonify(data)
@app.route('/check', methods=['GET'])
def check_count():
url_key = request.args.get('url-key', None)
if url_key is None:
return jsonify({
'error': 'url-key is required'
}), 400
r = connect_redis()
v = r.get(url_key)
r.close()
if v:
data = json.loads(v)
return jsonify(data)
else:
return jsonify({
'error': 'Not found or expired'
}), 404
if __name__ == '__main__':
app.run(port=7788, debug=True)