diff --git a/server/api/__init__.py b/server/api/__init__.py index 8f72dae..fc4a687 100644 --- a/server/api/__init__.py +++ b/server/api/__init__.py @@ -1,3 +1,4 @@ +import api import models -import utils -import user \ No newline at end of file +import user +import utils \ No newline at end of file diff --git a/server/api/models.py b/server/api/models.py index f17d203..c63243f 100644 --- a/server/api/models.py +++ b/server/api/models.py @@ -1,5 +1,5 @@ from flask.ext.sqlalchemy import SQLAlchemy -import api.utils +import utils db = SQLAlchemy() @@ -17,4 +17,4 @@ class Users(db.Model): self.username = username self.username_lower = username.lower() self.email = email.lower() - self.password = api.utils.hash_password(password) \ No newline at end of file + self.password = utils.hash_password(password) \ No newline at end of file diff --git a/server/api/user.py b/server/api/user.py index e69de29..315df69 100644 --- a/server/api/user.py +++ b/server/api/user.py @@ -0,0 +1,9 @@ +from flask import Blueprint +from utils import api_wrapper + +blueprint = Blueprint("user", __name__) + +@blueprint.route("/register", methods=["POST"]) +@api_wrapper +def user_register(): + return { "success": 0, "message": "Registration is not open yet." } \ No newline at end of file diff --git a/server/api/utils.py b/server/api/utils.py index b285192..8ecbd6a 100644 --- a/server/api/utils.py +++ b/server/api/utils.py @@ -1,21 +1,43 @@ import datetime +import json import random import string +import traceback +from functools import wraps from werkzeug.security import generate_password_hash, check_password_hash +class WebException(Exception): pass + def hash_password(s): - return generate_password_hash(s) + return generate_password_hash(s) def check_password(hashed_password, try_password): - return check_password_hash(hashed_password, try_password) + return check_password_hash(hashed_password, try_password) def generate_string(length): - return "".join([random.choice(string.letters + string.digits) for x in range(length)]) + return "".join([random.choice(string.letters + string.digits) for x in range(length)]) def unix_time_millis(dt): - epoch = datetime.datetime.utcfromtimestamp(0) - return (dt - epoch).total_seconds() * 1000.0 + epoch = datetime.datetime.utcfromtimestamp(0) + return (dt - epoch).total_seconds() * 1000.0 def get_time_since_epoch(): - return unix_time_millis(datetime.datetime.now()) + return unix_time_millis(datetime.datetime.now()) + +def api_wrapper(f): + @wraps(f) + def wrapper(*args, **kwds): + web_result = {} + response = 200 + try: + web_result = f(*args, **kwds) + except WebException as error: + response = 200 + web_result = { "success": 0, "message": str(error) } + except Exception as error: + response = 200 + traceback.print_exc() + web_result = { "success": 0, "message": "Something went wrong! Please notify us about this immediately.", error: traceback.format_exc() } + return json.dumps(web_result), response, { "Content-Type": "application/json; charset=utf-8" } + return wrapper \ No newline at end of file diff --git a/server/app.py b/server/app.py index 197d279..9ee2124 100644 --- a/server/app.py +++ b/server/app.py @@ -3,12 +3,16 @@ from flask import Flask import config import json +import api + +from api.api import api as api_blueprint app = Flask(__name__) app.secret_key = config.SECRET +app.register_blueprint(api_blueprint) @app.route("/api") -def api(): +def api_main(): return json.dumps({ "success": 1, "message": "The API is online." }) if __name__ == "__main__": @@ -26,6 +30,7 @@ if __name__ == "__main__": from api.models import db db.init_app(app) db.create_all() - print db - app.run(host="0.0.0.0", port=8000) \ No newline at end of file + app.register_blueprint(api.user.blueprint, url_prefix="/api/user") + + app.run(host="0.0.0.0", port=8000) diff --git a/web/about.html b/web/about.html new file mode 100644 index 0000000..1946531 --- /dev/null +++ b/web/about.html @@ -0,0 +1,9 @@ + + +
+