XWeb 框架是一款基于 Python 语言的 Web 开发框架。
我曾经使用过Java、PHP、Ruby和Python来开发Web应用,至于为什么使用Python作为XWEB的语言,其实最主要的原因:
马丁福勒的《企业应用架构模式》一书
- user = User.createByBiz()
- order = Order.createByBiz()
- order.user_id = user.id
如果使用Django的ORM,要写出: user = User() user.save() order = Order() order.user_id = user.id order.save()
从cache和从库中取出的数据是不允许写入主库
本框架有一个ORM框架和一个MVC框架构成,其主要设计思想如下:
- /domain - 领域文件夹
- /entity - 领域实体类
- /object - 非领域实体类
- /service - 领域服务类
- /www - www子App
- /controller - 控制器类
- /templates - 模板文件夹
- /static - 静态文件
- /admin - admin子App
- /controller
- /templates
- /config - 配置文件目录
-
App: 以域名划分,例如,一个项目有前台,后台,图片等服务,分别为www.xxx.com, admin.xxx.com和img.xxx.com,则分别对应www,admin和img的app
Controller: 以业务需求划分,例如:用户相关操作,订单操作分别属于UserController和OrderController,需要登录的在Controller的基类中实现或者在before方法中实现
Action: 一次具体的请求,如:下单,可标示为: controller/action,此为默认地址,rewrite规则书写方式为: c=controller&a=action。
- from xweb import App
-
- app = App()
-
- @app.use
- async def response(ctx):
- ctx.res.body = "Hello World"
-
- if __name__ == '__main__':
- app.listen(8000)
- import time
- from xweb import App
-
- app = App()
-
- @app.use
- async def logger(ctx, fn):
- await fn()
- rt = ctx['X-Response-Time']
- print(rt)
-
- @app.use
- async def response_time(ctx, fn):
- start = time.time()
- await fn()
- usage = (time.time() - start) * 1000_000
- ctx['X-Response-Time'] = f'{usage:.0f}µs'
-
- @app.use
- async def response(ctx):
- ctx.res.body = "Hello World"
-
- if __name__ == '__main__':
- app.listen(8000)
-
Requests/Sec: