Eiffel Web Framework


EWF Router

  • What is URL dispatching ?
  • How to use the WSF_ROUTER component.


Requirements: EiffelStudio 14.05 or upper

URL dispatching

  • Incoming requests URL are handled by the server application.

  • Mapping URL or template to specific handler is called dispatching the URL, or routing the request.
  • Examples of URL mappings:
    
    https://example.com/contact     => contact_handler
    https://example.com/user/bob    => user_handler -- "bob" as parameter
    https://example.com/user/john   => user_handler -- "john" as parameter
    https://example.com/page/foo    => page_handler -- "foo" as parameter
    https://example.com/page/bar    => page_handler -- "bar" as parameter
  • Templates can be the URL path itself, URI-template, or ...

Using WSF_ROUTER

execute (req: WSF_REQUEST; res: WSF_RESPONSE)
  local
    router: WSF_ROUTER
  do
    create router.make (3)
      --| /contact => contact_handler
    router.map (create {WSF_URI_MAPPING}.make (
        "/contact", contact_handler))
      --| GET /page/{title} => page_handler
    router.map_with_request_methods (create {WSF_URI_TEMPLATE_MAPPING}.make (
        "/page/{title}", page_handler), "GET")
      --| POST /page/{title} => post_page_handler
    router.map_with_request_methods (create {WSF_URI_TEMPLATE_MAPPING}.make (
        "/page/{title}", page_handler), "POST")

      --| Execute router ...
    router.dispatch (req, res, Void)
                    

Helpers classes

To help coding URL routing, setup and execution,
EWF provides a set of helper classes.


  • WSF_ROUTED_SERVICE:
    • only setup_router needs to be implemented.
    • and optionaly the execute_default routine.

  • WSF_*_HELPER_FOR_ROUTED_SERVICE classes make mapping code simpler.

Routed service

class APPLICATION
inherit 
 WSF_DEFAULT_SERVICE redefine initialize end
 WSF_ROUTED_SERVICE  -- To have the "routed" behavior

feature -- Initialization
 initialize
   do
     Precursor
     initialize_router
   end

 setup_router
   do
         --| /contact => contact_handler
     router.map (create {WSF_URI_MAPPING}.make (
        "/contact", contact_handler))
       --| GET /page/{title} => page_handler
     router.map_with_request_methods (create {WSF_URI_TEMPLATE_MAPPING}.make (
		"/page/{title}", page_handler), "GET")
       --| POST /page/{title} => post_page_handler
     router.map_with_request_methods (create {WSF_URI_TEMPLATE_MAPPING}.make (
        "/page/{title}", post_page_handler), "POST")
   end
            

Using all helper classes...

class APPLICATION 
inherit 
 WSF_DEFAULT_SERVICE redefine initialize end
 WSF_ROUTED_SERVICE                         -- Inherit "routed" behavior
 WSF_URI_HELPER_FOR_ROUTED_SERVICE          -- Help with URI mapping
 WSF_URI_TEMPLATE_HELPER_FOR_ROUTED_SERVICE -- Help with URI-template

feature -- Initialization
 initialize 
   do
     Precursor
     initialize_router
   end

 setup_router
   do
     map_uri ("/contact", contact_handler)

     map_uri_template_with_request_methods (
         "/page/{title}", page_handler, router.methods_get
     )
     map_uri_template_with_request_methods (
         "/page/{title}", post_page_handler, router.methods_post
     )
   end
            

Router handler for pages

class PAGE_HANDLER
inherit 
	WSF_URI_TEMPLATE_HANDLER

feature -- Execution

  execute (req: WSF_REQUEST; res: WSF_RESPONSE)
    do
      if attached {WSF_STRING} req.path_parameter ("title") as l_title then
	   ...
                    

Thank you for watching

  • https://github.com/EiffelWebFramework/EWF/
  • https://eiffel.org/

Select theme