使用pecan构建restful api

使用pecan可以方便的构建restful api

配置文件test.ini

[composite:hello]
use = egg:Paste#urlmap
/ = hello

[pipeline:hello]
pipeline = main

[app:main]
paste.app_factory = main:app_factory
root = main.Controller

main.py简单文件

import wsgiref.simple_server as wss
from paste import deploy
from pecan import expose, Response
import pecan

class Controller(object):
    @expose()
    def hello(self):
        return Response('Hello, World!', 202)

def build_wsgi_app():
    import os
    abspath = os.path.dirname(os.path.abspath(__file__))
    conf_path = os.path.join(abspath, "hello.ini")
    app = deploy.loadapp("config:{0}".format(conf_path), name="main")
    return app

def app_factory(global_config, **local_conf):
    return pecan.make_app(root=local_conf.get('root'))

if __name__ == "__main__":
    server = wss.make_server('', 8000, build_wsgi_app())
    server.serve_forever()

配置文件中,构建一个叫hello的composite,使用一个叫hello的pipeline。pipeline使用Controller作为控制器。

build_wsgi_app 加载配置文件。

运行server

python main.py
使用pecan构建restful api_第1张图片
Paste_Image.png

你可能感兴趣的:(使用pecan构建restful api)