finish v1
This commit is contained in:
parent
2723c0de11
commit
88ae291fad
@ -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('/<tail>')
|
||||
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
|
||||
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
3
go.sh
Normal file
@ -0,0 +1,3 @@
|
||||
#!/bin/sh
|
||||
echo start
|
||||
gunicorn curl:app -c gunicorn.conf.py
|
8
gunicorn.conf.py
Normal file
8
gunicorn.conf.py
Normal 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
|
@ -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):
|
||||
|
@ -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
|
||||
|
BIN
static/favicon.ico
Normal file
BIN
static/favicon.ico
Normal file
Binary file not shown.
After Width: | Height: | Size: 4.2 KiB |
Loading…
x
Reference in New Issue
Block a user