Requirements: EiffelStudio 14.05 or upper
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
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)
To help coding URL routing, setup and execution,
EWF provides a set of helper classes.
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
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
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
...
Select theme