微通道web应用框架
micropath的Python项目详细描述
控制器类
micropath的核心功能是控制器。 类;使用微路径创建wsgi web应用程序 只需创建micropath.controller的子类 填充它。 micropath.controller.\u call\uuu() 方法 实现一个wsgi web应用程序接口,这意味着 controller 子类是成熟的wsgi web应用程序。那里 也是一个micropath.controller. 参数,所有子类都应确保调用 重写它,或包含 super(classname, self)。 在实施过程中)。除了这两种方法之外 方法和属性的名称以micropath开头(对于 钩子和实用程序方法以及开发人员可以重写的数据值 定制功能)或micropath(用于内部方法 以及数据值)。本文档将涉及 功能,但有关详细信息,请参阅 微通道控制器
任何web应用程序框架最重要的部分是 确定如何将请求路由到将对其执行操作的处理程序 那个请求。微路径框架依赖于 函数 micropath.path() 和 micropath.bind() –这两个函数 返回一个内部类型 元素 bind() 方法-构造web 应为应用程序。在被子下面,这创造了一棵树 对象,根在控制器上,用于遍历 请求,并且应该比其他框架快得多, 它经常遍历正则表达式列表。这些路径 然后使用 @micropath.route() decorator(用于 controller)或 @element.route() decorator(用于相对请求 返回到由micropath.path()返回的 元素 micropath.bind() ,它接受作为参数的http方法 应该触发处理程序。(注意:任何一种形式的 @route() decorator必须是helper方法中最外层的decorator 是的, @route() 必须是源代码中的第一个decorator 文件。这个decorator存储传递给它的函数在 元素 树,因此在源代码中出现在它之前的任何装饰符 在处理请求时不会调用文件,即使它们 如果通过类实例直接访问该方法,则为。)
下面的示例应该有助于阐明这些组件是如何工作的 一起。该示例是库的rest样式web api的一部分 (死树类型)用于处理订户:
class SubscriberController(micropath.Controller): @micropath.route('get') def index(self): # Return a list of subscribers ... @micropath.route('post') def create(self, json_body): # Create a new subscriber with details from the JSON body ... # Bind a subscriber ID to the next path element. Since no # name is passed to ``bind()``, it will default to the # variable name "sub_id" (this is done by the metaclass). sub_id = micropath.bind() @sub_id.route('get') def get(self, sub_id): # Return the details about a specific subscriber ... @sub_id.route('put') def update(self, sub_id, json_body): # Update the subscriber with details from the JSON body ... @sub_id.route('delete') def delete(self, sub_id): # Delete the subscriber ...
在上面的示例中,对"/"的http get请求将映射到 方法,而对"/1234"的http put请求将调用 update() 结果是 sub_id="1234" 和 json_body 对请求体调用json.loads() 的方法。也值得 指出micropath为 http options方法;如果没有处理程序方法具有http options方法 路由到它, 微路径 将返回"204无内容"响应 使用基于路由方法设置的"allow"头。为了 例如,如果我们将http选项请求发送到"/1234",则"允许" header将包含字符串"delete,get,head,put,options"。(The "head"http方法由 微路径 路由算法,因此将出现在"允许"中 任何时候"get"都是;并且总是添加"options",因为 默认实现。)
在我们的toy web api中,我们可能还希望知道给定的 订户已签出。这可能有多种方式 已处理,但为了示例的目的,我们假设 希望订阅服务器上有一个rest资源,例如"/1234/books"将 列出订阅服务器已签出的图书,"/1234/books/5678" 会得到身份证号码为"5678"的借出簿的详细信息 微路径 ,有两种实现方法;第一种方法 将以下行添加到subscribercontroller 类别:
# Bind the "books" path element after the subscriber ID books = sub_id.path() @books.route('get') def books_index(self, sub_id): # Return a list of checked-out books ... @books.route('post') def books_create(self, sub_id, json_body): # Create (check out) a book under the subscriber from the JSON # body ... # Bind a book ID to the next path element book_id = books.bind() @book_id.route('get') def book_get(self, sub_id, book_id): # Return the details about a specific book ... @book_id.route('put') def book_update(self, sub_id, book_id, json_body): # Update the book with details from the JSON body ... @book_id.route('delete') def book_delete(self, sub_id, book_id): # Delete (check in) the book from the subscriber ...
对于一个简单的api,或者一个微服务风格的api,这个方案是 很好,但是对于大型api,controller类的大小 很快就会有问题。因此, 微通道 提供 完成此任务的另一种方法是:创建一个bookcontroller类 提供图书资源的功能,然后将其安装到 subscribercontroller类似于这样:
# The path() call is given the name "books" by the metaclass; the # mount() method configures the path element to delegate requests # to that path to the BookController class. The BookController # class will be instantiated when SubscriberController is, # assuming that the __init__() method is not overridden, or that # the superclass method is called. books = sub_id.path().mount(BookController)