finish v1

This commit is contained in:
RainSun 2020-08-05 00:28:01 +08:00
parent 2723c0de11
commit 88ae291fad
6 changed files with 37 additions and 10 deletions

View File

@ -1,23 +1,36 @@
from flask import Flask, escape, url_for, request, render_template, redirect, abort, send_from_directory from flask import Flask, escape, url_for, request, render_template, redirect, abort, send_from_directory
from lib.functions import manageInsert, manageRequest from lib.functions import manageInsert, manageRequest
import os
app = Flask(__name__) 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']) @app.route('/insert',methods=['POST'])
def insert(): def insert():
return manageInsert(request) 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('/<tail>') @app.route('/<tail>')
def tail(tail): def tail(tail):
res = manageRequest(tail) res = manageRequest(tail)
print(res[0])
if(res[-1] != 200): if(res[-1] != 200):
abort(res[-1]) abort(res[-1])
else: else:
redirect(res[0]) return redirect(res[0])
@app.errorhandler(404) @app.errorhandler(404)
@ -34,4 +47,7 @@ def page_not_found(e):
@app.errorhandler(410) @app.errorhandler(410)
def page_not_found(e): def page_not_found(e):
return render_template('410.html'), 500 return render_template('410.html'), 500
if __name__ == '__main__':
app.run(host="0.0.0.0", debug=True, port="80")

3
go.sh Normal file
View File

@ -0,0 +1,3 @@
#!/bin/sh
echo start
gunicorn curl:app -c gunicorn.conf.py

8
gunicorn.conf.py Normal file
View File

@ -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

View File

@ -3,8 +3,8 @@ from pymongo import MongoClient
# 获取数据集 # 获取数据集
def col(): def col():
# 链接数据库 # 链接数据库
conn = MongoClient('mongodb://surl:jV9cL0eW6mX4@mongo:27017/surl') conn = MongoClient('mongodb://curl:jV9cL0eW6mX4@mongo:27017/curl')
return conn.surl['surl'] return conn.curl['curl']
# 通过尾缀获取源地址 # 通过尾缀获取源地址
def getUrlByTail(tail): def getUrlByTail(tail):

View File

@ -11,7 +11,7 @@ def manageInsert(request):
except Exception as e: except Exception as e:
print(e) print(e)
return '数据不合法', 400 return '数据不合法', 400
url = data.url url = data['url']
tail_res = getTailByUrl(url) tail_res = getTailByUrl(url)
# 如果存在对应尾缀或者数据库查询失败就直接返回 # 如果存在对应尾缀或者数据库查询失败就直接返回
if tail_res[-1] != 102: if tail_res[-1] != 102:
@ -35,9 +35,9 @@ def manageInsert(request):
# 访问链接进行跳转 # 访问链接进行跳转
def manageRequest(tail): def manageRequest(tail):
if(str(tail).length != 5): if(len(str(tail)) != 5):
return ('参数错误', 400) return ('参数错误', 400)
url_res = getUrlByTail(tail) url_res = getUrlByTail(tail)
if(url_res[-1] != 200): if(url_res[-1] != 200):
return ('无该链接', 404) return ('无该链接', 404)
return url_res[0], 200 return url_res[0]['url'], 200

BIN
static/favicon.ico Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.2 KiB