def jsonify(**kwargs): content = json.dumps(kwargs) response = FanteWeb.Response() response.content_type = "application/json" # response.status_code = status response.body = "{}".format(content).encode() return response
def wrapper(ctx, request:FanteWeb): try: jwtstr = request.headers.get('Jwt') payload = jwt.decode(jwtstr, AUTH_SECRET, algorithms=['HS256']) # 时间问题,过期 if (datetime.datetime.now().timestamp() - payload.get('timestamp', 0)) > AUTH_EXPIRE: raise exc.HTTPUnauthorized() user_id = payload.get('user_id', -1) user = session.query(User).filter(User.id == user_id).first() if not user: raise exc.HTTPUnauthorized() request.user = user except Exception as e: raise exc.HTTPUnauthorized() return handler(ctx, request)
def index(request:FanteWeb.Request): res = FanteWeb.Response() res.body = "<h1>爱学习的fante写Python {}</h1>".format(request.vars.id).encode() return res
def index(request:FanteWeb.Request): print(request) res = FanteWeb.Response() res.body = "<h1>爱学习的fante</h1>".encode() return res
from wsgiref.simple_server import make_server from fanteweb import FanteWeb, jsonify idx = FanteWeb.Router() py = FanteWeb.Router('/python') FanteWeb.register(idx) FanteWeb.register(py) @idx.get('^/$') def index(request:FanteWeb.Request): print(request) res = FanteWeb.Response() res.body = "<h1>爱学习的fante</h1>".encode() return res @py.get('/{name:str}/{id:int}') def index(request:FanteWeb.Request): res = FanteWeb.Response() res.body = "<h1>爱学习的fante写Python {}</h1>".format(request.vars.id).encode() return res # 定义一个拦截器 @idx.reg_postinterceptor def showjson(ctx, request, response): body = response.body.decode() return jsonify(body=body) if __name__ == "__main__": ip = "127.0.0.1" port = 9999
# User 操作 import pymysql from fanteweb import FanteWeb import jwt import bcrypt from webob import exc from ..config import AUTH_EXPIRE, AUTH_SECRET from ..util import jsonify from ..model import session, User import datetime user_router = FanteWeb.Router('/user') def gen_token(user_id): return jwt.encode({ 'user_id':user_id, 'timestamp': int(datetime.datetime.now().timestamp()) }, AUTH_SECRET, 'HS256').decode() @user_router.post('/reg') def reg(ctx, request:FanteWeb.Request): payload = request.json print(payload, type(payload)) email = payload.get('email') query = session.query(User).filter(User.email == email).first() if query: raise exc.HTTPConflict() user = User() try:
from fanteweb import FanteWeb from .user import authenticate from webob import exc from ..util import jsonify, validate from ..model import Post, Content, session, Dig, Tag, Post_tag import datetime, math, re post_router = FanteWeb.Router('/post') @post_router.post('/') @authenticate def pub(ctx, request: FanteWeb.Request): payload = request.json try: title = payload['title'] tags = re.split('[\s,]+', payload.get('tags', '')) c = payload['content'] except Exception as e: raise exc.HTTPBadRequest() post = Post() post.author_id = request.user.id post.title = title post.postdate = datetime.datetime.now() content = Content() content.content = c post.content = content session.add(post)
from blog import config from fanteweb import FanteWeb from blog.handler.post import post_router from blog.handler.user import user_router if __name__ == '__main__': application = FanteWeb() # 路由注册 application.register(user_router) application.register(post_router) from wsgiref import simple_server server = simple_server.make_server(config.WSIP, config.WSPORT, application) try: server.serve_forever() except KeyboardInterrupt: server.shutdown() server.server_close()