diff --git a/shortUrl.py b/curl.py similarity index 56% rename from shortUrl.py rename to curl.py index d9ded1f..b12fb87 100644 --- a/shortUrl.py +++ b/curl.py @@ -1,23 +1,36 @@ from flask import Flask, escape, url_for, request, render_template, redirect, abort, send_from_directory from lib.functions import manageInsert, manageRequest - +import os app = Flask(__name__) -if __name__ == '__main__': - app.run(host="0.0.0.0", debug=True, port="80") +# 引入跨域访问处理模块 +from flask_cors import CORS +# 取消跨域访问限制,方便本地测试 注册CORS, "/*" 允许访问所有api +CORS(app, resources=r'/*') + +@app.route('/') +def base(): + return 'Hello! Glad to serve you.' @app.route('/insert',methods=['POST']) def insert(): return manageInsert(request) +# 图标 +@app.route('/favicon.ico') +def favicon(): + return send_from_directory(os.path.join(app.root_path, 'static'),'favicon.ico', mimetype='image/vnd.microsoft.icon') + + @app.route('/') def tail(tail): res = manageRequest(tail) + print(res[0]) if(res[-1] != 200): abort(res[-1]) else: - redirect(res[0]) + return redirect(res[0]) @app.errorhandler(404) @@ -34,4 +47,7 @@ def page_not_found(e): @app.errorhandler(410) def page_not_found(e): - return render_template('410.html'), 500 \ No newline at end of file + return render_template('410.html'), 500 + +if __name__ == '__main__': + app.run(host="0.0.0.0", debug=True, port="80") \ No newline at end of file diff --git a/go.sh b/go.sh new file mode 100644 index 0000000..4299dbb --- /dev/null +++ b/go.sh @@ -0,0 +1,3 @@ +#!/bin/sh +echo start +gunicorn curl:app -c gunicorn.conf.py \ No newline at end of file diff --git a/gunicorn.conf.py b/gunicorn.conf.py new file mode 100644 index 0000000..80f8002 --- /dev/null +++ b/gunicorn.conf.py @@ -0,0 +1,8 @@ +# 并行工作线程数 +workers = 4 +bind = '0.0.0.0:80' +daemon = True +timeout = 120 +accesslog = './logs/acess.log' +errorlog = './logs/error.log' +autostart = True \ No newline at end of file diff --git a/lib/db.py b/lib/db.py index 0224228..ca9c823 100644 --- a/lib/db.py +++ b/lib/db.py @@ -3,8 +3,8 @@ from pymongo import MongoClient # 获取数据集 def col(): # 链接数据库 - conn = MongoClient('mongodb://surl:jV9cL0eW6mX4@mongo:27017/surl') - return conn.surl['surl'] + conn = MongoClient('mongodb://curl:jV9cL0eW6mX4@mongo:27017/curl') + return conn.curl['curl'] # 通过尾缀获取源地址 def getUrlByTail(tail): diff --git a/lib/functions.py b/lib/functions.py index 291d131..13029b6 100644 --- a/lib/functions.py +++ b/lib/functions.py @@ -11,7 +11,7 @@ def manageInsert(request): except Exception as e: print(e) return '数据不合法', 400 - url = data.url + url = data['url'] tail_res = getTailByUrl(url) # 如果存在对应尾缀或者数据库查询失败就直接返回 if tail_res[-1] != 102: @@ -35,9 +35,9 @@ def manageInsert(request): # 访问链接进行跳转 def manageRequest(tail): - if(str(tail).length != 5): + if(len(str(tail)) != 5): return ('参数错误', 400) url_res = getUrlByTail(tail) if(url_res[-1] != 200): return ('无该链接', 404) - return url_res[0], 200 + return url_res[0]['url'], 200 diff --git a/static/favicon.ico b/static/favicon.ico new file mode 100644 index 0000000..aea4304 Binary files /dev/null and b/static/favicon.ico differ