diff --git a/app/api_service.py b/app/api_service.py new file mode 100644 index 0000000..fd88681 --- /dev/null +++ b/app/api_service.py @@ -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) + diff --git a/app/main.py b/app/main.py index 0c747a3..4af4456 100644 --- a/app/main.py +++ b/app/main.py @@ -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