Merge pull request '提供更便利的获取用户单词表的方法,以json数据格式返回' (#93) from SPM2023S-QianJunQi into Alpha-snapshot20240618

Reviewed-on: #93
Bug547-FanWenQi-Adapted
mrlan 2024-08-31 07:39:00 +08:00
commit 19ea14b38c
2 changed files with 34 additions and 0 deletions

31
app/api_service.py Normal file
View File

@ -0,0 +1,31 @@
from flask import *
from flask_httpauth import HTTPTokenAuth
from Article import load_freq_history
path_prefix = '/var/www/wordfreq/wordfreq/'
path_prefix = './' # comment this line in deployment
apiService = Blueprint('site',__name__)
auth = HTTPTokenAuth(scheme='Bearer')
tokens = {
"token": "token",
"secret-token": "lanhui" # token, username
}
@auth.verify_token
def verify_token(token):
if token in tokens:
return tokens[token]
@apiService.route('/api/mywords') # HTTPie usage: http -A bearer -a secret-token http://127.0.0.1:5000/api/mywords
@auth.login_required
def show():
username = auth.current_user()
word_freq_record = path_prefix + 'static/frequency/' + 'frequency_%s.pickle' % (username)
d = load_freq_history(word_freq_record)
return jsonify(d)

View File

@ -10,9 +10,11 @@ import Yaml
from user_service import userService
from account_service import accountService
from admin_service import adminService, ADMIN_NAME
from api_service import apiService
import os
from translate import *
app = Flask(__name__)
app.secret_key = os.urandom(32)
@ -20,6 +22,7 @@ app.secret_key = os.urandom(32)
app.register_blueprint(userService)
app.register_blueprint(accountService)
app.register_blueprint(adminService)
app.register_blueprint(apiService)
path_prefix = '/var/www/wordfreq/wordfreq/'
path_prefix = './' # comment this line in deployment