webob

# -*- coding: utf-8 -*-
from wsgiref.simple_server import make_server

import routes.middleware
import webob.dec
import webob.exc
from oslo_config import cfg
from oslo_context import context
from oslo_log import log as logging

DOMAIN = 'demo'
CONF = cfg.CONF
LOG = logging.getLogger(__name__)
logging.register_options(CONF)
logging.setup(CONF, DOMAIN)
class Controller:
    @webob.dec.wsgify
    def __call__(self, req):
        return webob.Response("Hello World!")


class Router(object):
    def __init__(self):
        self._mapper = routes.Mapper()
        self._mapper.connect('/spch',
                             controller=Controller(),
                             action='index',
                             conditions={'method': ['GET']})

        self._router = routes.middleware.RoutesMiddleware(self._dispatch, self._mapper)

    @webob.dec.wsgify
    def __call__(self, req):
        return self._router

    @staticmethod
    @webob.dec.wsgify
    def _dispatch(req):
        match = req.environ['wsgiorg.routing_args'][1]
        LOG.info("Listing backups for instance '%s'" % "fdfdfd")
        if not match:
            return webob.exc.HTTPNotFound()
        #根据连接,找到对应的控制器
        app = match['controller']
        return app


app = Router()
httpd = make_server('localhost', 8282, app)
httpd.serve_forever()

你可能感兴趣的:(webob)