def build_route(): router=RouteMatcher() cfg = vertx.config() config=JsonObject(cfg).getObject("router") for route in config.getArray("routes"): controller_description = route.getString("controller") def controller_name(): return str(controller_description).split(".")[0] if not route.getString("get") is None: print("get:"+str(route)) router.get( route.getString("get"),controller_renderer(config.getString("controller-path"),controller_name(),route.getString("view"),renderer(route,controller_description),cfg["session-dir"])) elif not route.getString("post") is None: print("post:"+str(route)) router.post( route.getString("post"),controller_renderer(config.getString("controller-path"),controller_name(),route.getString("view"),renderer(route,controller_description),cfg["session-dir"])) elif not route.getString("static") is None: print("static:"+str(route)) router.get(route.getString("static"),static_files_renderer(route.getString("folder")) ) return router
import vertx from core.http import RouteMatcher server = vertx.create_http_server() route_matcher = RouteMatcher() def getUser(req): # read parameter from URI userid = req.params['userid'] req.response.end('You requested user information '+userid) def addUser(req): userid = req.params['userid'] # read body @req.data_handler def data_handler(buffer): req.response.end('i received body :'+str(buffer)) print(buffer) route_matcher.post('/user/:userid',addUser) server.request_handler(route_matcher).listen(8080,'localhost')
import vertx from core.event_bus import EventBus from core.file_system import FileSystem from core.http import RouteMatcher from server import upload from server import file_service #inicialize server = vertx.create_http_server() route_matcher = RouteMatcher() logger = vertx.logger() fs = vertx.file_system() app_config = vertx.config() #set global path_web = app_config['paths']['web'] path_public = app_config['paths']['path_public'] path_symlink = app_config['paths']['path_symlink'] #cros module variable ##TODO upload.path_public = path_public file_service.path_public = path_public file_service.path_private = app_config['paths']['path_private'] upload.path_symlink = path_symlink upload.path_upload = app_config['paths']['path_private'] upload.path_temp = app_config['paths']['path_temp'] def index_handler(req): req.response.send_file( "%sindex.html"% path_web)
import vertx from core.file_system import FileSystem from core.http import RouteMatcher #inicialize server = vertx.create_http_server() route_matcher = RouteMatcher() logger = vertx.logger() fs = vertx.file_system() app_config = vertx.config() path_web = app_config['paths']['web'] #cros module variable ##TODO def index_handler(req): if ("js" in req.uri) or ("css" in req.uri) or ("images" in req.uri) or ("pages" in req.uri): #logger.info(req.uri) req.response.send_file("%spresenation/%s"% (path_web,req.uri)) else: req.response.send_file( "%spresentation/index.html"% path_web) @route_matcher.no_match def source_handler(req): if ("js" in req.uri) or ("css" in req.uri) or ("images" in req.uri) or ("pages" in req.uri): req.response.send_file("%spresentation/%s"% (path_web,req.uri)) route_matcher.get('/', index_handler) #set server #server.set_send_buffer_size(4 * 1024) #server.set_receive_buffer_size(100 * 1024) #logger.info("send buffer: %s"% server.send_buffer_size)
import sys sys.modules.clear() def get_subclasses(c): subclasses = c.__subclasses__() for d in list(subclasses): subclasses.extend(get_subclasses(d)) return subclasses if __name__ == '__main__': conf = vertx.config() logger = vertx.logger() #EventBus.register_handler('auth.github.login', False, log_event) rm = RouteMatcher() for cls in get_subclasses(mw.Middleware): logger.debug("init " + cls.__name__) if issubclass(cls, mw.Controller): cls().register(rm) elif issubclass(cls, mw.EventHandler): cls().register() else: cls().register() def msg_handler(message): print "Got message body %s"% message.body EventBus.register_handler('test.address', handler=msg_handler) """TODO: serve static files with apache or something""" def request_handler(req):
# Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. import vertx from core.http import RouteMatcher # Inspired from Sinatra / Express rm = RouteMatcher() def user_request_handler(req): req.response.end("User: %s ID: %s"% (req.params["user"], req.params["id"]) ) # Extract the params from the uri rm.get('/details/:user/:id', user_request_handler) def request_handler(req): req.response.send_file("route_match/index.html") # Catch all - serve the index page rm.get_re('.*', request_handler) vertx.create_http_server().request_handler(rm).listen(8080)
# http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. import vertx from test_utils import TestUtils from core.http import RouteMatcher tu = TestUtils() server = vertx.create_http_server() rm = RouteMatcher() server.request_handler(rm) client = vertx.create_http_client() client.port = 8080 class RouteMatcherTest(object): def __init__(self): self.params = {"name": "foo", "version": "v0.1"} self.re_params = {"param0": "foo", "param1": "v0.1"} self.regex = "\\/([^\\/]+)\\/([^\\/]+)" def test_get_with_pattern(self): route('get', False, "/:name/:version", self.params, "/foo/v0.1")
# -*- encoding: utf-8 -*- # usage http://localhost:8080/eventbus/hello # all event bus message handler and reply handler also executed in event loop thread import vertx import time from core.http import RouteMatcher from core.event_bus import EventBus server = vertx.create_http_server() route_matcher = RouteMatcher() def message_handler(message): # this is executed in event loop print 'Im worker and i got a message :'+str(message.body) time.sleep(5) # do some logic for data base transaction # populate return message message.reply('This is reply for message :'+str(message.body)) def reply_handler_logic(req,message): print 'im reply handler, i just invoked' time.sleep(5) key = req.params['key'] req.response.end('event bus call is called and replied from reply_handler :'+ key +str(message.body)) EventBus.register_handler('call_bus',handler=message_handler) def url_handler(req): # this is invoked by eventloop thread # read parameter from URI key = req.params['key']